Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
43 / 43 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
TwigServiceProvider | |
100.00% |
43 / 43 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
register | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
boot | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
registerTwigEngine | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
2 | |||
registerTwigExtensions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Renderer; |
6 | |
7 | use Engelsystem\Config\Config as EngelsystemConfig; |
8 | use Engelsystem\Container\ServiceProvider; |
9 | use Engelsystem\Renderer\Twig\Extensions\Assets; |
10 | use Engelsystem\Renderer\Twig\Extensions\Authentication; |
11 | use Engelsystem\Renderer\Twig\Extensions\Config; |
12 | use Engelsystem\Renderer\Twig\Extensions\Csrf; |
13 | use Engelsystem\Renderer\Twig\Extensions\Develop; |
14 | use Engelsystem\Renderer\Twig\Extensions\Globals; |
15 | use Engelsystem\Renderer\Twig\Extensions\Legacy; |
16 | use Engelsystem\Renderer\Twig\Extensions\Markdown; |
17 | use Engelsystem\Renderer\Twig\Extensions\Notification; |
18 | use Engelsystem\Renderer\Twig\Extensions\Session; |
19 | use Engelsystem\Renderer\Twig\Extensions\Translation; |
20 | use Engelsystem\Renderer\Twig\Extensions\Url; |
21 | use Engelsystem\Renderer\Twig\Extensions\Uuid; |
22 | use Symfony\Component\VarDumper\VarDumper; |
23 | use Twig\Environment as Twig; |
24 | use Twig\Extension\CoreExtension as TwigCore; |
25 | use Twig\Loader\LoaderInterface as TwigLoaderInterface; |
26 | use TwigBridge\Extension\Laravel\Model as TwigModel; |
27 | |
28 | class TwigServiceProvider extends ServiceProvider |
29 | { |
30 | /** @var array<string, class-string> */ |
31 | protected array $extensions = [ |
32 | 'assets' => Assets::class, |
33 | 'authentication' => Authentication::class, |
34 | 'config' => Config::class, |
35 | 'csrf' => Csrf::class, |
36 | 'develop' => Develop::class, |
37 | 'globals' => Globals::class, |
38 | 'notification' => Notification::class, |
39 | 'twigmodel' => TwigModel::class, |
40 | 'session' => Session::class, |
41 | 'legacy' => Legacy::class, |
42 | 'markdown' => Markdown::class, |
43 | 'translation' => Translation::class, |
44 | 'url' => Url::class, |
45 | 'uuid' => Uuid::class, |
46 | ]; |
47 | |
48 | public function register(): void |
49 | { |
50 | $this->registerTwigEngine(); |
51 | |
52 | foreach ($this->extensions as $alias => $class) { |
53 | $this->registerTwigExtensions($class, $alias); |
54 | } |
55 | } |
56 | |
57 | public function boot(): void |
58 | { |
59 | /** @var Twig $renderer */ |
60 | $renderer = $this->app->get('twig.environment'); |
61 | |
62 | foreach ($this->app->tagged('twig.extension') as $extension) { |
63 | $renderer->addExtension($extension); |
64 | } |
65 | |
66 | if (class_exists(VarDumper::class)) { |
67 | /** @var Develop $dev */ |
68 | $dev = $this->app->get('twig.extension.develop'); |
69 | $dev->setDumper($this->app->make(VarDumper::class)); |
70 | } |
71 | } |
72 | |
73 | protected function registerTwigEngine(): void |
74 | { |
75 | $viewsPath = $this->app->get('path.views'); |
76 | /** @var EngelsystemConfig $config */ |
77 | $config = $this->app->get('config'); |
78 | |
79 | $twigLoader = $this->app->make(TwigLoader::class, ['paths' => $viewsPath]); |
80 | $this->app->instance(TwigLoader::class, $twigLoader); |
81 | $this->app->instance(TwigLoaderInterface::class, $twigLoader); |
82 | $this->app->instance('twig.loader', $twigLoader); |
83 | |
84 | $cache = $this->app->get('path.cache.views'); |
85 | $twigDebug = false; |
86 | if ($config->get('environment') == 'development') { |
87 | $cache = false; |
88 | $twigDebug = true; |
89 | } |
90 | |
91 | $twig = $this->app->make( |
92 | Twig::class, |
93 | [ |
94 | 'options' => [ |
95 | 'cache' => $cache, |
96 | 'auto_reload' => true, |
97 | 'debug' => $twigDebug, |
98 | 'strict_variables' => $twigDebug, |
99 | ], |
100 | ] |
101 | ); |
102 | $this->app->instance(Twig::class, $twig); |
103 | $this->app->instance('twig.environment', $twig); |
104 | |
105 | /** @var TwigCore $twigCore */ |
106 | $twigCore = $twig->getExtension(TwigCore::class); |
107 | $twigCore->setTimezone($config->get('timezone')); |
108 | |
109 | $twigEngine = $this->app->make(TwigEngine::class); |
110 | $this->app->instance('renderer.twigEngine', $twigEngine); |
111 | $this->app->tag('renderer.twigEngine', ['renderer.engine']); |
112 | } |
113 | |
114 | protected function registerTwigExtensions(string $class, string $alias): void |
115 | { |
116 | $alias = 'twig.extension.' . $alias; |
117 | |
118 | $extension = $this->app->make($class); |
119 | |
120 | $this->app->instance($class, $extension); |
121 | $this->app->instance($alias, $extension); |
122 | |
123 | $this->app->tag($alias, ['twig.extension']); |
124 | } |
125 | } |