Commit 5163e695 authored by Fabien Potencier's avatar Fabien Potencier

feature #1445 Fix some deprecations from Twig and Symfony (fabpot)

This PR was squashed before being merged into the 2.0.x-dev branch (closes #1445).

Discussion
----------

Fix some deprecations from Twig and Symfony

Commits
-------

c5506448 changed a test to help debugging
72d7746e added support for Twig runtime loaders
147d7cc0 fixed Twig deprecation notices
d2dab720 added 3.2 in test matrix
parents 512d626f c5506448
......@@ -18,6 +18,7 @@ before_script:
- sh -c "if [ '$TWIG_VERSION' != '2.0' ]; then sed -i 's/~1.8|~2.0/~1.8/g' composer.json; composer update; fi"
- sh -c "if [ '$SYMFONY_DEPS_VERSION' = '3.0' ]; then sed -i 's/~2\.8|^3\.0/3.0.*@dev/g' composer.json; composer update; fi"
- sh -c "if [ '$SYMFONY_DEPS_VERSION' = '3.1' ]; then sed -i 's/~2\.8|^3\.0/3.1.*@dev/g' composer.json; composer update; fi"
- sh -c "if [ '$SYMFONY_DEPS_VERSION' = '3.2' ]; then sed -i 's/~2\.8|^3\.0/3.2.*@dev/g' composer.json; composer update; fi"
- sh -c "if [ '$SYMFONY_DEPS_VERSION' = '' ]; then sed -i 's/~2\.8|^3\.0/2.8.*@dev/g' composer.json; composer update; fi"
- composer install
......@@ -32,5 +33,7 @@ matrix:
env: SYMFONY_DEPS_VERSION=3.0
- php: 5.6
env: SYMFONY_DEPS_VERSION=3.1
- php: 5.6
env: SYMFONY_DEPS_VERSION=3.2
- php: 7.0
- php: hhvm
<?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;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Provider\Twig\RuntimeLoader;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Extension\AssetExtension;
use Symfony\Bridge\Twig\Extension\DumpExtension;
......@@ -24,6 +25,7 @@ use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
/**
* Twig integration for Silex.
......@@ -97,8 +99,8 @@ class TwigServiceProvider implements ServiceProviderInterface
}
if (isset($app['form.factory'])) {
$app['twig.form.engine'] = function ($app) {
return new TwigRendererEngine($app['twig.form.templates']);
$app['twig.form.engine'] = function ($app) use ($twig) {
return new TwigRendererEngine($app['twig.form.templates'], $twig);
};
$app['twig.form.renderer'] = function ($app) {
......@@ -118,6 +120,10 @@ class TwigServiceProvider implements ServiceProviderInterface
if (isset($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;
......@@ -141,5 +147,20 @@ class TwigServiceProvider implements ServiceProviderInterface
$app['twig.environment_factory'] = $app->protect(function ($app) {
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']);
};
}
}
......@@ -22,6 +22,7 @@ class HttpFragmentServiceProviderTest extends \PHPUnit_Framework_TestCase
public function testRenderFunction()
{
$app = new Application();
unset($app['exception_handler']);
$app->register(new HttpFragmentServiceProvider());
$app->register(new HttpCacheServiceProvider(), array('http_cache.cache_dir' => sys_get_temp_dir()));
......
......@@ -49,11 +49,11 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
'twig.templates' => array('foo' => 'foo'),
));
$loader = $this->getMockBuilder('\Twig_LoaderInterface')->getMock();
$loader->expects($this->never())->method('getSource');
$loader->expects($this->never())->method('getSourceContext');
$app['twig.loader.filesystem'] = function ($app) use ($loader) {
return $loader;
};
$this->assertEquals('foo', $app['twig.loader']->getSource('foo'));
$this->assertEquals('foo', $app['twig.loader']->getSourceContext('foo')->getCode());
}
public function testHttpFoundationIntegration()
......
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