Skip to content

Commit

Permalink
Merge pull request #25 from cocur/silex
Browse files Browse the repository at this point in the history
Added Silex service provider.
  • Loading branch information
Florian Eckerstorfer committed Apr 4, 2014
2 parents d81d0d4 + bb10418 commit be677cc
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 2 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Features
- No external dependencies.
- PSR-4 compatible.
- Compatible with PHP >= 5.3.3 and [HHVM](http://hhvm.com).
- Integrations for Symfony2 and Twig.
- Integrations for [Symfony2](http://symfony.com), [Silex](http://silex.sensiolabs.org) and [Twig](http://twig.sensiolabs.org).


Installation
Expand Down Expand Up @@ -99,6 +99,28 @@ $twig->addExtension(new SlugifyExtension(Slugify::create()));

You can find more information about registering extensions in the [Twig documentation](http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension).

### Silex

Slugify also provides a service provider to integrate into Silex.

```php
$app->register(new Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider());
```

You can use the `slugify` method in your controllers:

```php
$app->get('/', function () {
return $app['slugify']->slugify('welcome to the homepage');
});
```

And if you use Silex in combination with Twig you can also use it in your templates:

```twig
{{ app.slugify.slugify('welcome to the homepage') }}
```


Changelog
---------
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"mockery/mockery": "~0.9",
"symfony/http-kernel": "~2.4",
"symfony/dependency-injection": "~2.4",
"twig/twig": "~1"
"twig/twig": "~1",
"silex/silex": "~1.2"
},
"autoload": {
"psr-4": {
Expand Down
47 changes: 47 additions & 0 deletions src/Bridge/Silex/SlugifyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Silex;

use Cocur\Slugify\Slugify;
use Silex\Application;
use Silex\ServiceProviderInterface;

/**
* SlugifyServiceProvider
*
* @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 SlugifyServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritDoc}
*/
public function register(Application $app)
{
$app['slugify'] = $app->share(function ($app) {
$app->flush();

return new Slugify();
});
}

/**
* {@inheritDoc}
*/
public function boot(Application $app)
{
}
}
61 changes: 61 additions & 0 deletions tests/Bridge/Silex/SlugifySilexProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\Silex;

use Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider;

/**
* SlugifyServiceProviderTest
*
* @category test
* @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
* @group unit
*/
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()
*/
public function register()
{
// it seems like Application is not mockable.
$app = new \Silex\Application();
$this->provider->register($app);

$this->assertArrayHasKey('slugify', $app);
$this->assertInstanceOf('Cocur\Slugify\Slugify', $app['slugify']);
}
}

0 comments on commit be677cc

Please sign in to comment.