/
TemplatingBundle.php
215 lines (180 loc) · 7.14 KB
/
TemplatingBundle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
declare(strict_types=1);
namespace Snicco\Bundle\Templating;
use InvalidArgumentException;
use RuntimeException;
use Snicco\Bundle\Templating\Option\TemplatingOption;
use Snicco\Component\HttpRouting\Routing\UrlGenerator\UrlGenerator;
use Snicco\Component\Kernel\Bundle;
use Snicco\Component\Kernel\Configuration\WritableConfig;
use Snicco\Component\Kernel\Kernel;
use Snicco\Component\Kernel\ValueObject\Directories;
use Snicco\Component\Kernel\ValueObject\Environment;
use Snicco\Component\Templating\Context\GlobalViewContext;
use Snicco\Component\Templating\Context\ViewComposer;
use Snicco\Component\Templating\Context\ViewContextResolver;
use Snicco\Component\Templating\TemplateEngine;
use Snicco\Component\Templating\ViewFactory\PHPViewFactory;
use Snicco\Component\Templating\ViewFactory\ViewFactory;
use function array_map;
use function copy;
use function dirname;
use function is_file;
use function is_readable;
use function sprintf;
final class TemplatingBundle implements Bundle
{
/**
* @var string
*/
public const ALIAS = 'snicco/templating-bundle';
public function shouldRun(Environment $env): bool
{
return true;
}
public function configure(WritableConfig $config, Kernel $kernel): void
{
$config->mergeDefaultsFromFile(dirname(__DIR__) . '/config/templating.php');
$this->copyConfiguration($kernel);
$kernel->afterConfiguration(function (WritableConfig $config) use ($kernel) {
foreach ($config->getListOfStrings('templating.directories') as $directory) {
$this->relativeToAbsDirPath($directory, $kernel->directories());
}
});
}
public function register(Kernel $kernel): void
{
$this->bindViewEngine($kernel);
$this->bindPHPViewFactory($kernel);
$this->bindViewComposerCollection($kernel);
$this->bindGlobalViewContext($kernel);
if ($kernel->usesBundle('snicco/http-routing-bundle')) {
$this->bindTemplatingMiddleware($kernel);
$this->bindExceptionDisplayer($kernel);
}
}
public function bootstrap(Kernel $kernel): void
{
}
public function alias(): string
{
return self::ALIAS;
}
private function copyConfiguration(Kernel $kernel): void
{
if (! $kernel->env()->isDevelop()) {
return;
}
$destination = $kernel->directories()
->configDir() . '/templating.php';
if (is_file($destination)) {
return;
}
$copied = copy(dirname(__DIR__) . '/config/templating.php', $destination);
if (! $copied) {
// @codeCoverageIgnoreStart
throw new RuntimeException(
sprintf('Could not copy the default templating config to destination [%s]', $destination)
);
// @codeCoverageIgnoreEnd
}
}
private function bindViewEngine(Kernel $kernel): void
{
$container = $kernel->container();
$config = $kernel->config();
$container->shared(TemplateEngine::class, function () use ($container, $config): TemplateEngine {
/** @var class-string<ViewFactory>[] $class_names */
$class_names = $config->getListOfStrings('templating.' . TemplatingOption::VIEW_FACTORIES);
$factories = array_map(
fn (string $class_name): ViewFactory => $container->make($class_name),
$class_names
);
return new TemplateEngine(...$factories);
});
}
private function bindPHPViewFactory(Kernel $kernel): void
{
$config = $kernel->config();
$dirs = $kernel->directories();
$kernel->container()
->shared(PHPViewFactory::class, fn (): PHPViewFactory => new PHPViewFactory(
$kernel->container()
->make(ViewContextResolver::class),
array_map(
fn (string $dir) => $this->relativeToAbsDirPath($dir, $dirs),
$config->getListOfStrings('templating.' . TemplatingOption::DIRECTORIES)
),
$config->getInteger('templating.' . TemplatingOption::PARENT_VIEW_PARSE_LENGTH)
));
}
private function bindTemplatingMiddleware(Kernel $kernel): void
{
$kernel->container()
->shared(TemplatingMiddleware::class, fn (): TemplatingMiddleware => new TemplatingMiddleware(
fn (): TemplateEngine => $kernel->container()
->make(TemplateEngine::class)
));
}
private function bindExceptionDisplayer(Kernel $kernel): void
{
$kernel->container()
->shared(
TemplatingExceptionDisplayer::class,
fn (): TemplatingExceptionDisplayer => new TemplatingExceptionDisplayer(
$kernel->container()
->make(TemplateEngine::class)
)
);
}
private function bindViewComposerCollection(Kernel $kernel): void
{
$kernel->container()
->shared(ViewContextResolver::class, function () use ($kernel): ViewContextResolver {
$composer_collection = new ViewContextResolver(
$kernel->container()
->make(GlobalViewContext::class),
new PsrViewComposerFactory($kernel->container())
);
$composers = $kernel->config()
->getArray('templating.' . TemplatingOption::VIEW_COMPOSERS);
/**
* @var array<class-string<ViewComposer>,list<string>> $composers
*/
foreach ($composers as $class => $views) {
$composer_collection->addComposer($views, $class);
}
return $composer_collection;
});
}
private function bindGlobalViewContext(Kernel $kernel): void
{
$kernel->container()
->shared(GlobalViewContext::class, function () use ($kernel): GlobalViewContext {
$context = new GlobalViewContext();
// This needs to be a closure.
$context->add('view', fn (): TemplateEngine => $kernel->container()->make(TemplateEngine::class));
if ($kernel->usesBundle('snicco/http-routing-bundle')) {
$context->add('url', fn (): UrlGenerator => $kernel->container()->make(UrlGenerator::class));
}
return $context;
});
}
private function relativeToAbsDirPath(string $directory_initial, Directories $kernel_directories): string
{
if (is_readable($directory_initial)) {
return $directory_initial;
}
$directory_absolute = "{$kernel_directories->baseDir()}/{$directory_initial}";
if (! is_readable($directory_absolute)) {
throw new InvalidArgumentException(
sprintf(
'templating.directories: Directory is not readable. Tried: [%s] and [%s]',
$directory_initial,
$directory_absolute
)
);
}
return $directory_absolute;
}
}