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
|
<?php
namespace Engelsystem\Renderer;
use Engelsystem\Container\ServiceProvider;
use Engelsystem\Renderer\Twig\Extensions\Config;
use Engelsystem\Renderer\Twig\Extensions\Globals;
use Engelsystem\Renderer\Twig\Extensions\Session;
use Engelsystem\Renderer\Twig\Extensions\Translation;
use Engelsystem\Renderer\Twig\Extensions\Url;
use Twig_Environment as Twig;
use Twig_LoaderInterface as TwigLoaderInterface;
class TwigServiceProvider extends ServiceProvider
{
/** @var array */
protected $extensions = [
'config' => Config::class,
'globals' => Globals::class,
'session' => Session::class,
'url' => Url::class,
'translation' => Translation::class,
];
public function register()
{
$this->registerTwigEngine();
foreach ($this->extensions as $alias => $class) {
$this->registerTwigExtensions($class, $alias);
}
}
public function boot()
{
/** @var Twig $renderer */
$renderer = $this->app->get('twig.environment');
foreach ($this->app->tagged('twig.extension') as $extension) {
$renderer->addExtension($extension);
}
}
protected function registerTwigEngine()
{
$viewsPath = $this->app->get('path.views');
$twigLoader = $this->app->make(TwigLoader::class, ['paths' => $viewsPath]);
$this->app->instance(TwigLoader::class, $twigLoader);
$this->app->instance(TwigLoaderInterface::class, $twigLoader);
$this->app->instance('twig.loader', $twigLoader);
$twig = $this->app->make(Twig::class);
$this->app->instance(Twig::class, $twig);
$this->app->instance('twig.environment', $twig);
$twigEngine = $this->app->make(TwigEngine::class);
$this->app->instance('renderer.twigEngine', $twigEngine);
$this->app->tag('renderer.twigEngine', ['renderer.engine']);
}
/**
* @param string $class
* @param string $alias
*/
protected function registerTwigExtensions($class, $alias)
{
$alias = 'twig.extension.' . $alias;
$extension = $this->app->make($class);
$this->app->instance($class, $extension);
$this->app->instance($alias, $extension);
$this->app->tag($alias, ['twig.extension']);
}
}
|