diff --git a/src/PuliServiceProvider.php b/src/PuliServiceProvider.php index cfcc5ea..03422d7 100644 --- a/src/PuliServiceProvider.php +++ b/src/PuliServiceProvider.php @@ -15,6 +15,7 @@ use Puli\TwigExtension\PuliTemplateLoader; use Silex\Application; use Silex\ServiceProviderInterface; +use Twig_Loader_Chain; class PuliServiceProvider implements ServiceProviderInterface { @@ -54,12 +55,24 @@ public function register(Application $app) */ public function boot(Application $app) { - if (isset($app['twig']) && $app['puli.enable_twig']) { - $app['twig.loader'] = $app->share(function (Application $app) { - return new PuliTemplateLoader($app['puli.repository']); - }); - - $app['twig']->addExtension(new PuliExtension($app['puli.repository'], $app['puli.asset_url_generator'])); + if (!isset($app['twig']) || !$app['puli.enable_twig']) { + return; } + + $app->extend('twig.loader', function ($twigLoader, $app) { + if (!$twigLoader instanceof Twig_Loader_Chain) { + $twigLoader = new Twig_Loader_Chain(array($twigLoader)); + } + + $twigLoader->addLoader(new PuliTemplateLoader($app['puli.repository'])); + + return $twigLoader; + }); + + $app->extend('twig', function ($twig, $app) { + $twig->addExtension(new PuliExtension($app['puli.repository'], $app['puli.asset_url_generator'])); + + return $twig; + }); } } diff --git a/tests/PuliServiceProviderTest.php b/tests/PuliServiceProviderTest.php index d34c8f4..ba7c318 100644 --- a/tests/PuliServiceProviderTest.php +++ b/tests/PuliServiceProviderTest.php @@ -11,10 +11,13 @@ namespace Puli\SilexProvider\Tests; +use PHPUnit_Framework_Assert; use PHPUnit_Framework_TestCase; use Puli\SilexProvider\PuliServiceProvider; +use Puli\TwigExtension\PuliTemplateLoader; use Silex\Application; use Silex\Provider\TwigServiceProvider; +use Twig_Loader_Array; class PuliServiceProviderTest extends PHPUnit_Framework_TestCase { @@ -38,7 +41,37 @@ public function testConfiguredApplicationWithTwigExtension() $app->boot(); $this->assertTrue($app['twig']->hasExtension('puli')); - $this->assertInstanceOf('Puli\TwigExtension\PuliTemplateLoader', $app['twig.loader']); + + $loaders = PHPUnit_Framework_Assert::readAttribute($app['twig.loader'], 'loaders'); + $puliLoaders = array_filter($loaders, function ($loader) { + return !$loader instanceof PuliTemplateLoader; + }); + + $this->assertNotEmpty($puliLoaders); + } + + public function testConfiguredApplicationWithTwigExtensionAndLoader() + { + $app = new Application(); + + $app->register(new TwigServiceProvider()); + $app['twig.loader'] = function () { + return new Twig_Loader_Array(array()); + }; + + $app->register(new PuliServiceProvider()); + $app->boot(); + + $this->assertTrue($app['twig']->hasExtension('puli')); + + $loaders = PHPUnit_Framework_Assert::readAttribute($app['twig.loader'], 'loaders'); + + $this->assertNotEmpty(array_filter($loaders, function ($loader) { + return !$loader instanceof PuliTemplateLoader; + })); + $this->assertNotEmpty(array_filter($loaders, function ($loader) { + return !$loader instanceof Twig_Loader_Array; + })); } public function testConfiguredApplicationWithTwigExtensionDisabled() @@ -51,6 +84,12 @@ public function testConfiguredApplicationWithTwigExtensionDisabled() $app->boot(); $this->assertFalse($app['twig']->hasExtension('puli')); - $this->assertNotInstanceOf('Puli\TwigExtension\PuliTemplateLoader', $app['twig.loader']); + + $loaders = PHPUnit_Framework_Assert::readAttribute($app['twig.loader'], 'loaders'); + $puliLoaders = array_filter($loaders, function ($loader) { + return $loader instanceof PuliTemplateLoader; + }); + + $this->assertEmpty($puliLoaders); } }