|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use Hyde\Framework\Contracts\AbstractMarkdownPage; |
|
6
|
|
|
use Hyde\Framework\Contracts\AbstractPage; |
|
7
|
|
|
use Hyde\Framework\Hyde; |
|
8
|
|
|
use Hyde\Framework\Models\Pages\BladePage; |
|
9
|
|
|
use Hyde\Framework\Models\Pages\DocumentationPage; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Dynamically constructs data for a page model. |
|
14
|
|
|
* |
|
15
|
|
|
* @see \Hyde\Framework\Testing\Feature\PageModelConstructorTest |
|
16
|
|
|
*/ |
|
17
|
|
|
class PageModelConstructor |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var AbstractPage|AbstractMarkdownPage|BladePage |
|
21
|
|
|
*/ |
|
22
|
|
|
protected AbstractPage|AbstractMarkdownPage|BladePage $page; |
|
23
|
|
|
|
|
24
|
|
|
public static function run(AbstractPage $page): AbstractPage |
|
25
|
|
|
{ |
|
26
|
|
|
return (new static($page))->get(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function __construct(AbstractPage $page) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->page = $page; |
|
32
|
|
|
$this->constructDynamicData(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function constructDynamicData(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->page->title = static::findTitleForPage(); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
if ($this->page instanceof DocumentationPage) { |
|
40
|
|
|
$this->page->category = static::getDocumentationPageCategory(); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function get(): AbstractPage |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->page; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function getDocumentationPageCategory(): ?string |
|
50
|
|
|
{ |
|
51
|
|
|
// If the documentation page is in a subdirectory, |
|
52
|
|
|
// then we can use that as the category name. |
|
53
|
|
|
// Otherwise, we look in the front matter. |
|
54
|
|
|
|
|
55
|
|
|
return str_contains($this->page->identifier, '/') |
|
56
|
|
|
? Str::before($this->page->identifier, '/') |
|
57
|
|
|
: $this->page->matter('category'); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function findTitleForPage(): string |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->page instanceof AbstractMarkdownPage |
|
63
|
|
|
? $this->findTitleForMarkdownPage() |
|
64
|
|
|
: Hyde::makeTitle($this->page->identifier); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function findTitleForMarkdownPage(): string |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->page->matter('title') |
|
70
|
|
|
?? static::findTitleFromMarkdownHeadings() |
|
|
|
|
|
|
71
|
|
|
?? Hyde::makeTitle($this->page->identifier); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function findTitleFromMarkdownHeadings(): ?string |
|
75
|
|
|
{ |
|
76
|
|
|
foreach ($this->page->markdown()->toArray() as $line) { |
|
|
|
|
|
|
77
|
|
|
if (str_starts_with($line, '# ')) { |
|
78
|
|
|
return trim(substr($line, 2), ' '); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|