From af5fda8da4eaaa70ec83096a634273f169d916b7 Mon Sep 17 00:00:00 2001 From: Carson Full Date: Mon, 3 Aug 2015 15:48:00 -0500 Subject: [PATCH 1/3] Slugify should not flush silex routes --- src/Bridge/Silex/SlugifyServiceProvider.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Bridge/Silex/SlugifyServiceProvider.php b/src/Bridge/Silex/SlugifyServiceProvider.php index 2c5d25c9..d370d4c2 100644 --- a/src/Bridge/Silex/SlugifyServiceProvider.php +++ b/src/Bridge/Silex/SlugifyServiceProvider.php @@ -31,9 +31,7 @@ class SlugifyServiceProvider implements ServiceProviderInterface */ public function register(Application $app) { - $app['slugify'] = $app->share(function (Application $app) { - $app->flush(); - + $app['slugify'] = $app->share(function () { return new Slugify(); }); } From 14d96e1a15031b42d4eff5e228b748d06159a8be Mon Sep 17 00:00:00 2001 From: Carson Full Date: Mon, 3 Aug 2015 15:57:18 -0500 Subject: [PATCH 2/3] Added regex and options to container --- src/Bridge/Silex/SlugifyServiceProvider.php | 7 +++-- .../Bridge/Silex/SlugifySilexProviderTest.php | 26 ++++--------------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/Bridge/Silex/SlugifyServiceProvider.php b/src/Bridge/Silex/SlugifyServiceProvider.php index d370d4c2..47e1e422 100644 --- a/src/Bridge/Silex/SlugifyServiceProvider.php +++ b/src/Bridge/Silex/SlugifyServiceProvider.php @@ -31,8 +31,11 @@ class SlugifyServiceProvider implements ServiceProviderInterface */ public function register(Application $app) { - $app['slugify'] = $app->share(function () { - return new Slugify(); + $app['slugify.regex'] = null; + $app['slugify.options'] = array(); + + $app['slugify'] = $app->share(function ($app) { + return new Slugify($app['slugify.regex'], $app['slugify.options']); }); } diff --git a/tests/Bridge/Silex/SlugifySilexProviderTest.php b/tests/Bridge/Silex/SlugifySilexProviderTest.php index 08bc9390..0e4272b7 100644 --- a/tests/Bridge/Silex/SlugifySilexProviderTest.php +++ b/tests/Bridge/Silex/SlugifySilexProviderTest.php @@ -26,36 +26,20 @@ */ class SlugifyServiceProviderTest extends \PHPUnit_Framework_TestCase { - /** @var SlugifyServiceProvider */ - private $provider; - - public function setUp() - { - $this->provider = new SlugifyServiceProvider(); - } - - /** - * @test - * @covers Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider::boot() - */ - public function boot() - { - // it seems like Application is not mockable. - $app = new \Silex\Application(); - $this->provider->boot($app); - } - /** * @test - * @covers Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider::register() + * @covers Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider */ public function register() { // it seems like Application is not mockable. $app = new \Silex\Application(); - $this->provider->register($app); + $app->register(new SlugifyServiceProvider()); + $app->boot(); $this->assertArrayHasKey('slugify', $app); + $this->assertArrayHasKey('slugify.regex', $app); + $this->assertArrayHasKey('slugify.options', $app); $this->assertInstanceOf('Cocur\Slugify\Slugify', $app['slugify']); } } From cfd4397448acd0fa41a5254c5b68f60fea6ea185 Mon Sep 17 00:00:00 2001 From: Carson Full Date: Mon, 3 Aug 2015 16:04:29 -0500 Subject: [PATCH 3/3] Registering twig extension if app has twig --- src/Bridge/Silex/SlugifyServiceProvider.php | 9 +++++++++ tests/Bridge/Silex/SlugifySilexProviderTest.php | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Bridge/Silex/SlugifyServiceProvider.php b/src/Bridge/Silex/SlugifyServiceProvider.php index 47e1e422..6a612785 100644 --- a/src/Bridge/Silex/SlugifyServiceProvider.php +++ b/src/Bridge/Silex/SlugifyServiceProvider.php @@ -11,6 +11,7 @@ namespace Cocur\Slugify\Bridge\Silex; +use Cocur\Slugify\Bridge\Twig\SlugifyExtension; use Cocur\Slugify\Slugify; use Silex\Application; use Silex\ServiceProviderInterface; @@ -37,6 +38,14 @@ public function register(Application $app) $app['slugify'] = $app->share(function ($app) { return new Slugify($app['slugify.regex'], $app['slugify.options']); }); + + if (isset($app['twig'])) { + $app['twig'] = $app->share($app->extend('twig', function (\Twig_Environment $twig, $app) { + $twig->addExtension(new SlugifyExtension($app['slugify'])); + + return $twig; + })); + } } /** diff --git a/tests/Bridge/Silex/SlugifySilexProviderTest.php b/tests/Bridge/Silex/SlugifySilexProviderTest.php index 0e4272b7..3f429968 100644 --- a/tests/Bridge/Silex/SlugifySilexProviderTest.php +++ b/tests/Bridge/Silex/SlugifySilexProviderTest.php @@ -12,6 +12,8 @@ namespace Cocur\Slugify\Bridge\Silex; use Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider; +use Silex\Application; +use Silex\Provider\TwigServiceProvider; /** * SlugifyServiceProviderTest @@ -33,7 +35,7 @@ class SlugifyServiceProviderTest extends \PHPUnit_Framework_TestCase public function register() { // it seems like Application is not mockable. - $app = new \Silex\Application(); + $app = new Application(); $app->register(new SlugifyServiceProvider()); $app->boot(); @@ -42,4 +44,16 @@ public function register() $this->assertArrayHasKey('slugify.options', $app); $this->assertInstanceOf('Cocur\Slugify\Slugify', $app['slugify']); } + + /** + * @test + */ + public function registerWithTwig() + { + $app = new Application(); + $app->register(new TwigServiceProvider()); + $app->register(new SlugifyServiceProvider()); + + $this->assertTrue($app['twig']->hasExtension('slugify_extension')); + } }