Commit 72d7746e authored by Fabien Potencier's avatar Fabien Potencier

added support for Twig runtime loaders

parent 147d7cc0
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider\Twig;
use Pimple\Container;
/**
* Loads Twig extension runtimes via Pimple.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RuntimeLoader implements \Twig_RuntimeLoaderInterface
{
private $container;
private $mapping;
public function __construct(Container $container, array $mapping)
{
$this->container = $container;
$this->mapping = $mapping;
}
/**
* {@inheritdoc}
*/
public function load($class)
{
if (isset($this->mapping[$class])) {
return $this->container[$this->mapping[$class]];
}
}
}
...@@ -13,6 +13,7 @@ namespace Silex\Provider; ...@@ -13,6 +13,7 @@ namespace Silex\Provider;
use Pimple\Container; use Pimple\Container;
use Pimple\ServiceProviderInterface; use Pimple\ServiceProviderInterface;
use Silex\Provider\Twig\RuntimeLoader;
use Symfony\Bridge\Twig\AppVariable; use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Extension\AssetExtension; use Symfony\Bridge\Twig\Extension\AssetExtension;
use Symfony\Bridge\Twig\Extension\DumpExtension; use Symfony\Bridge\Twig\Extension\DumpExtension;
...@@ -24,6 +25,7 @@ use Symfony\Bridge\Twig\Extension\HttpFoundationExtension; ...@@ -24,6 +25,7 @@ use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
use Symfony\Bridge\Twig\Extension\HttpKernelExtension; use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
use Symfony\Bridge\Twig\Form\TwigRendererEngine; use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Form\TwigRenderer; use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
/** /**
* Twig integration for Silex. * Twig integration for Silex.
...@@ -118,6 +120,10 @@ class TwigServiceProvider implements ServiceProviderInterface ...@@ -118,6 +120,10 @@ class TwigServiceProvider implements ServiceProviderInterface
if (isset($app['var_dumper.cloner'])) { if (isset($app['var_dumper.cloner'])) {
$twig->addExtension(new DumpExtension($app['var_dumper.cloner'])); $twig->addExtension(new DumpExtension($app['var_dumper.cloner']));
} }
if (class_exists(HttpKernelRuntime::class)) {
$twig->addRuntimeLoader($app['twig.runtime_loader']);
}
} }
return $twig; return $twig;
...@@ -141,5 +147,20 @@ class TwigServiceProvider implements ServiceProviderInterface ...@@ -141,5 +147,20 @@ class TwigServiceProvider implements ServiceProviderInterface
$app['twig.environment_factory'] = $app->protect(function ($app) { $app['twig.environment_factory'] = $app->protect(function ($app) {
return new \Twig_Environment($app['twig.loader'], $app['twig.options']); return new \Twig_Environment($app['twig.loader'], $app['twig.options']);
}); });
$app['twig.runtime.httpkernel'] = function ($app) {
return new HttpKernelRuntime($app['fragment.handler']);
};
$app['twig.runtimes'] = function ($app) {
return array(
HttpKernelRuntime::class => 'twig.runtime.httpkernel',
TwigRenderer::class => 'twig.form.renderer',
);
};
$app['twig.runtime_loader'] = function ($app) {
return new RuntimeLoader($app, $app['twig.runtimes']);
};
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment