Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extended twig-related things instead of forcing them #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/PuliServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Puli\TwigExtension\PuliTemplateLoader;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Twig_Loader_Chain;

class PuliServiceProvider implements ServiceProviderInterface
{
Expand Down Expand Up @@ -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;
});
}
}
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);
}
}