|
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\Models\Pages\BladePage; |
|
8
|
|
|
use Hyde\Framework\Models\Pages\DocumentationPage; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Dynamically constructs data for a page model. |
|
13
|
|
|
* |
|
14
|
|
|
* @see \Hyde\Framework\Testing\Feature\PageModelConstructorTest |
|
15
|
|
|
*/ |
|
16
|
|
|
class PageModelConstructor |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var AbstractPage|AbstractMarkdownPage|BladePage |
|
20
|
|
|
*/ |
|
21
|
|
|
protected AbstractPage|AbstractMarkdownPage|BladePage $page; |
|
22
|
|
|
|
|
23
|
|
|
public static function run(AbstractPage $page): AbstractPage |
|
24
|
|
|
{ |
|
25
|
|
|
return (new static($page))->get(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function __construct(AbstractPage $page) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->page = $page; |
|
31
|
|
|
$this->constructDynamicData(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function constructDynamicData(): void |
|
35
|
|
|
{ |
|
36
|
|
|
// @deprecated v0.58.x-beta (will be added to docpage schema) |
|
37
|
|
|
if ($this->page instanceof DocumentationPage) { |
|
38
|
|
|
$this->page->category = static::getDocumentationPageCategory(); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function get(): AbstractPage |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->page; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function getDocumentationPageCategory(): ?string |
|
48
|
|
|
{ |
|
49
|
|
|
// If the documentation page is in a subdirectory, |
|
50
|
|
|
// then we can use that as the category name. |
|
51
|
|
|
// Otherwise, we look in the front matter. |
|
52
|
|
|
|
|
53
|
|
|
return str_contains($this->page->identifier, '/') |
|
54
|
|
|
? Str::before($this->page->identifier, '/') |
|
55
|
|
|
: $this->page->matter('category'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|