Sfoglia il codice sorgente

[Templating] changed helpers from Engine properties to Engine array items

For instance, instead of $view->slots->output(...), you must now write $view['slots']->output(...)

This has been changed for two main reasons:

* To be consistent with the way we access services from the container ($container['mailer'])
* To allow better organization of helpers (names can now safely contain dots for instance -- $view['project.slots']->output(...),
  whereas before, you would have written $view->get('projects.slot') or $view->{'projects.slot'})
Fabien Potencier 14 anni fa
parent
commit
7514177b51

+ 24 - 3
src/Symfony/Components/Templating/Engine.php

@@ -21,7 +21,7 @@ use Symfony\Components\Templating\Helper\HelperInterface;
  *
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  */
-class Engine
+class Engine implements \ArrayAccess
 {
     protected $loader;
     protected $renderers;
@@ -145,7 +145,7 @@ class Engine
      *
      * @throws \InvalidArgumentException if the helper is not defined
      */
-    public function __get($name)
+    public function offsetGet($name)
     {
         return $this->$name = $this->get($name);
     }
@@ -157,11 +157,32 @@ class Engine
      *
      * @return Boolean true if the helper is defined, false otherwise
      */
-    public function __isset($name)
+    public function offsetExists($name)
     {
         return isset($this->helpers[$name]);
     }
 
+    /**
+     * Sets a helper.
+     *
+     * @param HelperInterface $value The helper instance
+     * @param string          $alias An alias
+     */
+    public function offsetSet($name, $value)
+    {
+        $this->set($name, $value);
+    }
+
+    /**
+     * Removes a helper.
+     *
+     * @param string $name The helper name
+     */
+    public function offsetUnset($name)
+    {
+        throw new \LogicException(sprintf('You can\'t unset a helper (%s).', $name));
+    }
+
     /**
      * @param Helper[] $helpers An array of helper
      */

+ 9 - 9
tests/Symfony/Tests/Components/Templating/EngineTest.php

@@ -45,18 +45,18 @@ class EngineTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($engine->getRenderers() === array('php' => self::$renderer), '__construct() can overridde the default PHP renderer');
     }
 
-    public function testMagicGet()
+    public function testOffsetGet()
     {
         $engine = new ProjectTemplateEngine(self::$loader);
         $engine->set($helper = new \SimpleHelper('bar'), 'foo');
-        $this->assertEquals($helper, $engine->foo, '->__get() returns the value of a helper');
+        $this->assertEquals($helper, $engine['foo'], '->offsetGet() returns the value of a helper');
 
         try {
-            $engine->bar;
-            $this->fail('->__get() throws an InvalidArgumentException if the helper is not defined');
+            $engine['bar'];
+            $this->fail('->offsetGet() throws an InvalidArgumentException if the helper is not defined');
         } catch (\Exception $e) {
-            $this->assertInstanceOf('\InvalidArgumentException', $e, '->__get() throws an InvalidArgumentException if the helper is not defined');
-            $this->assertEquals('The helper "bar" is not defined.', $e->getMessage(), '->__get() throws an InvalidArgumentException if the helper is not defined');
+            $this->assertInstanceOf('\InvalidArgumentException', $e, '->offsetGet() throws an InvalidArgumentException if the helper is not defined');
+            $this->assertEquals('The helper "bar" is not defined.', $e->getMessage(), '->offsetGet() throws an InvalidArgumentException if the helper is not defined');
         }
     }
 
@@ -104,15 +104,15 @@ class EngineTest extends \PHPUnit_Framework_TestCase
 
         $engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
         $engine->set(new \SimpleHelper('bar'));
-        self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $view->foo.$foo ?>');
-        self::$loader->setTemplate('layout.php', '-<?php echo $view->slots->get("_content") ?>-');
+        self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $view[\'foo\'].$foo ?>');
+        self::$loader->setTemplate('layout.php', '-<?php echo $view[\'slots\']->get("_content") ?>-');
         $this->assertEquals('-barfoo-', $engine->render('foo', array('foo' => 'foo')), '->render() uses the decorator to decorate the template');
 
         $engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper()));
         $engine->set(new \SimpleHelper('bar'));
         self::$loader->setTemplate('bar.php', 'bar');
         self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $foo ?>');
-        self::$loader->setTemplate('layout.php', '<?php echo $view->render("bar") ?>-<?php echo $view->slots->get("_content") ?>-');
+        self::$loader->setTemplate('layout.php', '<?php echo $view->render("bar") ?>-<?php echo $view[\'slots\']->get("_content") ?>-');
         $this->assertEquals('bar-foo-', $engine->render('foo', array('foo' => 'foo', 'bar' => 'bar')), '->render() supports render() calls in templates');
     }