changed templating to use setter injection for renderers
This commit is contained in:
parent
5390b16573
commit
f2ac2a4c8a
@ -30,11 +30,12 @@ class AddTemplatingRenderersPass implements CompilerPassInterface
|
||||
|
||||
$definition = $container->getDefinition('templating.engine');
|
||||
$arguments = $definition->getArguments();
|
||||
$arguments[2] = $renderers;
|
||||
$definition->setArguments($arguments);
|
||||
|
||||
$definition->addMethodCall('setRenderers', array($renderers));
|
||||
|
||||
if (count($helpers) > 0) {
|
||||
$definition->addMethodCall('setHelpers', array($helpers));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,6 @@
|
||||
<service id="templating.engine" class="%templating.engine.class%" public="false">
|
||||
<argument type="service" id="service_container" />
|
||||
<argument type="service" id="templating.loader" />
|
||||
<argument type="collection" />
|
||||
<call method="setCharset"><argument>%kernel.charset%</argument></call>
|
||||
</service>
|
||||
|
||||
|
@ -32,11 +32,11 @@ class Engine extends BaseEngine
|
||||
* @param LoaderInterface $loader A loader instance
|
||||
* @param array $renderers All templating renderers
|
||||
*/
|
||||
public function __construct(ContainerInterface $container, LoaderInterface $loader, array $renderers)
|
||||
public function __construct(ContainerInterface $container, LoaderInterface $loader)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
parent::__construct($loader, $renderers);
|
||||
parent::__construct($loader);
|
||||
}
|
||||
|
||||
public function getContainer()
|
||||
|
@ -39,14 +39,11 @@ class Engine implements \ArrayAccess
|
||||
* Constructor.
|
||||
*
|
||||
* @param LoaderInterface $loader A loader instance
|
||||
* @param array $renderers An array of renderer instances
|
||||
* @param array $helpers A array of helper instances
|
||||
* @param array $escapers An array of escapers
|
||||
*/
|
||||
public function __construct(LoaderInterface $loader, array $renderers = array(), array $helpers = array(), array $escapers = array())
|
||||
public function __construct(LoaderInterface $loader, array $helpers = array())
|
||||
{
|
||||
$this->loader = $loader;
|
||||
$this->renderers = $renderers;
|
||||
$this->helpers = array();
|
||||
$this->parents = array();
|
||||
$this->stack = array();
|
||||
@ -56,6 +53,17 @@ class Engine implements \ArrayAccess
|
||||
|
||||
$this->addHelpers($helpers);
|
||||
|
||||
$this->initializeEscapers();
|
||||
|
||||
foreach ($this->escapers as $context => $escaper) {
|
||||
$this->setEscaper($context, $escaper);
|
||||
}
|
||||
}
|
||||
|
||||
public function setRenderers(array $renderers = array())
|
||||
{
|
||||
$this->renderers = $renderers;
|
||||
|
||||
if (!isset($this->renderers['php'])) {
|
||||
$this->renderers['php'] = new PhpRenderer();
|
||||
}
|
||||
@ -63,12 +71,6 @@ class Engine implements \ArrayAccess
|
||||
foreach ($this->renderers as $renderer) {
|
||||
$renderer->setEngine($this);
|
||||
}
|
||||
|
||||
$this->initializeEscapers();
|
||||
|
||||
foreach ($this->escapers as $context => $escaper) {
|
||||
$this->setEscaper($context, $escaper);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,13 +35,17 @@ class EngineTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$engine = new ProjectTemplateEngine(self::$loader);
|
||||
$this->assertEquals(self::$loader, $engine->getLoader(), '__construct() takes a loader instance as its second first argument');
|
||||
$this->assertEquals(array('php'), array_keys($engine->getRenderers()), '__construct() automatically registers a PHP renderer if none is given');
|
||||
}
|
||||
|
||||
$engine = new ProjectTemplateEngine(self::$loader, array('foo' => self::$renderer));
|
||||
public function testSetRenderers()
|
||||
{
|
||||
$engine = new ProjectTemplateEngine(self::$loader);
|
||||
$engine->setRenderers(array('foo' => self::$renderer));
|
||||
$this->assertEquals(array('foo', 'php'), array_keys($engine->getRenderers()), '__construct() takes an array of renderers as its third argument');
|
||||
$this->assertTrue(self::$renderer->getEngine() === $engine, '__construct() registers itself on all renderers');
|
||||
|
||||
$engine = new ProjectTemplateEngine(self::$loader, array('php' => self::$renderer));
|
||||
$engine = new ProjectTemplateEngine(self::$loader);
|
||||
$engine->setRenderers(array('php' => self::$renderer));
|
||||
$this->assertTrue($engine->getRenderers() === array('php' => self::$renderer), '__construct() can overridde the default PHP renderer');
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user