|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Publications\Actions; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Framework\Actions\AnonymousViewCompiler; |
|
8
|
|
|
use Hyde\Publications\Pages\PublicationListPage; |
|
9
|
|
|
use Hyde\Publications\Pages\PublicationPage; |
|
10
|
|
|
use Illuminate\Support\Facades\View; |
|
11
|
|
|
|
|
12
|
|
|
use function basename; |
|
13
|
|
|
use function str_ends_with; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @see \Hyde\Publications\Testing\Feature\PublicationPageCompilerTest |
|
17
|
|
|
*/ |
|
18
|
|
|
class PublicationPageCompiler |
|
19
|
|
|
{ |
|
20
|
|
|
protected PublicationPage|PublicationListPage $page; |
|
21
|
|
|
|
|
22
|
|
|
public static function call(PublicationPage|PublicationListPage $page): string |
|
23
|
|
|
{ |
|
24
|
|
|
return (new self($page))->__invoke(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(PublicationPage|PublicationListPage $page) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->page = $page; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function __invoke(): string |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->page instanceof PublicationPage |
|
35
|
|
|
? $this->compilePublicationPage() |
|
36
|
|
|
: $this->compilePublicationListPage(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function compilePublicationPage(): string |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->compileView($this->page->type->detailTemplate, [ |
|
42
|
|
|
'publication' => $this->page, |
|
43
|
|
|
]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function compilePublicationListPage(): string |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->compileView($this->page->type->listTemplate, [ |
|
49
|
|
|
'publicationType' => $this->page->type, |
|
50
|
|
|
]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function compileView(string $template, array $data): string |
|
54
|
|
|
{ |
|
55
|
|
|
return str_ends_with($template, '.blade.php') |
|
56
|
|
|
? AnonymousViewCompiler::handle($this->getTemplateFilePath($template), $data) |
|
57
|
|
|
: View::make($template, $data)->render(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function getTemplateFilePath(string $template): string |
|
61
|
|
|
{ |
|
62
|
|
|
$template = basename($template, '.blade.php'); |
|
63
|
|
|
|
|
64
|
|
|
return "{$this->page->type->getDirectory()}/$template.blade.php"; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|