Passed
Push — master ( f4663c...ca30a2 )
by Caen
03:05 queued 14s
created

Includes::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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