Passed
Push — master ( c6d817...cca966 )
by Caen
03:03 queued 12s
created

AbstractPage::getCurrentPagePath()   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\Contracts\CompilableContract;
7
use Hyde\Framework\Contracts\FrontMatter\PageSchema;
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, PageSchema
33
{
34
    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...
35
36
    public static string $sourceDirectory;
37
    public static string $outputDirectory;
38
    public static string $fileExtension;
39
    public static string $template;
40
41
    public string $identifier;
42
    public string $routeKey;
43
44
    public FrontMatter $matter;
45
    public Metadata $metadata;
46
47
    public string $title;
48
    public ?array $navigation = null;
49
    public ?string $canonicalUrl = null;
50
51
    public function __construct(string $identifier = '', FrontMatter|array $matter = [])
52
    {
53
        $this->identifier = $identifier;
54
        $this->routeKey = trim(static::getOutputDirectory().'/'.$this->identifier, '/');
55
56
        $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...
57
        $this->constructPageSchemas();
58
        $this->metadata = new Metadata($this);
59
    }
60
61
    /** @inheritDoc */
62
    final public static function getSourceDirectory(): string
63
    {
64
        return unslash(static::$sourceDirectory);
65
    }
66
67
    /** @inheritDoc */
68
    final public static function getOutputDirectory(): string
69
    {
70
        return unslash(static::$outputDirectory);
71
    }
72
73
    /** @inheritDoc */
74
    final public static function getFileExtension(): string
75
    {
76
        return '.'.ltrim(static::$fileExtension, '.');
77
    }
78
79
    /** @inheritDoc */
80
    public static function parse(string $slug): PageContract
81
    {
82
        return (new SourceFileParser(static::class, $slug))->get();
83
    }
84
85
    /** @inheritDoc */
86
    public static function files(): array|false
87
    {
88
        return DiscoveryService::getSourceFileListForModel(static::class);
89
    }
90
91
    /** @inheritDoc */
92
    public static function all(): PageCollection
93
    {
94
        return Hyde::pages()->getPages(static::class);
95
    }
96
97
    /** @inheritDoc */
98
    public static function qualifyBasename(string $basename): string
99
    {
100
        return static::getSourceDirectory().'/'.unslash($basename).static::getFileExtension();
101
    }
102
103
    /** @inheritDoc */
104
    public static function getOutputLocation(string $basename): string
105
    {
106
        // Using the trim function we ensure we don't have a leading slash when the output directory is the root directory.
107
        return trim(
108
            static::getOutputDirectory().'/'.unslash($basename),
109
            '/'
110
        ).'.html';
111
    }
112
113
    /** @inheritDoc */
114
    public function get(string $key = null, mixed $default = null): mixed
115
    {
116
        if ($key !== null && property_exists($this, $key) && isset($this->$key)) {
117
            return $this->$key;
118
        }
119
120
        return $this->matter($key, $default);
121
    }
122
123
    /** @inheritDoc */
124
    public function matter(string $key = null, mixed $default = null): mixed
125
    {
126
        return $this->matter->get($key, $default);
127
    }
128
129
    /** @inheritDoc */
130
    public function has(string $key, bool $strict = false): bool
131
    {
132
        if ($strict) {
133
            return property_exists($this, $key) || $this->matter->has($key);
134
        }
135
136
        return ! blank($this->get($key));
137
    }
138
139
    /** @inheritDoc */
140
    public function getIdentifier(): string
141
    {
142
        return $this->identifier;
143
    }
144
145
    /** @inheritDoc */
146
    public function getSourcePath(): string
147
    {
148
        return static::qualifyBasename($this->identifier);
149
    }
150
151
    /** @inheritDoc */
152
    public function getOutputPath(): string
153
    {
154
        return $this->getRouteKey().'.html';
155
    }
156
157
    /** @inheritDoc */
158
    public function getRouteKey(): string
159
    {
160
        return $this->routeKey;
161
    }
162
163
    /** @inheritDoc */
164
    public function getRoute(): Route
165
    {
166
        return new Route($this);
167
    }
168
169
    /** @inheritDoc */
170
    public function htmlTitle(): string
171
    {
172
        return config('site.name', 'HydePHP').' - '.$this->title;
173
    }
174
175
    /** @inheritDoc */
176
    public function getBladeView(): string
177
    {
178
        return static::$template;
179
    }
180
181
    /** @inheritDoc */
182
    abstract public function compile(): string;
183
184
    public function renderPageMetadata(): string
185
    {
186
        return $this->metadata->render();
187
    }
188
189
    public function showInNavigation(): bool
190
    {
191
        return ! $this->navigation['hidden'];
192
    }
193
194
    public function navigationMenuPriority(): int
195
    {
196
        return $this->navigation['priority'];
197
    }
198
199
    public function navigationMenuTitle(): string
200
    {
201
        return $this->navigation['title'];
202
    }
203
}
204