Skip to content

Commit

Permalink
extended twig-related things instead of forcing them
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleMinotto committed Dec 28, 2015
1 parent 059a003 commit 94b9bd1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
26 changes: 20 additions & 6 deletions src/PuliServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Puli\TwigExtension\PuliTemplateLoader;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Twig_Loader_Chain;
use Twig_LoaderInterface;

class PuliServiceProvider implements ServiceProviderInterface
{
Expand Down Expand Up @@ -54,12 +56,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;
});
}
}
43 changes: 41 additions & 2 deletions tests/PuliServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand All @@ -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);
}
}

0 comments on commit 94b9bd1

Please sign in to comment.