Passed
Push — master ( f9c187...0868ed )
by Caen
02:55 queued 12s
created

AbstractPage::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Concerns;
4
5
use Hyde\Framework\Actions\SourceFileParser;
6
use Hyde\Framework\Concerns\FrontMatter\Schemas\PageSchema;
7
use Hyde\Framework\Contracts\CompilableContract;
8
use Hyde\Framework\Contracts\PageContract;
9
use Hyde\Framework\Foundation\PageCollection;
10
use Hyde\Framework\Hyde;
11
use Hyde\Framework\Models\FrontMatter;
12
use Hyde\Framework\Models\Metadata\Metadata;
13
use Hyde\Framework\Models\Route;
14
use Hyde\Framework\Services\DiscoveryService;
15
16
/**
17
 * To ensure compatibility with the Hyde Framework, all Page Models should extend this class.
18
 * Markdown-based Pages can extend the AbstractMarkdownPage class to get relevant helpers.
19
 * To learn about what the methods do, see the PHPDocs in the PageContract.
20
 *
21
 * Unlike other frameworks, in general you don't instantiate pages yourself in Hyde,
22
 * instead, the page models acts as blueprints defining information for Hyde to
23
 * know how to parse a file, and what data around it should be generated.
24
 *
25
 * To create a parsed file instance, you'd typically just create a source file,
26
 * and you can then access the parsed file from the HydeKernel's page index.
27
 *
28
 * @see \Hyde\Framework\Contracts\PageContract
29
 * @see \Hyde\Framework\Concerns\AbstractMarkdownPage
30
 * @see \Hyde\Framework\Testing\Feature\AbstractPageTest
31
 */
32
abstract class AbstractPage implements PageContract, CompilableContract
33
{
34
    use PageSchema;
35
    use ConstructsPageSchemas;
0 ignored issues
show
Bug introduced by
The trait Hyde\Framework\Concerns\ConstructsPageSchemas requires the property $markdown which is not provided by Hyde\Framework\Concerns\AbstractPage.
Loading history...
36
37
    public static string $sourceDirectory;
38
    public static string $outputDirectory;
39
    public static string $fileExtension;
40
    public static string $template;
41
42
    public string $identifier;
43
    public string $routeKey;
44
45
    public FrontMatter $matter;
46
    public Metadata $metadata;
47
48
    public function __construct(string $identifier = '', FrontMatter|array $matter = [])
49
    {
50
        $this->identifier = $identifier;
51
        $this->routeKey = $this->getCurrentPagePath();
52
53
        $this->matter = $matter instanceof FrontMatter ? $matter : new FrontMatter($matter);
0 ignored issues
show
introduced by
$matter is never a sub-type of Hyde\Framework\Models\FrontMatter.
Loading history...
54
        $this->constructPageSchemas();
55
        $this->metadata = new Metadata($this);
56
    }
57
58
    /** @inheritDoc */
59
    final public static function getSourceDirectory(): string
60
    {
61
        return unslash(static::$sourceDirectory);
62
    }
63
64
    /** @inheritDoc */
65
    final public static function getOutputDirectory(): string
66
    {
67
        return unslash(static::$outputDirectory);
68
    }
69
70
    /** @inheritDoc */
71
    final public static function getFileExtension(): string
72
    {
73
        return '.'.ltrim(static::$fileExtension, '.');
74
    }
75
76
    /** @inheritDoc */
77
    public static function parse(string $slug): PageContract
78
    {
79
        return (new SourceFileParser(static::class, $slug))->get();
80
    }
81
82
    /** @inheritDoc */
83
    public static function files(): array|false
84
    {
85
        return DiscoveryService::getSourceFileListForModel(static::class);
86
    }
87
88
    /** @inheritDoc */
89
    public static function all(): PageCollection
90
    {
91
        return Hyde::pages()->getPages(static::class);
92
    }
93
94
    /** @inheritDoc */
95
    public static function qualifyBasename(string $basename): string
96
    {
97
        return static::getSourceDirectory().'/'.unslash($basename).static::getFileExtension();
98
    }
99
100
    /** @inheritDoc */
101
    public static function getOutputLocation(string $basename): string
102
    {
103
        // Using the trim function we ensure we don't have a leading slash when the output directory is the root directory.
104
        return trim(
105
            static::getOutputDirectory().'/'.unslash($basename),
106
            '/'
107
        ).'.html';
108
    }
109
110
    /** @inheritDoc */
111
    public function get(string $key = null, mixed $default = null): mixed
112
    {
113
        if ($key !== null && property_exists($this, $key) && isset($this->$key)) {
114
            return $this->$key;
115
        }
116
117
        return $this->matter($key, $default);
118
    }
119
120
    /** @inheritDoc */
121
    public function matter(string $key = null, mixed $default = null): mixed
122
    {
123
        return $this->matter->get($key, $default);
124
    }
125
126
    /** @inheritDoc */
127
    public function has(string $key, bool $strict = false): bool
128
    {
129
        if ($strict) {
130
            return property_exists($this, $key) || $this->matter->has($key);
131
        }
132
133
        return ! blank($this->get($key));
134
    }
135
136
    /** @inheritDoc */
137
    public function getIdentifier(): string
138
    {
139
        return $this->identifier;
140
    }
141
142
    /** @inheritDoc */
143
    public function getSourcePath(): string
144
    {
145
        return static::qualifyBasename($this->identifier);
146
    }
147
148
    /** @inheritDoc */
149
    public function getOutputPath(): string
150
    {
151
        return $this->getCurrentPagePath().'.html';
152
    }
153
154
    /** @inheritDoc */
155
    public function getCurrentPagePath(): string
156
    {
157
        return trim(static::getOutputDirectory().'/'.$this->identifier, '/');
158
    }
159
160
    /** @inheritDoc */
161
    public function getRouteKey(): string
162
    {
163
        return $this->routeKey;
164
    }
165
166
    /** @inheritDoc */
167
    public function getRoute(): Route
168
    {
169
        return new Route($this);
170
    }
171
172
    /** @inheritDoc */
173
    public function htmlTitle(): string
174
    {
175
        return config('site.name', 'HydePHP').' - '.$this->title;
176
    }
177
178
    /** @inheritDoc */
179
    public function getBladeView(): string
180
    {
181
        return static::$template;
182
    }
183
184
    /** @inheritDoc */
185
    abstract public function compile(): string;
186
187
    public function renderPageMetadata(): string
188
    {
189
        return $this->metadata->render();
190
    }
191
192
    public function showInNavigation(): bool
193
    {
194
        return ! $this->navigation['hidden'];
195
    }
196
197
    public function navigationMenuPriority(): int
198
    {
199
        return $this->navigation['priority'];
200
    }
201
202
    public function navigationMenuTitle(): string
203
    {
204
        return $this->navigation['title'];
205
    }
206
}
207