1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Charcoal\View; |
6
|
|
|
|
7
|
|
|
// From Pimple |
8
|
|
|
use Charcoal\View\Mustache\HelpersInterface; |
9
|
|
|
use Pimple\ServiceProviderInterface; |
10
|
|
|
use Pimple\Container; |
11
|
|
|
|
12
|
|
|
// From 'erusev/parsedown' |
13
|
|
|
use Parsedown; |
14
|
|
|
|
15
|
|
|
// From 'charcoal-view' |
16
|
|
|
use Charcoal\View\Mustache\MustacheEngine; |
17
|
|
|
use Charcoal\View\Mustache\MustacheLoader; |
18
|
|
|
use Charcoal\View\Mustache\AssetsHelpers; |
19
|
|
|
use Charcoal\View\Mustache\MarkdownHelpers; |
20
|
|
|
use Charcoal\View\Mustache\TranslatorHelpers; |
21
|
|
|
use Charcoal\View\Php\PhpEngine; |
22
|
|
|
use Charcoal\View\Php\PhpLoader; |
23
|
|
|
use Charcoal\View\Twig\TwigEngine; |
24
|
|
|
use Charcoal\View\Twig\TwigLoader; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* View Service Provider |
28
|
|
|
* |
29
|
|
|
* ## Requirements / Dependencies |
30
|
|
|
* |
31
|
|
|
* - `config` |
32
|
|
|
* - The global / base app config (`ConfigInterface`). |
33
|
|
|
* |
34
|
|
|
* ## Services |
35
|
|
|
* |
36
|
|
|
* - `view/config` |
37
|
|
|
* - The global view config (`ViewConfig`). |
38
|
|
|
* - `view` |
39
|
|
|
* - The default `ViewInterface` object, determined by `view/config`. |
40
|
|
|
* - `view/renderer` |
41
|
|
|
* - A PSR-7 renderer using the default `view` object. |
42
|
|
|
* |
43
|
|
|
* ## Helpers |
44
|
|
|
* |
45
|
|
|
* - `view/engine` |
46
|
|
|
* - The default `EngineInterface` object, determined by `view/config`. |
47
|
|
|
* - `view/loader` |
48
|
|
|
* - The defailt `LoaderInterface` object, determined by `view/config` |
49
|
|
|
* |
50
|
|
|
*/ |
51
|
|
|
class ViewServiceProvider implements ServiceProviderInterface |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Registers services on the given container. |
55
|
|
|
* |
56
|
|
|
* This method should only be used to configure services and parameters. |
57
|
|
|
* It should not get services. |
58
|
|
|
* |
59
|
|
|
* @param Container $container A container instance. |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function register(Container $container): void |
63
|
|
|
{ |
64
|
|
|
$this->registerViewConfig($container); |
|
|
|
|
65
|
|
|
$this->registerLoaderServices($container); |
|
|
|
|
66
|
|
|
$this->registerEngineServices($container); |
|
|
|
|
67
|
|
|
$this->registerMustacheTemplatingServices($container); |
68
|
|
|
$this->registerTwigTemplatingServices($container); |
|
|
|
|
69
|
|
|
$this->registerViewServices($container); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Container $container The DI container. |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
protected function registerViewConfig(Container $container): void |
77
|
|
|
{ |
78
|
|
|
/** |
79
|
|
|
* @param Container $container A container instance. |
80
|
|
|
* @return ViewConfig |
81
|
|
|
*/ |
82
|
|
|
$container['view/config'] = function (Container $container): ViewConfig { |
83
|
|
|
$appConfig = isset($container['config']) ? $container['config'] : []; |
84
|
|
|
$viewConfig = isset($appConfig['view']) ? $appConfig['view'] : null; |
85
|
|
|
$viewConfig = new ViewConfig($viewConfig); |
86
|
|
|
|
87
|
|
|
if (isset($container['module/classes'])) { |
88
|
|
|
$extraPaths = []; |
89
|
|
|
$basePath = rtrim($appConfig['base_path'], '/'); |
90
|
|
|
$modules = $container['module/classes']; |
91
|
|
|
foreach ($modules as $module) { |
92
|
|
|
if (defined(sprintf('%s::APP_CONFIG', $module))) { |
93
|
|
|
$configPath = ltrim($module::APP_CONFIG, '/'); |
94
|
|
|
$configPath = $basePath.'/'.$configPath; |
95
|
|
|
|
96
|
|
|
$configData = $viewConfig->loadFile($configPath); |
97
|
|
|
$extraPaths = array_merge( |
98
|
|
|
$extraPaths, |
99
|
|
|
$configData['view']['paths'] |
100
|
|
|
); |
101
|
|
|
}; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (!empty($extraPaths)) { |
105
|
|
|
$viewConfig->addPaths($extraPaths); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $viewConfig; |
110
|
|
|
}; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Container $container The DI container. |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function registerLoaderServices(Container $container): void |
118
|
|
|
{ |
119
|
|
|
/** |
120
|
|
|
* @param Container $container A container instance. |
121
|
|
|
* @return array The view loader dependencies array. |
122
|
|
|
*/ |
123
|
|
|
$container['view/loader/dependencies'] = function (Container $container): array { |
124
|
|
|
return [ |
125
|
|
|
'base_path' => $container['config']['base_path'], |
126
|
|
|
'paths' => $container['view/config']['paths'] |
127
|
|
|
]; |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Container $container A container instance. |
132
|
|
|
* @return MustacheLoader |
133
|
|
|
*/ |
134
|
|
|
$container['view/loader/mustache'] = function (Container $container): MustacheLoader { |
135
|
|
|
return new MustacheLoader($container['view/loader/dependencies']); |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param Container $container A container instance. |
140
|
|
|
* @return PhpLoader |
141
|
|
|
*/ |
142
|
|
|
$container['view/loader/php'] = function (Container $container): PhpLoader { |
143
|
|
|
return new PhpLoader($container['view/loader/dependencies']); |
144
|
|
|
}; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param Container $container A container instance. |
148
|
|
|
* @return TwigLoader |
149
|
|
|
*/ |
150
|
|
|
$container['view/loader/twig'] = function (Container $container): TwigLoader { |
151
|
|
|
return new TwigLoader($container['view/loader/dependencies']); |
152
|
|
|
}; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param Container $container The DI container. |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
protected function registerEngineServices(Container $container): void |
160
|
|
|
{ |
161
|
|
|
/** |
162
|
|
|
* @param Container $container A container instance. |
163
|
|
|
* @return MustacheEngine |
164
|
|
|
*/ |
165
|
|
|
$container['view/engine/mustache'] = function (Container $container) { |
166
|
|
|
return new MustacheEngine([ |
167
|
|
|
'loader' => $container['view/loader/mustache'], |
168
|
|
|
'helpers' => $container['view/mustache/helpers'], |
169
|
|
|
'cache' => $container['view/mustache/cache'] |
170
|
|
|
]); |
171
|
|
|
}; |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param Container $container A container instance. |
175
|
|
|
* @return PhpEngine |
176
|
|
|
*/ |
177
|
|
|
$container['view/engine/php'] = function (Container $container): PhpEngine { |
178
|
|
|
return new PhpEngine([ |
179
|
|
|
'loader' => $container['view/loader/php'] |
180
|
|
|
]); |
181
|
|
|
}; |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param Container $container A container instance. |
185
|
|
|
* @return TwigEngine |
186
|
|
|
*/ |
187
|
|
|
$container['view/engine/twig'] = function (Container $container): TwigEngine { |
188
|
|
|
return new TwigEngine([ |
189
|
|
|
'loader' => $container['view/loader/twig'], |
190
|
|
|
'cache' => $container['view/twig/cache'] |
191
|
|
|
]); |
192
|
|
|
}; |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* The default view engine. |
196
|
|
|
* |
197
|
|
|
* @param Container $container A container instance. |
198
|
|
|
* @return EngineInterface |
199
|
|
|
*/ |
200
|
|
|
$container['view/engine'] = function (Container $container): EngineInterface { |
201
|
|
|
$viewConfig = $container['view/config']; |
202
|
|
|
$type = $viewConfig['default_engine']; |
203
|
|
|
return $container['view/engine/'.$type]; |
204
|
|
|
}; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param Container $container The DI container. |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
protected function registerMustacheTemplatingServices(Container $container): void |
212
|
|
|
{ |
213
|
|
|
$this->registerMustacheHelpersServices($container); |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param Container $container A container instance. |
217
|
|
|
* @return string|null |
218
|
|
|
*/ |
219
|
|
|
$container['view/mustache/cache'] = function (Container $container): ?string { |
220
|
|
|
$viewConfig = $container['view/config']; |
221
|
|
|
return $viewConfig['engines.mustache.cache']; |
222
|
|
|
}; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param Container $container The DI container. |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
|
|
protected function registerMustacheHelpersServices(Container $container): void |
230
|
|
|
{ |
231
|
|
|
if (!isset($container['view/mustache/helpers'])) { |
232
|
|
|
$container['view/mustache/helpers'] = function (): array { |
233
|
|
|
return []; |
234
|
|
|
}; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Asset helpers for Mustache. |
239
|
|
|
* |
240
|
|
|
* @return AssetsHelpers |
241
|
|
|
*/ |
242
|
|
|
$container['view/mustache/helpers/assets'] = function (): AssetsHelpers { |
243
|
|
|
return new AssetsHelpers(); |
244
|
|
|
}; |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Translation helpers for Mustache. |
248
|
|
|
* |
249
|
|
|
* @return TranslatorHelpers |
250
|
|
|
*/ |
251
|
|
|
$container['view/mustache/helpers/translator'] = function (Container $container): TranslatorHelpers { |
252
|
|
|
return new TranslatorHelpers([ |
253
|
|
|
'translator' => (isset($container['translater']) ? $container['translator'] : null) |
254
|
|
|
]); |
255
|
|
|
}; |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Markdown helpers for Mustache. |
259
|
|
|
* |
260
|
|
|
* @return MarkdownHelpers |
261
|
|
|
*/ |
262
|
|
|
$container['view/mustache/helpers/markdown'] = function (Container $container): MarkdownHelpers { |
263
|
|
|
return new MarkdownHelpers([ |
264
|
|
|
'parsedown' => $container['view/parsedown'] |
265
|
|
|
]); |
266
|
|
|
}; |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Extend global helpers for the Mustache Engine. |
270
|
|
|
* |
271
|
|
|
* @param array $helpers The Mustache helper collection. |
272
|
|
|
* @param Container $container A container instance. |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
|
$container->extend('view/mustache/helpers', function (array $helpers, Container $container): array { |
276
|
|
|
return array_merge( |
277
|
|
|
$helpers, |
278
|
|
|
$container['view/mustache/helpers/assets']->toArray(), |
279
|
|
|
$container['view/mustache/helpers/translator']->toArray(), |
280
|
|
|
$container['view/mustache/helpers/markdown']->toArray() |
281
|
|
|
); |
282
|
|
|
}); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param Container $container The DI container. |
287
|
|
|
* @return void |
288
|
|
|
*/ |
289
|
|
|
protected function registerTwigTemplatingServices(Container $container) |
290
|
|
|
{ |
291
|
|
|
/** |
292
|
|
|
* @param Container $container A container instance. |
293
|
|
|
* @return string|null |
294
|
|
|
*/ |
295
|
|
|
$container['view/twig/cache'] = function (Container $container) { |
296
|
|
|
$viewConfig = $container['view/config']; |
297
|
|
|
return $viewConfig['engines.twig.cache']; |
298
|
|
|
}; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param Container $container The DI container. |
303
|
|
|
* @return void |
304
|
|
|
*/ |
305
|
|
|
protected function registerViewServices(Container $container) |
306
|
|
|
{ |
307
|
|
|
/** |
308
|
|
|
* The default view instance. |
309
|
|
|
* |
310
|
|
|
* @param Container $container A container instance. |
311
|
|
|
* @return ViewInterface |
312
|
|
|
*/ |
313
|
|
|
$container['view'] = function (Container $container) { |
314
|
|
|
return new GenericView([ |
315
|
|
|
'engine' => $container['view/engine'] |
316
|
|
|
]); |
317
|
|
|
}; |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* A PSR-7 renderer, using the default view instance. |
321
|
|
|
* |
322
|
|
|
* @param Container $container A container instance. |
323
|
|
|
* @return Renderer |
324
|
|
|
*/ |
325
|
|
|
$container['view/renderer'] = function (Container $container) { |
326
|
|
|
return new Renderer([ |
327
|
|
|
'view' => $container['view'] |
328
|
|
|
]); |
329
|
|
|
}; |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* A Markdown parser. |
333
|
|
|
* |
334
|
|
|
* @return Parsedown |
335
|
|
|
*/ |
336
|
|
|
$container['view/parsedown'] = function () { |
337
|
|
|
$parsedown = new Parsedown(); |
338
|
|
|
$parsedown->setSafeMode(true); |
339
|
|
|
return $parsedown; |
340
|
|
|
}; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: