Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Includes | |
100.00% |
20 / 20 |
|
100.00% |
5 / 5 |
13 | |
100.00% |
1 / 1 |
| path | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| get | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| html | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| markdown | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| blade | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Support; |
| 6 | |
| 7 | use Hyde\Hyde; |
| 8 | use Hyde\Markdown\Models\Markdown; |
| 9 | use Illuminate\Support\Facades\Blade; |
| 10 | use Hyde\Framework\Concerns\InteractsWithDirectories; |
| 11 | |
| 12 | use function basename; |
| 13 | use function file_exists; |
| 14 | use function file_get_contents; |
| 15 | |
| 16 | /** |
| 17 | * The Includes facade provides a simple way to access partials in the includes directory. |
| 18 | * |
| 19 | * Both Markdown and Blade includes will be rendered to HTML. |
| 20 | */ |
| 21 | class Includes |
| 22 | { |
| 23 | use InteractsWithDirectories; |
| 24 | |
| 25 | /** |
| 26 | * @var string The directory where includes are stored. |
| 27 | */ |
| 28 | protected static string $includesDirectory = 'resources/includes'; |
| 29 | |
| 30 | /** |
| 31 | * Return the path to the includes directory, or a partial within it, if requested. |
| 32 | * |
| 33 | * @param string|null $filename The partial to return, or null to return the directory. |
| 34 | * @return string Absolute Hyde::path() to the partial, or the includes directory. |
| 35 | */ |
| 36 | public static function path(?string $filename = null): string |
| 37 | { |
| 38 | static::needsDirectory(static::$includesDirectory); |
| 39 | |
| 40 | return $filename === null |
| 41 | ? Hyde::path(static::$includesDirectory) |
| 42 | : Hyde::path(static::$includesDirectory.'/'.$filename); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get the raw contents of a partial file in the includes directory. |
| 47 | * |
| 48 | * @param string $filename The name of the partial file, including the extension. |
| 49 | * @param string|null $default The default value to return if the partial is not found. |
| 50 | * @return string|null The contents of the partial file, or the default value if not found. |
| 51 | */ |
| 52 | public static function get(string $filename, ?string $default = null): ?string |
| 53 | { |
| 54 | $path = static::path($filename); |
| 55 | |
| 56 | if (! file_exists($path)) { |
| 57 | return $default; |
| 58 | } |
| 59 | |
| 60 | return file_get_contents($path); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the HTML contents of a partial file in the includes directory. |
| 65 | * |
| 66 | * @param string $filename The name of the partial file, with or without the extension. |
| 67 | * @param string|null $default The default value to return if the partial is not found. |
| 68 | * @return string|null The raw contents of the partial file, or the default value if not found. |
| 69 | */ |
| 70 | public static function html(string $filename, ?string $default = null): ?string |
| 71 | { |
| 72 | $path = static::path(basename($filename, '.html').'.html'); |
| 73 | |
| 74 | if (! file_exists($path)) { |
| 75 | return $default === null ? null : $default; |
| 76 | } |
| 77 | |
| 78 | return file_get_contents($path); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the rendered Markdown of a partial file in the includes directory. |
| 83 | * |
| 84 | * @param string $filename The name of the partial file, with or without the extension. |
| 85 | * @param string|null $default The default value to return if the partial is not found. |
| 86 | * @return string|null The rendered contents of the partial file, or the default value if not found. |
| 87 | */ |
| 88 | public static function markdown(string $filename, ?string $default = null): ?string |
| 89 | { |
| 90 | $path = static::path(basename($filename, '.md').'.md'); |
| 91 | |
| 92 | if (! file_exists($path)) { |
| 93 | return $default === null ? null : Markdown::render($default); |
| 94 | } |
| 95 | |
| 96 | return Markdown::render(file_get_contents($path)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the rendered Blade of a partial file in the includes directory. |
| 101 | * |
| 102 | * @param string $filename The name of the partial file, with or without the extension. |
| 103 | * @param string|null $default The default value to return if the partial is not found. |
| 104 | * @return string|null The rendered contents of the partial file, or the default value if not found. |
| 105 | */ |
| 106 | public static function blade(string $filename, ?string $default = null): ?string |
| 107 | { |
| 108 | $path = static::path(basename($filename, '.blade.php').'.blade.php'); |
| 109 | |
| 110 | if (! file_exists($path)) { |
| 111 | return $default === null ? null : Blade::render($default); |
| 112 | } |
| 113 | |
| 114 | return Blade::render(file_get_contents($path)); |
| 115 | } |
| 116 | } |