diff --git a/src/Symfony/Components/Templating/Engine.php b/src/Symfony/Components/Templating/Engine.php index 24a517e8b8..366be19a4d 100644 --- a/src/Symfony/Components/Templating/Engine.php +++ b/src/Symfony/Components/Templating/Engine.php @@ -21,7 +21,7 @@ use Symfony\Components\Templating\Helper\HelperInterface; * * @author Fabien Potencier */ -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 */ diff --git a/tests/Symfony/Tests/Components/Templating/EngineTest.php b/tests/Symfony/Tests/Components/Templating/EngineTest.php index b743c4ff1b..b0e20696e3 100644 --- a/tests/Symfony/Tests/Components/Templating/EngineTest.php +++ b/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', 'extend("layout"); echo $view->foo.$foo ?>'); - self::$loader->setTemplate('layout.php', '-slots->get("_content") ?>-'); + self::$loader->setTemplate('foo.php', 'extend("layout"); echo $view[\'foo\'].$foo ?>'); + self::$loader->setTemplate('layout.php', '-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', 'extend("layout"); echo $foo ?>'); - self::$loader->setTemplate('layout.php', 'render("bar") ?>-slots->get("_content") ?>-'); + self::$loader->setTemplate('layout.php', 'render("bar") ?>-get("_content") ?>-'); $this->assertEquals('bar-foo-', $engine->render('foo', array('foo' => 'foo', 'bar' => 'bar')), '->render() supports render() calls in templates'); }