1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Contracts; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Concerns\CanBeInNavigation; |
6
|
|
|
use Hyde\Framework\Concerns\HasPageMetadata; |
7
|
|
|
use Hyde\Framework\Hyde; |
8
|
|
|
use Hyde\Framework\Models\Route; |
9
|
|
|
use Hyde\Framework\Services\DiscoveryService; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* To ensure compatibility with the Hyde Framework, all Page Models should extend this class. |
14
|
|
|
* |
15
|
|
|
* Markdown-based Pages can extend the AbstractMarkdownPage class to get relevant helpers. |
16
|
|
|
* |
17
|
|
|
* To learn about what the methods do, see the PHPDocs in the PageContract. |
18
|
|
|
* |
19
|
|
|
* @see \Hyde\Framework\Contracts\PageContract |
20
|
|
|
* @see \Hyde\Framework\Contracts\AbstractMarkdownPage |
21
|
|
|
* @test \Hyde\Framework\Testing\Feature\AbstractPageTest |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractPage implements PageContract |
24
|
|
|
{ |
25
|
|
|
use HasPageMetadata; |
|
|
|
|
26
|
|
|
use CanBeInNavigation; |
|
|
|
|
27
|
|
|
|
28
|
|
|
public static string $sourceDirectory; |
29
|
|
|
public static string $outputDirectory; |
30
|
|
|
public static string $fileExtension; |
31
|
|
|
public static string $parserClass; |
32
|
|
|
|
33
|
|
|
/** @inheritDoc */ |
34
|
|
|
final public static function getSourceDirectory(): string |
35
|
|
|
{ |
36
|
|
|
return unslash(static::$sourceDirectory); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @inheritDoc */ |
40
|
|
|
final public static function getOutputDirectory(): string |
41
|
|
|
{ |
42
|
|
|
return unslash(static::$outputDirectory); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** @inheritDoc */ |
46
|
|
|
final public static function getFileExtension(): string |
47
|
|
|
{ |
48
|
|
|
return '.'.ltrim(static::$fileExtension, '.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @inheritDoc */ |
52
|
|
|
final public static function getParserClass(): string |
53
|
|
|
{ |
54
|
|
|
return static::$parserClass; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @inheritDoc */ |
58
|
|
|
public static function getParser(string $slug): PageParserContract |
59
|
|
|
{ |
60
|
|
|
return new static::$parserClass($slug); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @inheritDoc */ |
64
|
|
|
public static function parse(string $slug): static |
65
|
|
|
{ |
66
|
|
|
return (new static::$parserClass($slug))->get(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @inheritDoc */ |
70
|
|
|
public static function files(): array |
71
|
|
|
{ |
72
|
|
|
return DiscoveryService::getSourceFileListForModel(static::class); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @inheritDoc */ |
76
|
|
|
public static function all(): Collection |
77
|
|
|
{ |
78
|
|
|
$collection = new Collection(); |
79
|
|
|
|
80
|
|
|
foreach (static::files() as $basename) { |
81
|
|
|
$collection->push(static::parse($basename)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $collection; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @inheritDoc */ |
88
|
|
|
public static function qualifyBasename(string $basename): string |
89
|
|
|
{ |
90
|
|
|
return static::getSourceDirectory().'/'.unslash($basename).static::getFileExtension(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** @inheritDoc */ |
94
|
|
|
public static function getOutputLocation(string $basename): string |
95
|
|
|
{ |
96
|
|
|
// Using the trim function we ensure we don't have a leading slash when the output directory is the root directory. |
97
|
|
|
return trim( |
98
|
|
|
static::getOutputDirectory().'/'.unslash($basename), |
99
|
|
|
'/' |
100
|
|
|
).'.html'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public string $slug; |
104
|
|
|
|
105
|
|
|
/** @inheritDoc */ |
106
|
|
|
public function getSourcePath(): string |
107
|
|
|
{ |
108
|
|
|
return static::qualifyBasename($this->slug); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** @inheritDoc */ |
112
|
|
|
public function getOutputPath(): string |
113
|
|
|
{ |
114
|
|
|
return static::getCurrentPagePath().'.html'; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** @inheritDoc */ |
118
|
|
|
public function getCurrentPagePath(): string |
119
|
|
|
{ |
120
|
|
|
return trim(static::getOutputDirectory().'/'.$this->slug, '/'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @inheritDoc */ |
124
|
|
|
public function getRoute(): Route |
125
|
|
|
{ |
126
|
|
|
return new Route($this); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** @inheritDoc */ |
130
|
|
|
public function htmlTitle(?string $title = null): string |
131
|
|
|
{ |
132
|
|
|
$pageTitle = $title ?? $this->title ?? null; |
133
|
|
|
|
134
|
|
|
return $pageTitle |
135
|
|
|
? config('site.name', 'HydePHP').' - '.$pageTitle |
136
|
|
|
: config('site.name', 'HydePHP'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|