From f5cd2982576a7d3fadfcca40476afe8e8d486d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Tue, 17 Mar 2015 11:39:21 +0100 Subject: [PATCH] Added Nette & Latte integrations --- README.md | 49 +++++++- composer.json | 4 +- src/Bridge/Latte/SlugifyHelper.php | 37 ++++++ src/Bridge/Nette/SlugifyExtension.php | 49 ++++++++ tests/Bridge/Latte/SlugifyHelperTest.php | 36 ++++++ tests/Bridge/Nette/SlugifyExtensionTest.php | 132 ++++++++++++++++++++ 6 files changed, 305 insertions(+), 2 deletions(-) create mode 100644 src/Bridge/Latte/SlugifyHelper.php create mode 100644 src/Bridge/Nette/SlugifyExtension.php create mode 100644 tests/Bridge/Latte/SlugifyHelperTest.php create mode 100644 tests/Bridge/Nette/SlugifyExtensionTest.php diff --git a/README.md b/README.md index ef15c671..7d1f3d70 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Features - No external dependencies. - PSR-4 compatible. - Compatible with PHP >= 5.3.3 and [HHVM](http://hhvm.com). -- Integrations for [Symfony2](http://symfony.com), [Silex](http://silex.sensiolabs.org), [Laravel](http://laravel.com), [Twig](http://twig.sensiolabs.org) and [Zend Framework 2](http://framework.zend.com/). +- Integrations for [Symfony2](http://symfony.com), [Silex](http://silex.sensiolabs.org), [Laravel](http://laravel.com), [Twig](http://twig.sensiolabs.org), [Zend Framework 2](http://framework.zend.com/), [Nette Framework](http://nette.org/) and [Latte](http://latte.nette.org/). ¹ Some Esperanto transliterations conflict with others. You need to enable the Esperanto ruleset to use these transliterations. @@ -283,6 +283,53 @@ return array( ); ``` +### Nette Framework + +Slugify contains a Nette extension that allows you to use it as a service in your Nette application. You only need to register it in your `config.neon`: + +```yml +# app/config/config.neon + +extensions: + slugify: Cocur\Slugify\Bridge\Nette\SlugifyExtension +``` + +You can now use the `Cocur\Slugify\SlugifyInterface` service everywhere in your application, for example in your presenter: + +```php +class MyPresenter extends \Nette\Application\UI\Presenter +{ + + /** @var \Cocur\Slugify\SlugifyInterface @inject */ + public $slugify; + + public function renderDefault() + { + $this->template->hello = $this->slugify->slugify('Hällo Wörld'); + } + +} +``` + +### Latte + +If you use the Nette Framework with it's native Latte templating engine, you can use the Latte filter `slugify` in your templates after you have setup Nette extension (see above). + +```smarty +{$hello|slugify} +``` + +If you use Latte outside of the Nette Framework you first need to add the filter to your engine: + +```php +use Cocur\Slugify\Bridge\Latte\SlugifyHelper; +use Cocur\Slugify\Slugify; +use Latte; + +$latte = new Latte\Engine(); +$latte->addFilter('slugify', array(new SlugifyHelper(Slugify::create()), 'slugify')); +``` + Changelog --------- diff --git a/composer.json b/composer.json index b2180fa8..19e68a2e 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,9 @@ "zendframework/zend-view": "~2.2", "zendframework/zend-servicemanager": "~2.2", "codeclimate/php-test-reporter": "dev-master", - "laravel/framework": "~4.1" + "laravel/framework": "~4.1", + "nette/di": "~2.2", + "latte/latte": "~2.2" }, "autoload": { "psr-4": { diff --git a/src/Bridge/Latte/SlugifyHelper.php b/src/Bridge/Latte/SlugifyHelper.php new file mode 100644 index 00000000..8547a458 --- /dev/null +++ b/src/Bridge/Latte/SlugifyHelper.php @@ -0,0 +1,37 @@ + + * @license http://www.opensource.org/licenses/MIT The MIT License + */ +class SlugifyHelper +{ + /** @var SlugifyInterface */ + private $slugify; + + /** + * @codeCoverageIgnore + */ + public function __construct(SlugifyInterface $slugify) + { + $this->slugify = $slugify; + } + + /** + * @param string + * @param string + * @return string + */ + public function slugify($string, $separator = '-') + { + return $this->slugify->slugify($string, $separator); + } +} diff --git a/src/Bridge/Nette/SlugifyExtension.php b/src/Bridge/Nette/SlugifyExtension.php new file mode 100644 index 00000000..963d6373 --- /dev/null +++ b/src/Bridge/Nette/SlugifyExtension.php @@ -0,0 +1,49 @@ + + * @license http://www.opensource.org/licenses/MIT The MIT License + */ +class SlugifyExtension extends CompilerExtension +{ + public function loadConfiguration() + { + $builder = $this->getContainerBuilder(); + + $builder->addDefinition($this->prefix('slugify')) + ->setClass('Cocur\Slugify\SlugifyInterface') + ->setFactory('Cocur\Slugify\Slugify'); + + $builder->addDefinition($this->prefix('helper')) + ->setClass('Cocur\Slugify\Bridge\Latte\SlugifyHelper') + ->setInject(false); + } + + public function beforeCompile() + { + $builder = $this->getContainerBuilder(); + + $self = $this; + $registerToLatte = function(ServiceDefinition $def) use ($self) { + $def->addSetup('addFilter', array('slugify', array($self->prefix('@helper'), 'slugify'))); + }; + + $latteFactory = $builder->getByType('Nette\Bridges\ApplicationLatte\ILatteFactory') ?: 'nette.latteFactory'; + if ($builder->hasDefinition($latteFactory)) { + $registerToLatte($builder->getDefinition($latteFactory)); + } + + if ($builder->hasDefinition('nette.latte')) { + $registerToLatte($builder->getDefinition('nette.latte')); + } + } +} diff --git a/tests/Bridge/Latte/SlugifyHelperTest.php b/tests/Bridge/Latte/SlugifyHelperTest.php new file mode 100644 index 00000000..7cf1dbbb --- /dev/null +++ b/tests/Bridge/Latte/SlugifyHelperTest.php @@ -0,0 +1,36 @@ + + * @license http://www.opensource.org/licenses/MIT The MIT License + * @group unit + */ +class SlugifyHelperTest extends \PHPUnit_Framework_TestCase +{ + public function setUp() + { + $this->slugify = m::mock('Cocur\Slugify\SlugifyInterface'); + $this->helper = new SlugifyHelper($this->slugify); + } + + /** + * @test + * @covers Cocur\Slugify\Bridge\Latte\SlugifyHelper::slugify() + */ + public function slugify() + { + $this->slugify->shouldReceive('slugify')->with('hällo wörld', '_')->once()->andReturn('haello_woerld'); + + $this->assertEquals('haello_woerld', $this->helper->slugify('hällo wörld', '_')); + } +} diff --git a/tests/Bridge/Nette/SlugifyExtensionTest.php b/tests/Bridge/Nette/SlugifyExtensionTest.php new file mode 100644 index 00000000..b6830a16 --- /dev/null +++ b/tests/Bridge/Nette/SlugifyExtensionTest.php @@ -0,0 +1,132 @@ + + * @license http://www.opensource.org/licenses/MIT The MIT License + * @group unit + */ +class SlugifyExtensionTest extends \PHPUnit_Framework_TestCase +{ + public function setUp() + { + $this->extension = new SlugifyExtension(); + } + + /** + * @test + * @covers Cocur\Slugify\Bridge\Nette\SlugifyExtension::loadConfiguration() + */ + public function loadConfiguration() + { + $slugify = m::mock('Nette\DI\ServiceDefinition'); + $slugify + ->shouldReceive('setClass') + ->with('Cocur\Slugify\SlugifyInterface') + ->once() + ->andReturn($slugify); + $slugify + ->shouldReceive('setFactory') + ->with('Cocur\Slugify\Slugify') + ->once() + ->andReturn($slugify); + + $helper = m::mock('Nette\DI\ServiceDefinition'); + $helper + ->shouldReceive('setClass') + ->with('Cocur\Slugify\Bridge\Latte\SlugifyHelper') + ->once() + ->andReturn($helper); + $helper + ->shouldReceive('setInject') + ->with(false) + ->once() + ->andReturn($helper); + + $builder = m::mock('Nette\DI\ContainerBuilder'); + $builder + ->shouldReceive('addDefinition') + ->with('slugify.slugify') + ->once() + ->andReturn($slugify); + $builder + ->shouldReceive('addDefinition') + ->with('slugify.helper') + ->once() + ->andReturn($helper); + + $compiler = m::mock('Nette\DI\Compiler'); + $compiler + ->shouldReceive('getContainerBuilder') + ->once() + ->andReturn($builder); + + $this->extension->setCompiler($compiler, 'slugify'); + $this->extension->loadConfiguration(); + } + + /** + * @test + * @covers Cocur\Slugify\Bridge\Nette\SlugifyExtension::beforeCompile() + */ + public function beforeCompile() + { + $latteFactory = m::mock('Nette\DI\ServiceDefinition'); + $latteFactory + ->shouldReceive('addSetup') + ->with('addFilter', array('slugify', array('@slugify.helper', 'slugify'))) + ->once() + ->andReturn($latteFactory); + + $latte = m::mock('Nette\DI\ServiceDefinition'); + $latte + ->shouldReceive('addSetup') + ->with('addFilter', array('slugify', array('@slugify.helper', 'slugify'))) + ->once() + ->andReturn($latte); + + $builder = m::mock('Nette\DI\ContainerBuilder'); + $builder + ->shouldReceive('getByType') + ->with('Nette\Bridges\ApplicationLatte\ILatteFactory') + ->once() + ->andReturn('latte.latteFactory'); + $builder + ->shouldReceive('hasDefinition') + ->with('latte.latteFactory') + ->once() + ->andReturn(true); + $builder + ->shouldReceive('getDefinition') + ->with('latte.latteFactory') + ->once() + ->andReturn($latteFactory); + $builder + ->shouldReceive('hasDefinition') + ->with('nette.latte') + ->once() + ->andReturn(true); + $builder + ->shouldReceive('getDefinition') + ->with('nette.latte') + ->once() + ->andReturn($latte); + + $compiler = m::mock('Nette\DI\Compiler'); + $compiler + ->shouldReceive('getContainerBuilder') + ->once() + ->andReturn($builder); + + $this->extension->setCompiler($compiler, 'slugify'); + $this->extension->beforeCompile(); + } +}