Commit 44183a2c authored by Fabien Potencier's avatar Fabien Potencier

merged branch fabpot/twig-perf (PR #635)

This PR was merged into the master branch.

Commits
-------

7e554db2 improved the performance of the Twig loader by not registering a chain loader if not needed

Discussion
----------

improved the performance of the Twig loader by not registering a chain loader if not needed

When `twig.templates` is empty, there is no need to register the Twig array loader as it will never be used anyway.

---------------------------------------------------------------------------

by lyrixx at 2013-02-18T09:50:07Z

totally 👍 More over, I think this feature was not documented.

---------------------------------------------------------------------------

by GromNaN at 2013-02-18T10:04:32Z

Nice tip. How much is it more performant ?

@lyrixx it is documented here : http://silex.sensiolabs.org/doc/providers/twig.html#parameters

---------------------------------------------------------------------------

by fabpot at 2013-02-18T10:06:17Z

It's not really about how much but just keeping the array loader around is a waste of time.

---------------------------------------------------------------------------

by igorw at 2013-02-18T13:37:58Z

👎 This prevents extending the `twig.loader` service and also feels like a micro-optimization to me.

---------------------------------------------------------------------------

by fabpot at 2013-02-18T13:57:13Z

I've removed the optimization but I've kept the different order in registration as it makes more sense.

---------------------------------------------------------------------------

by igorw at 2013-02-18T13:59:55Z

👍
parents c421eab3 7e554db2
......@@ -96,8 +96,8 @@ class TwigServiceProvider implements ServiceProviderInterface
$app['twig.loader'] = $app->share(function ($app) {
return new \Twig_Loader_Chain(array(
$app['twig.loader.filesystem'],
$app['twig.loader.array'],
$app['twig.loader.filesystem'],
));
});
}
......
......@@ -63,4 +63,18 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
$response = $app->handle($request);
$this->assertEquals('foo', $response->getContent());
}
public function testLoaderPriority()
{
$app = new Application();
$app->register(new TwigServiceProvider(), array(
'twig.templates' => array('foo' => 'foo'),
));
$loader = $this->getMock('\Twig_LoaderInterface');
$loader->expects($this->never())->method('getSource');
$app['twig.loader.filesystem'] = $app->share(function ($app) use ($loader) {
return $loader;
});
$this->assertEquals('foo', $app['twig.loader']->getSource('foo'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment