1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Stempler\Bootloader; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
9
|
|
|
use Spiral\Config\ConfiguratorInterface; |
10
|
|
|
use Spiral\Config\Patch\Append; |
11
|
|
|
use Spiral\Core\Container\Autowire; |
12
|
|
|
use Spiral\Core\Container\SingletonInterface; |
13
|
|
|
use Spiral\Stempler\Builder; |
14
|
|
|
use Spiral\Stempler\Config\StemplerConfig; |
15
|
|
|
use Spiral\Stempler\Directive; |
16
|
|
|
use Spiral\Stempler\Processor\NullLocaleProcessor; |
17
|
|
|
use Spiral\Stempler\StemplerCache; |
18
|
|
|
use Spiral\Stempler\StemplerEngine; |
19
|
|
|
use Spiral\Stempler\Transform\Finalizer; |
20
|
|
|
use Spiral\Stempler\Transform\Visitor; |
21
|
|
|
use Spiral\Stempler\VisitorInterface; |
22
|
|
|
use Spiral\Translator\Views\LocaleProcessor; |
23
|
|
|
use Spiral\Views\Bootloader\ViewsBootloader; |
24
|
|
|
use Spiral\Views\Config\ViewsConfig; |
25
|
|
|
use Spiral\Views\Processor; |
26
|
|
|
use Spiral\Views\ProcessorInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initiates stempler engine, it's cache and directives. |
30
|
|
|
*/ |
31
|
|
|
final class StemplerBootloader extends Bootloader implements SingletonInterface |
32
|
|
|
{ |
33
|
|
|
protected const SINGLETONS = [ |
34
|
|
|
StemplerEngine::class => [self::class, 'stemplerEngine'], |
35
|
|
|
]; |
36
|
|
|
|
37
|
269 |
|
public function __construct( |
38
|
|
|
private readonly ConfiguratorInterface $config |
39
|
|
|
) { |
40
|
|
|
} |
41
|
|
|
|
42
|
269 |
|
public function init(ContainerInterface $container, ViewsBootloader $views): void |
43
|
|
|
{ |
44
|
269 |
|
$this->config->setDefaults( |
45
|
|
|
StemplerConfig::CONFIG, |
46
|
|
|
[ |
47
|
|
|
'directives' => [ |
48
|
|
|
Directive\PHPDirective::class, |
49
|
|
|
Directive\RouteDirective::class, |
50
|
|
|
Directive\LoopDirective::class, |
51
|
|
|
Directive\JsonDirective::class, |
52
|
|
|
Directive\ConditionalDirective::class, |
53
|
|
|
Directive\ContainerDirective::class, |
54
|
|
|
], |
55
|
|
|
'processors' => [ |
56
|
|
|
Processor\ContextProcessor::class, |
57
|
|
|
], |
58
|
|
|
'visitors' => [ |
59
|
|
|
Builder::STAGE_PREPARE => [ |
60
|
|
|
Visitor\DefineBlocks::class, |
61
|
|
|
Visitor\DefineAttributes::class, |
62
|
|
|
Visitor\DefineHidden::class, |
63
|
|
|
], |
64
|
|
|
Builder::STAGE_TRANSFORM => [ |
65
|
|
|
|
66
|
|
|
], |
67
|
|
|
Builder::STAGE_FINALIZE => [ |
68
|
|
|
Visitor\DefineStacks::class, |
69
|
|
|
Finalizer\StackCollector::class, |
70
|
|
|
], |
71
|
|
|
Builder::STAGE_COMPILE => [ |
72
|
|
|
], |
73
|
|
|
], |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
|
77
|
269 |
|
$views->addEngine(StemplerEngine::class); |
78
|
|
|
|
79
|
269 |
|
if ($container->has(LocaleProcessor::class)) { |
80
|
267 |
|
$this->addProcessor(LocaleProcessor::class); |
81
|
|
|
} else { |
82
|
2 |
|
$this->addProcessor(NullLocaleProcessor::class); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function addDirective(string|Autowire|Directive\DirectiveRendererInterface $directive): void |
87
|
|
|
{ |
88
|
|
|
$this->config->modify( |
89
|
|
|
StemplerConfig::CONFIG, |
90
|
|
|
new Append('directives', null, $directive) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
269 |
|
public function addProcessor(string|Autowire|ProcessorInterface $processor): void |
95
|
|
|
{ |
96
|
269 |
|
$this->config->modify( |
97
|
|
|
StemplerConfig::CONFIG, |
98
|
269 |
|
new Append('processors', null, $processor) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function addVisitor(string|Autowire|VisitorInterface $visitor, int $stage = Builder::STAGE_COMPILE): void |
103
|
|
|
{ |
104
|
|
|
$this->config->modify( |
105
|
|
|
StemplerConfig::CONFIG, |
106
|
|
|
new Append('visitors.' . $stage, null, $visitor) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
21 |
|
protected function stemplerEngine( |
111
|
|
|
ContainerInterface $container, |
112
|
|
|
StemplerConfig $config, |
113
|
|
|
ViewsConfig $viewConfig |
114
|
|
|
): StemplerEngine { |
115
|
21 |
|
$cache = null; |
116
|
21 |
|
if ($viewConfig->isCacheEnabled()) { |
117
|
2 |
|
$cache = new StemplerCache($viewConfig->getCacheDirectory()); |
118
|
|
|
} |
119
|
|
|
|
120
|
21 |
|
return new StemplerEngine($container, $config, $cache); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|