Browse Source

[TwigBundle] moved global variables under the app. prefix

Before:

{{ session.flash('notice') }}

After:

{{ app.session.flash('notice') }}
Fabien Potencier 14 years ago
parent
commit
450a6b39a2

+ 66 - 0
src/Symfony/Bundle/TwigBundle/GlobalVariables.php

@@ -0,0 +1,66 @@
+<?php
+
+namespace Symfony\Bundle\TwigBundle;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * AppVariables is the entry point for Symfony global variables in Twig templates.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class GlobalVariables
+{
+    protected $container;
+
+    // act as a cache to avoid calling the getters more than once
+    // request related variables cannot be cached as we can have sub-requests
+    public $security;
+    public $user;
+
+    public function __construct(ContainerInterface $container)
+    {
+        $this->container = $container;
+    }
+
+    public function getSecurity()
+    {
+        if ($this->container->has('security.context')) {
+            $this->security = $this->container->get('security.context');
+        }
+
+        return $this->security;
+    }
+
+    public function getUser()
+    {
+        if ($security = $this->getSecurity() && $user = $security->getUser()) {
+            $this->user = $user;
+        }
+
+        return $this->user;
+    }
+
+    public function getRequest()
+    {
+        if ($this->container->has('request') && $request = $this->container->get('request')) {
+            return $request;
+        }
+    }
+
+    public function getSession()
+    {
+        if ($request = $this->getRequest()) {
+            return $request->getSession();
+        }
+    }
+}

+ 3 - 26
src/Symfony/Bundle/TwigBundle/Renderer/Renderer.php

@@ -5,6 +5,7 @@ namespace Symfony\Bundle\TwigBundle\Renderer;
 use Symfony\Component\Templating\Renderer\Renderer as BaseRenderer;
 use Symfony\Component\Templating\Storage\Storage;
 use Symfony\Component\Templating\Engine;
+use Symfony\Bundle\TwigBundle\GlobalVariables;
 
 /*
  * This file is part of the Symfony package.
@@ -23,27 +24,11 @@ class Renderer extends BaseRenderer
 {
     protected $environment;
 
-    public function __construct(\Twig_Environment $environment)
+    public function __construct(\Twig_Environment $environment, GlobalVariables $globals)
     {
         $this->environment = $environment;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function setEngine(Engine $engine)
-    {
-        parent::setEngine($engine);
-
-        $container = $engine->getContainer();
-        if ($container->has('security.context')) {
-            $security = $container->get('security.context');
-            $this->environment->addGlobal('security', $security);
 
-            if ($user = $security->getUser()) {
-                $this->environment->addGlobal('user', $user);
-            }
-        }
+        $environment->addGlobal('app', $globals);
     }
 
     /**
@@ -56,14 +41,6 @@ class Renderer extends BaseRenderer
      */
     public function evaluate(Storage $template, array $parameters = array())
     {
-        $container = $this->engine->getContainer();
-
-        // cannot be set in the constructor as we need the current request
-        if ($container->has('request') && ($request = $container->get('request'))) {
-            $this->environment->addGlobal('request', $request);
-            $this->environment->addGlobal('session', $request->getSession());
-        }
-
         return $this->environment->loadTemplate($template)->render($parameters);
     }
 }

+ 6 - 0
src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

@@ -13,6 +13,7 @@
         </parameter>
         <parameter key="twig.loader.class">Symfony\Bundle\TwigBundle\Loader\Loader</parameter>
         <parameter key="twig.renderer.class">Symfony\Bundle\TwigBundle\Renderer\Renderer</parameter>
+        <parameter key="twig.globals.class">Symfony\Bundle\TwigBundle\GlobalVariables</parameter>
         <parameter key="twig.form.resources" type="collection">
             <parameter>TwigBundle::form.twig</parameter>
         </parameter>
@@ -29,9 +30,14 @@
             <argument type="service" id="templating.loader" />
         </service>
 
+        <service id="twig.globals" class="%twig.globals.class%">
+            <argument type="service" id="service_container" />
+        </service>
+
         <service id="twig.renderer" class="%twig.renderer.class%">
             <tag name="templating.renderer" alias="twig" />
             <argument type="service" id="twig" />
+            <argument type="service" id="twig.globals" />
         </service>
 
         <service id="twig.extension.trans" class="Symfony\Bundle\TwigBundle\Extension\TransExtension" public="false">

+ 9 - 8
src/Symfony/Bundle/TwigBundle/Tests/Renderer/RendererTest.php

@@ -9,15 +9,16 @@ use Symfony\Component\DependencyInjection\Container;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Session;
 use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
+use Symfony\Bundle\TwigBundle\GlobalVariables;
 
 class RendererTest extends TestCase
 {
-    public function testEvalutateAddsRequestAndSessionGlobals()
+    public function testEvalutateAddsAppGlobal()
     {
         $environment = $this->getTwigEnvironment();
-        $renderer = new Renderer($environment);
-
         $container = $this->getContainer();
+        $renderer = new Renderer($environment, $app = new GlobalVariables($container));
+
         $engine = new Engine($container, $this->getMock('Symfony\Component\Templating\Loader\LoaderInterface'), array());
 
         $storage = $this->getStorage();
@@ -33,16 +34,15 @@ class RendererTest extends TestCase
 
         $request = $container->get('request');
         $globals = $environment->getGlobals();
-        $this->assertSame($request, $globals['request']);
-        $this->assertSame($request->getSession(), $globals['session']);
+        $this->assertSame($app, $globals['app']);
     }
 
     public function testEvalutateWithoutAvailableRequest()
     {
         $environment = $this->getTwigEnvironment();
-        $renderer = new Renderer($environment);
-
         $container = new Container();
+        $renderer = new Renderer($environment, new GlobalVariables($container));
+
         $engine = new Engine($container, $this->getMock('Symfony\Component\Templating\Loader\LoaderInterface'), array());
 
         $storage = $this->getStorage();
@@ -57,7 +57,8 @@ class RendererTest extends TestCase
         $renderer->setEngine($engine);
         $renderer->evaluate($storage);
 
-        $this->assertEmpty($environment->getGlobals());
+        $globals = $environment->getGlobals();
+        $this->assertEmpty($globals['app']->getRequest());
     }
 
     /**