Passed
Push — master ( 32a4a2...da9bd5 )
by Caen
04:12 queued 14s
created

Includes::blade()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
11
/**
12
 * The Includes facade provides a simple way to access partials in the includes directory.
13
 *
14
 * Both Markdown and Blade includes will be rendered to HTML.
15
 */
16
class Includes
17
{
18
    /**
19
     * @var string The directory where includes are stored.
20
     */
21
    protected static string $includesDirectory = 'resources/includes';
22
23
    /**
24
     * Return the path to the includes directory, or a partial within it, if requested.
25
     *
26
     * @param  string|null  $filename  The partial to return, or null to return the directory.
27
     * @return string Absolute Hyde::path() to the partial, or the includes directory.
28
     */
29
    public static function path(?string $filename = null): string
30
    {
31
        static::needsDirectory(static::$includesDirectory);
32
33
        return $filename === null
34
            ? Hyde::path(static::$includesDirectory)
35
            : Hyde::path(static::$includesDirectory.'/'.$filename);
36
    }
37
38
    /**
39
     * Get the raw contents of a partial file in the includes directory.
40
     *
41
     * @param  string  $filename  The name of the partial file, including the extension.
42
     * @param  string|null  $default  The default value to return if the partial is not found.
43
     * @return string|null The contents of the partial file, or the default value if not found.
44
     */
45
    public static function get(string $filename, ?string $default = null): ?string
46
    {
47
        $path = static::path($filename);
48
49
        if (! file_exists($path)) {
50
            return $default;
51
        }
52
53
        return file_get_contents($path);
54
    }
55
56
    /**
57
     * Get the rendered Markdown of a partial file in the includes directory.
58
     *
59
     * @param  string  $filename  The name of the partial file, with or without the extension.
60
     * @param  string|null  $default  The default value to return if the partial is not found.
61
     * @return string|null The rendered contents of the partial file, or the default value if not found.
62
     */
63
    public static function markdown(string $filename, ?string $default = null): ?string
64
    {
65
        $path = static::path(basename($filename, '.md').'.md');
66
67
        if (! file_exists($path)) {
68
            return $default === null ? null : Markdown::render($default);
69
        }
70
71
        return Markdown::render(file_get_contents($path));
72
    }
73
74
    /**
75
     * Get the rendered Blade of a partial file in the includes directory.
76
     *
77
     * @param  string  $filename  The name of the partial file, with or without the extension.
78
     * @param  string|null  $default  The default value to return if the partial is not found.
79
     * @return string|null The rendered contents of the partial file, or the default value if not found.
80
     */
81
    public static function blade(string $filename, ?string $default = null): ?string
82
    {
83
        $path = static::path(basename($filename, '.blade.php').'.blade.php');
84
85
        if (! file_exists($path)) {
86
            return $default === null ? null : Blade::render($default);
87
        }
88
89
        return Blade::render(file_get_contents($path));
90
    }
91
92
    protected static function needsDirectory(string $directory): void
93
    {
94
        if (! file_exists($directory)) {
95
            mkdir($directory, recursive: true);
96
        }
97
    }
98
}
99