Skip to content

Commit e1606b0

Browse files
path resolver support for absolute/relative path and View Usage
1 parent a268b6d commit e1606b0

File tree

21 files changed

+329
-119
lines changed

21 files changed

+329
-119
lines changed

Mail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function attach(...$attachments)
198198
$this->attachments = $this->formatAttachments($attachments);
199199

200200
foreach ($this->attachments as $path => $name) {
201-
if(Tame()->exists($path)){
201+
if(File::exists($path)){
202202
$this->mailer->addAttachment($path, $name);
203203
}
204204
}

PDF.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,20 @@ public static function create(array $options = [])
8989

9090
self::init();
9191

92+
// Get the destination path
93+
$destination = Tame()->stringReplacer(self::$options['destination']);
94+
95+
// Make directory
96+
File::makeDirectory(
97+
dirname($destination)
98+
);
99+
92100
// Get the HTML content
93101
$content = self::$options['content'];
94102

95103
// if title is empty, then use the file name as title
96104
if(empty(self::$options['title'])){
97-
self::$options['title'] = pathinfo(self::$options['destination'], PATHINFO_FILENAME);
105+
self::$options['title'] = pathinfo($destination, PATHINFO_FILENAME);
98106
}
99107

100108
// Add the title tag if <html> or <head> is not present
@@ -117,19 +125,19 @@ public static function create(array $options = [])
117125
// Render PDF output to browser without saving
118126
if(in_array(self::$options['output'], ['preview', 'view']))
119127
{
120-
File::put(self::$options['destination'], self::$dompdf->output());
128+
File::put($destination, self::$dompdf->output());
121129

122130
// for reading to browser as well, from package
123-
// self::$dompdf->stream(self::$options['destination'], array("Attachment" => false));
131+
// self::$dompdf->stream($destination, array("Attachment" => false));
124132

125133
// render output to browser
126-
Tame::readPDFToBrowser(self::$options['destination'], self::$options['delete']);
134+
Tame::readPDFToBrowser($destination, self::$options['delete']);
127135
}
128136

129137
// Save PDF to server only
130138
elseif(in_array(self::$options['output'], ['save', 'saves', 'getsave']))
131139
{
132-
File::put(self::$options['destination'], self::$dompdf->output());
140+
File::put($destination, self::$dompdf->output());
133141
}
134142

135143
// Stream PDF to browser for download

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Support Package For PHP and Laravel
209209
* [Unzip](#unzip)
210210
* [Zip Download](#zip-download)
211211
* [PDF](#pdf)
212+
* [Usage](#pdf-usage)
212213
* [Read PDF](#read-pdf)
213214
* [Time](#time)
214215
* [time-usage](#time-usage)
@@ -267,6 +268,10 @@ Support Package For PHP and Laravel
267268
* [Asset](#Asset)
268269
* [Asset config](#asset-config)
269270
* [Asset Cache](#asset-cache)
271+
* [View](#view)
272+
* [Usage](#view-usage)
273+
* [Support](#view-support)
274+
* [Helper tview](#view-helper)
270275
* [Env](#env)
271276
* [Env Create](#env-create)
272277
* [Env Load](#env-load)
@@ -755,6 +760,16 @@ Mail::to('[email protected]')->send(function($reponse){
755760
});
756761
```
757762

763+
### flush
764+
- Accepts mandatory `bool` Default value is false
765+
- Clear buffers and send email in the background without waiting (But only to be used when using an API/Submitting via Ajax/Fetch or similar method of form submission)
766+
```
767+
Mail::to('[email protected]')
768+
->body('<p>Body Text</p>)
769+
->flush(true)
770+
->send();
771+
```
772+
758773
## Zip
759774
- Takes two param as `string`
760775
- [sourcePath] relative path of zip-file
@@ -799,6 +814,8 @@ TameZip()->download('newData.zip')
799814
| delete `bool` | Default is `true` --- `false` If output is `view` you can choose to delete file after preview |
800815

801816

817+
### PDF Usage
818+
802819
```
803820
Tamedevelopers\Support\PDF
804821
@@ -1166,6 +1183,84 @@ asset_config('storage/style.css', true, true);
11661183
// Output: /storage/style.css?v=111111111
11671184
```
11681185

1186+
## View
1187+
1188+
### View Usage
1189+
- Basic usage with layout and sections
1190+
1191+
```php
1192+
use Tamedevelopers\Support\View;
1193+
1194+
// Using a child view that extends a layout
1195+
$view = new View('tests.layout.home2', [
1196+
'title' => 'Homepage',
1197+
]);
1198+
1199+
echo $view->render();
1200+
```
1201+
1202+
- Rendering multiple times safely (same instance)
1203+
```php
1204+
$view = new View('tests.layout.home2', [
1205+
'title' => 'Homepage',
1206+
]);
1207+
1208+
// First render
1209+
echo $view->render();
1210+
1211+
// Second render (fresh render, no duplicated sections)
1212+
echo $view->render();
1213+
```
1214+
1215+
- Render and capture as a string
1216+
```php
1217+
$html = (new View('tests.layout.home2', ['title' => 'Homepage']))->render();
1218+
```
1219+
1220+
### View Support
1221+
- Supported extensions for views [only resolves filename]
1222+
- Similar Laravel blade syntax usage
1223+
1224+
```php
1225+
$extensions = [
1226+
'.php', // Generic / CodeIgniter / CakePHP 4+
1227+
'.blade.php', // Laravel
1228+
'.twig', // Symfony/Twig generic
1229+
'.html.twig', // Symfony typical
1230+
];
1231+
```
1232+
1233+
Samples
1234+
```blade
1235+
1236+
@extends('layout.partials.app')
1237+
1238+
1239+
@section('content')
1240+
<h1>Welcome to the Homepage!</h1>
1241+
@endsection
1242+
1243+
@include('layout.partials.footer', ['year' => 2025])
1244+
@yield('content')
1245+
```
1246+
1247+
### View Helper
1248+
- Using the helper function `tview()`
1249+
1250+
```php
1251+
// set base folder for views
1252+
// [optional], but when set - this will be the default path to look for view files.
1253+
tview()->base('tests');
1254+
1255+
// Create a view instance via helper and render
1256+
$view = tview('layout.home2', ['title' => 'Homepage']);
1257+
echo $view->render();
1258+
1259+
// One-liner (render via static call)
1260+
use Tamedevelopers\Support\View;
1261+
echo View::render('layout.home2', ['title' => 'Homepage']);
1262+
```
1263+
11691264
## Env
11701265
- By default it use the default root as `.env` path, so mandatory to install vendor in root directory.
11711266

Server.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tamedevelopers\Support;
66

77

8+
use Tamedevelopers\Support\Str;
89
use Tamedevelopers\Support\Capsule\File;
910
use Tamedevelopers\Support\Traits\ServerTrait;
1011
use Tamedevelopers\Support\Traits\ReusableTrait;
@@ -79,8 +80,9 @@ public static function config($key, $default = null, string $base_folder = 'conf
7980
*/
8081
public static function createTemplateFile(?array $data = [], ?string $filename = null)
8182
{
82-
// Get the file name
83-
$filePath = base_path($filename);
83+
// removing default base directory path if added by default
84+
$filename = Str::replace(self::formatWithBaseDirectory(), '', $filename);
85+
$filePath = Server::formatWithBaseDirectory($filename);
8486

8587
// Generate PHP code
8688
$exported = var_export($data, true);
@@ -112,9 +114,13 @@ public static function createTemplateFile(?array $data = [], ?string $filename =
112114
];
113115
PHP;
114116

117+
// Make directory
118+
$dirPath = dirname($filePath);
119+
File::makeDirectory($dirPath);
120+
115121
// to avoid warning error
116122
// we check if path is a directory first before executing the code
117-
if(is_dir(dirname($filePath))){
123+
if(File::isDirectory($dirPath)){
118124
File::put($filePath, $phpCode);
119125
}
120126
}

0 commit comments

Comments
 (0)