1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Symplify |
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Symplify\PHP7_Sculpin\Renderable\Latte; |
11
|
|
|
|
12
|
|
|
use Latte\Engine; |
13
|
|
|
use Symplify\PHP7_Sculpin\Configuration\Configuration; |
14
|
|
|
use Symplify\PHP7_Sculpin\Contract\Renderable\DecoratorInterface; |
15
|
|
|
use Symplify\PHP7_Sculpin\Renderable\File\AbstractFile; |
16
|
|
|
use Symplify\PHP7_Sculpin\Renderable\File\PostFile; |
17
|
|
|
|
18
|
|
|
final class LatteDecorator implements DecoratorInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Configuration |
22
|
|
|
*/ |
23
|
|
|
private $configuration; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Engine |
27
|
|
|
*/ |
28
|
|
|
private $latteEngine; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var DynamicStringLoader |
32
|
|
|
*/ |
33
|
|
|
private $dynamicStringLoader; |
34
|
|
|
|
35
|
8 |
|
public function __construct( |
36
|
|
|
Configuration $configuration, |
37
|
|
|
Engine $latteEngine, |
38
|
|
|
DynamicStringLoader $dynamicStringLoader |
39
|
|
|
) { |
40
|
8 |
|
$this->configuration = $configuration; |
41
|
8 |
|
$this->latteEngine = $latteEngine; |
42
|
8 |
|
$this->dynamicStringLoader = $dynamicStringLoader; |
43
|
8 |
|
} |
44
|
|
|
|
45
|
5 |
|
public function decorateFile(AbstractFile $file) |
46
|
|
|
{ |
47
|
5 |
|
$options = $this->configuration->getGlobalVariables(); |
48
|
|
|
|
49
|
5 |
|
$parameters = $file->getConfiguration() + $options + [ |
50
|
5 |
|
'posts' => $options['posts'] ?? [], |
51
|
|
|
]; |
52
|
|
|
|
53
|
5 |
|
if ($file instanceof PostFile) { |
54
|
2 |
|
$parameters['post'] = $file; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// 1. render inner post content; might be generic to any sub-layouts |
58
|
5 |
|
if ($file instanceof PostFile) { |
59
|
2 |
|
$this->addTemplateToDynamicLatteStringLoader($file); |
60
|
2 |
|
$htmlContent = $this->latteEngine->renderToString($file->getBaseName(), $parameters); |
61
|
2 |
|
$file->changeContent($htmlContent); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// 2. render outer with layout |
65
|
5 |
|
$this->prependLayoutToFileContent($file); |
66
|
5 |
|
$this->addTemplateToDynamicLatteStringLoader($file); |
67
|
5 |
|
$htmlContent = $this->latteEngine->renderToString($file->getBaseName(), $parameters); |
68
|
|
|
|
69
|
|
|
// 3. trim left-over {layout tag}, probably bug-fix |
70
|
5 |
|
if ($file instanceof PostFile) { |
71
|
2 |
|
$htmlContent = preg_replace('/{[^)]+}/', '', $htmlContent); |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
$file->changeContent($htmlContent); |
75
|
5 |
|
} |
76
|
|
|
|
77
|
5 |
|
private function addTemplateToDynamicLatteStringLoader(AbstractFile $file) |
78
|
|
|
{ |
79
|
5 |
|
$this->dynamicStringLoader->addTemplate( |
80
|
5 |
|
$file->getBaseName(), |
81
|
5 |
|
$file->getContent() |
82
|
|
|
); |
83
|
5 |
|
} |
84
|
|
|
|
85
|
5 |
|
private function prependLayoutToFileContent(AbstractFile $file) |
86
|
|
|
{ |
87
|
5 |
|
if (! $file->getLayout()) { |
88
|
4 |
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$layoutLine = sprintf('{layout "%s"}', $file->getLayout()); |
92
|
1 |
|
$file->changeContent($layoutLine . PHP_EOL . PHP_EOL . $file->getContent()); |
93
|
1 |
|
} |
94
|
|
|
} |
95
|
|
|
|