engelsystem/src/Renderer/TwigServiceProvider.php

110 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace Engelsystem\Renderer;
2018-09-02 02:09:56 +02:00
use Engelsystem\Config\Config as EngelsystemConfig;
use Engelsystem\Container\ServiceProvider;
2018-09-10 14:40:33 +02:00
use Engelsystem\Renderer\Twig\Extensions\Assets;
2018-09-03 16:33:13 +02:00
use Engelsystem\Renderer\Twig\Extensions\Authentication;
2018-08-26 12:23:47 +02:00
use Engelsystem\Renderer\Twig\Extensions\Config;
2018-09-03 16:33:13 +02:00
use Engelsystem\Renderer\Twig\Extensions\Csrf;
2018-08-26 12:23:47 +02:00
use Engelsystem\Renderer\Twig\Extensions\Globals;
2018-09-02 02:09:56 +02:00
use Engelsystem\Renderer\Twig\Extensions\Legacy;
use Engelsystem\Renderer\Twig\Extensions\Markdown;
2018-08-26 12:23:47 +02:00
use Engelsystem\Renderer\Twig\Extensions\Session;
use Engelsystem\Renderer\Twig\Extensions\Translation;
2018-08-26 12:23:47 +02:00
use Engelsystem\Renderer\Twig\Extensions\Url;
use Twig\Environment as Twig;
use Twig\Extension\CoreExtension as TwigCore;
use Twig\Loader\LoaderInterface as TwigLoaderInterface;
class TwigServiceProvider extends ServiceProvider
{
2018-08-26 12:23:47 +02:00
/** @var array */
protected $extensions = [
2018-09-23 19:13:19 +02:00
'assets' => Assets::class,
2018-09-02 02:09:56 +02:00
'authentication' => Authentication::class,
'config' => Config::class,
2018-09-03 16:33:13 +02:00
'csrf' => Csrf::class,
2018-09-02 02:09:56 +02:00
'globals' => Globals::class,
'session' => Session::class,
'legacy' => Legacy::class,
'markdown' => Markdown::class,
2018-09-02 02:09:56 +02:00
'translation' => Translation::class,
'url' => Url::class,
2018-08-26 12:23:47 +02:00
];
public function register()
{
$this->registerTwigEngine();
2018-08-26 12:23:47 +02:00
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');
2018-09-02 02:09:56 +02:00
/** @var EngelsystemConfig $config */
$config = $this->app->get('config');
$twigLoader = $this->app->make(TwigLoader::class, ['paths' => $viewsPath]);
$this->app->instance(TwigLoader::class, $twigLoader);
$this->app->instance(TwigLoaderInterface::class, $twigLoader);
2018-08-26 12:23:47 +02:00
$this->app->instance('twig.loader', $twigLoader);
$cache = $this->app->get('path.cache.views');
if ($config->get('environment') == 'development') {
$cache = false;
}
$twig = $this->app->make(
Twig::class,
[
'options' => [
'cache' => $cache,
'auto_reload' => true,
'strict_variables' => ($config->get('environment') == 'development'),
],
]
);
$this->app->instance(Twig::class, $twig);
2018-08-26 12:23:47 +02:00
$this->app->instance('twig.environment', $twig);
2018-09-02 02:09:56 +02:00
/** @var TwigCore $twigCore */
$twigCore = $twig->getExtension(TwigCore::class);
$twigCore->setTimezone($config->get('timezone'));
$twigEngine = $this->app->make(TwigEngine::class);
$this->app->instance('renderer.twigEngine', $twigEngine);
$this->app->tag('renderer.twigEngine', ['renderer.engine']);
}
2018-08-26 12:23:47 +02:00
/**
* @param string $class
* @param string $alias
*/
protected function registerTwigExtensions(string $class, string $alias)
2018-08-26 12:23:47 +02:00
{
$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']);
}
}