|
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 Nette\Utils\Strings; |
|
14
|
|
|
use Symplify\PHP7_Sculpin\Contract\Renderable\DecoratorInterface; |
|
15
|
|
|
use Symplify\PHP7_Sculpin\Configuration\Configuration; |
|
16
|
|
|
use Symplify\PHP7_Sculpin\Renderable\File\File; |
|
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
|
2 |
|
public function __construct( |
|
36
|
|
|
Configuration $configuration, |
|
37
|
|
|
Engine $latteEngine, |
|
38
|
|
|
DynamicStringLoader $dynamicStringLoader |
|
39
|
|
|
) { |
|
40
|
2 |
|
$this->configuration = $configuration; |
|
41
|
2 |
|
$this->latteEngine = $latteEngine; |
|
42
|
2 |
|
$this->dynamicStringLoader = $dynamicStringLoader; |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
public function decorateFile(File $file) |
|
46
|
|
|
{ |
|
47
|
2 |
|
$options = $this->configuration->getOptions(); |
|
48
|
|
|
|
|
49
|
|
|
$parameters = [ |
|
50
|
2 |
|
'site' => $options, |
|
51
|
2 |
|
'page' => $file->getConfiguration(), |
|
52
|
|
|
'posts' => $options['posts'] ?? [], |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
2 |
|
$this->prependLayoutToFileContent($file); |
|
56
|
2 |
|
$this->addTemplateToDynamicLatteStringLoader($file); |
|
57
|
|
|
|
|
58
|
2 |
|
$htmlContent = $this->latteEngine->renderToString($file->getBaseName(), $parameters); |
|
59
|
2 |
|
$file->changeContent($htmlContent); |
|
60
|
2 |
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
private function addTemplateToDynamicLatteStringLoader(File $file) |
|
63
|
|
|
{ |
|
64
|
2 |
|
$this->dynamicStringLoader->addTemplate($file->getBaseName(), $file->getContent()); |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
private function prependLayoutToFileContent(File $file) |
|
68
|
|
|
{ |
|
69
|
2 |
|
if (Strings::startsWith($file->getContent(), '{layout')) { |
|
70
|
1 |
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
$layoutLine = sprintf('{layout "%s"}', $file->getLayout()); |
|
74
|
1 |
|
$file->changeContent($layoutLine.$file->getContent()); |
|
75
|
1 |
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|