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

Add Symfony2 service #23

Merged
merged 4 commits into from
Apr 3, 2014
Merged
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Features
- No external dependencies (except PHPUnit for testing).
- PSR-4 compatible
- Compatible with PHP >= 5.3.3 and [HHVM](http://hhvm.com)
- Integration as service for the Symfony2 framework


Installation
Expand Down Expand Up @@ -53,6 +54,32 @@ echo $slugify->slugify('Hello World!', '_'); // hello_world
The library also contains `Cocur\Slugify\SlugifyInterface`. Use this interface whenever you reference a type in your code.


Integrations
------------

### Symfony2

Slugify contains a Symfony2 bundle and service definition that allow you to use it as a service in your Symfony2 application. The code resides in the `Cocur\Slugify\Bridge\Bundle` namespace and you only need to add the bundle class to your `AppKernel.php`:

```php
# app/AppKernel.php

class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Cocur\Slugify\Bridge\Bundle\CocurSlugifyBundle(),
);
// ...
}

// ...
}
```


Changelog
---------

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"php": ">=5.3.3"
},
"require-dev": {
"phpunit/phpunit": "~3.7"
"phpunit/phpunit": "~3.7",
"symfony/http-kernel": "~2.4",
"symfony/dependency-injection": "~2.4"
},
"autoload": {
"psr-4": {
Expand Down
40 changes: 40 additions & 0 deletions src/Bridge/Bundle/CocurSlugifyBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* This file is part of cocur/slugify.
*
* (c) Florian Eckerstorfer <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cocur\Slugify\Bridge\Bundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* CourSlugifyBundle
*
* @package cocur/slugify
* @subpackage bridge
* @author Florian Eckerstorfer <[email protected]>
* @copyright 2012-2014 Florian Eckerstorfer
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class CocurSlugifyBundle extends Bundle
{
/**
* {@inheritDoc}
*/
public function build(ContainerBuilder $container)
{
parent::build($container);

$extension = new CocurSlugifyExtension();
$extension->load(array(), $container);

$container->registerExtension($extension);
}
}
39 changes: 39 additions & 0 deletions src/Bridge/Bundle/CocurSlugifyExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* This file is part of cocur/slugify.
*
* (c) Florian Eckerstorfer <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cocur\Slugify\Bridge\Bundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* CocurSlugifyExtension
*
* @package cocur/slugify
* @subpackage bridge
* @author Florian Eckerstorfer <[email protected]>
* @copyright 2012-2014 Florian Eckerstorfer
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class CocurSlugifyExtension extends Extension
{
/**
* {@inheritDoc}
*
* @param mixed[] $configs
* @param ContainerBuilder $container
*/
public function load(array $configs, ContainerBuilder $container)
{
$container->setDefinition('cocur_slugify', new Definition('Cocur\Slugify\Slugify'));
}
}
36 changes: 36 additions & 0 deletions tests/Bridge/Bundle/CocurSlugifyBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Cocur\Slugify\Bridge\Bundle;

use Cocur\Slugify\Bridge\Bundle\CocurSlugifyBundle;

/**
* CocurSlugifyBundleTest
*
* @group unit
*/
class CocurSlugifyBundleTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->bundle = new CocurSlugifyBundle();
}

/**
* @test
* @covers Cocur\Slugify\Bridge\Bundle\CocurSlugifyBundle::build()
*/
public function build()
{
$container = $this->getMock(
'Symfony\Component\DependencyInjection\ContainerBuilder',
array('registerExtension')
);
$container->expects($this->once())
->method('registerExtension')
->with($this->isInstanceOf('Cocur\Slugify\Bridge\Bundle\CocurSlugifyExtension'));

$this->bundle->build($container);
}
}

36 changes: 36 additions & 0 deletions tests/Bridge/Bundle/CocurSlugifyExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Cocur\Slugify\Bridge\Bundle;

use Cocur\Slugify\Bridge\Bundle\CocurSlugifyExtension;

/**
* CocurSlugifyExtensionTest
*
* @group unit
*/
class CocurSlugifyExtensionTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->extension = new CocurSlugifyExtension();
}

/**
* @test
* @covers Cocur\Slugify\Bridge\Bundle\CocurSlugifyExtension::load()
*/
public function load()
{
$container = $this->getMock(
'Symfony\Component\DependencyInjection\ContainerBuilder',
array('setDefinition')
);
$container->expects($this->once())
->method('setDefinition')
->with('cocur_slugify', $this->isInstanceOf('Symfony\Component\DependencyInjection\Definition'));

$this->extension->load(array(), $container);
}
}