From 61abc3d01f5a09016cbc739bfc87ef7d0b4bb169 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 16 Mar 2011 13:11:29 +0100 Subject: [PATCH 1/2] Added the global variable in PHP templates too --- .../FrameworkBundle/Resources/config/templating_php.xml | 5 +++++ .../Templating}/GlobalVariables.php | 2 +- .../Bundle/FrameworkBundle/Templating/PhpEngine.php | 3 ++- src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml | 7 +------ src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php | 2 +- src/Symfony/Bundle/TwigBundle/TwigEngine.php | 1 + 6 files changed, 11 insertions(+), 9 deletions(-) rename src/Symfony/Bundle/{TwigBundle => FrameworkBundle/Templating}/GlobalVariables.php (97%) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml index 1d3323aa3a..b9b30b83b3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml @@ -15,6 +15,7 @@ Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper + Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables @@ -71,5 +72,9 @@ + + + + diff --git a/src/Symfony/Bundle/TwigBundle/GlobalVariables.php b/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php similarity index 97% rename from src/Symfony/Bundle/TwigBundle/GlobalVariables.php rename to src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php index 1b493bf9ba..8eeb010fb3 100644 --- a/src/Symfony/Bundle/TwigBundle/GlobalVariables.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Bundle\TwigBundle; +namespace Symfony\Bundle\FrameworkBundle\Templating; use Symfony\Component\DependencyInjection\ContainerInterface; diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/PhpEngine.php b/src/Symfony/Bundle/FrameworkBundle/Templating/PhpEngine.php index 6bcf917f49..64d89aadc5 100755 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/PhpEngine.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/PhpEngine.php @@ -33,11 +33,12 @@ class PhpEngine extends BasePhpEngine implements EngineInterface * @param ContainerInterface $container The DI container * @param LoaderInterface $loader A loader instance */ - public function __construct(TemplateNameParserInterface $parser, ContainerInterface $container, LoaderInterface $loader) + public function __construct(TemplateNameParserInterface $parser, ContainerInterface $container, LoaderInterface $loader, GlobalVariables $globals) { $this->container = $container; parent::__construct($parser, $loader); + $this->addGlobal('app', $globals); } /** diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 6283e3ea70..2220db3fae 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -7,7 +7,6 @@ Twig_Environment Symfony\Bundle\TwigBundle\Loader\FilesystemLoader - Symfony\Bundle\TwigBundle\GlobalVariables Symfony\Bundle\TwigBundle\TwigEngine Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheCacheWarmer @@ -30,11 +29,7 @@ - - - - - + diff --git a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php index c66496d551..23889abd7a 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php @@ -17,7 +17,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session; use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; use Symfony\Component\Templating\TemplateNameParser; -use Symfony\Bundle\TwigBundle\GlobalVariables; +use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; class TwigEngineTest extends TestCase { diff --git a/src/Symfony/Bundle/TwigBundle/TwigEngine.php b/src/Symfony/Bundle/TwigBundle/TwigEngine.php index 83ad8542f6..5d3f9181ee 100644 --- a/src/Symfony/Bundle/TwigBundle/TwigEngine.php +++ b/src/Symfony/Bundle/TwigBundle/TwigEngine.php @@ -12,6 +12,7 @@ namespace Symfony\Bundle\TwigBundle; use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; +use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; use Symfony\Component\Templating\TemplateNameParserInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\DependencyInjection\ContainerInterface; From 8dc6464aa4bf586d99d0ce5b8ccb7185096c420a Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 16 Mar 2011 14:47:21 +0100 Subject: [PATCH 2/2] [FrameworkBundle] Fixed previous commit and added some tests for PHP globals --- .../Resources/config/templating_php.xml | 1 + .../Tests/Templating/PhpEngineTest.php | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml index b9b30b83b3..41cf5fbb63 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml @@ -23,6 +23,7 @@ + %kernel.charset% diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php new file mode 100644 index 0000000000..027ffec616 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Templating; + +use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine; +use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session; +use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; +use Symfony\Component\Templating\TemplateNameParser; +use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; +use Symfony\Bundle\FrameworkBundle\Tests\TestCase; + +class PhpEngineTest extends TestCase +{ + public function testEvaluateAddsAppGlobal() + { + $container = $this->getContainer(); + $loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader'); + $engine = new PhpEngine(new TemplateNameParser(), $container, $loader, $app = new GlobalVariables($container)); + $globals = $engine->getGlobals(); + $this->assertSame($app, $globals['app']); + } + + public function testEvaluateWithoutAvailableRequest() + { + $container = new Container(); + $loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader'); + $engine = new PhpEngine(new TemplateNameParser(), $container, $loader, $app = new GlobalVariables($container)); + + $container->set('request', null); + + $globals = $engine->getGlobals(); + $this->assertEmpty($globals['app']->getRequest()); + } + + /** + * Creates a Container with a Session-containing Request service. + * + * @return Container + */ + protected function getContainer() + { + $container = new Container(); + $request = new Request(); + $session = new Session(new ArraySessionStorage()); + + $request->setSession($session); + $container->set('request', $request); + + return $container; + } +}