8000 [SYMFONY] Add ability to configure slugify by estahn · Pull Request #104 · cocur/slugify · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[SYMFONY] Add ability to configure slugify #104

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

Merged
merged 1 commit into from
Mar 30, 2016
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
Unified Diff View
Unified
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ The bundle also provides an alias `slugify` for the `cocur_slugify` service:
$slug = $this->get('slugify')->slugify('Hello World!');
```

You can set the following configuration settings in `app/config.yml` to adjust the slugify service:

```yaml
cocur_slugify:
lowercase: <boolean>
regexp: <string>
rulesets: { }
```

### Twig

If you use the Symfony2 framework with Twig you can use the Twig filter `slugify` in your templates after you have setup
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"mikey179/vfsStream": "~1.6",
"symfony/http-kernel": "~2.4|~3.0",
"symfony/dependency-injection": "~2.4|~3.0",
"symfony/config": "~2.4|~3.0",
"twig/twig": "~1.12",
"silex/silex": "~1.3",
"pimple/pimple": "~1.1",
Expand Down
13 changes: 2 additions & 11 deletions src/Bridge/Symfony/CocurSlugifyBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Cocur\Slugify\Bridge\Symfony;

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

/**
Expand All @@ -25,16 +24,8 @@
*/
class CocurSlugifyBundle extends Bundle
{
/**
* {@inheritDoc}
*/
public function build(ContainerBuilder $container)
public function getContainerExtension()
{
parent::build($container);

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

$container->registerExtension($extension);
return new CocurSlugifyExtension();
}
}
8 changes: 7 additions & 1 deletion src/Bridge/Symfony/CocurSlugifyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ class CocurSlugifyExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$container->setDefinition('cocur_slugify', new Definition('Cocur\Slugify\Slugify'));
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

// Extract slugify arguments from config
$slugifyArguments = array_intersect_key($config, array_flip(['lowercase', 'regexp', 'rulesets']));

$container->setDefinition('cocur_slugify', new Definition('Cocur\Slugify\Slugify', [$slugifyArguments]));
$container
->setDefinition(
'cocur_slugify.twig.slugify',
Expand Down
36 changes: 36 additions & 0 deletions src/Bridge/Symfony/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the cocur/slugify package.
*
* (c) Enrico Stahn <enrico.stahn@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cocur\Slugify\Bridge\Symfony;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('cocur_slugify');

$rootNode
->children()
->booleanNode('lowercase')->defaultTrue()->end()
->scalarNode('regexp')->end()
->arrayNode('rulesets')->prototype('scalar')->end()
->end();

return $treeBuilder;
}
}
20 changes: 4 additions & 16 deletions tests/Bridge/Symfony/CocurSlugifyBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,14 @@
*/
class CocurSlugifyBundleTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->bundle = new CocurSlugifyBundle();
}

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

$this->bundle->build($container);
static::assertInstanceOf(CocurSlugifyExtension::class, $bundle->getContainerExtension());
}
}

6 changes: 2 additions & 4 deletions tests/Bridge/Symfony/CocurSlugifyExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
* file that was distributed with this source 6D4E code.
*/

namespace Cocur\Slugify\Bridge\Bundle;

use Cocur\Slugify\Bridge\Symfony\CocurSlugifyExtension;
use \Mockery as m;
namespace Cocur\Slugify\Bridge\Symfony;

use Mockery as m;

/**
* CocurSlugifyExtensionTest
Expand Down
53 changes: 53 additions & 0 deletions tests/Bridge/Symfony/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the cocur/slugify package.
*
* (c) Enrico Stahn <enrico.stahn@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cocur\Slugify\Bridge\Symfony;

use Symfony\Component\Conf 8EED ig\Definition\Processor;

class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
public function testAll()
{
$configs = array(
array(
'lowercase' => true,
'regexp' => 'abcd',
'rulesets' => ['burmese', 'hindi']
),
);

$this->process($configs);
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
public function testLowercaseOnlyAcceptsBoolean()
{
$configs = [['lowercase' => 'abc']];
$this->process($configs);
}

/**
* Processes an array of configurations and returns a compiled version.
*
* @param array $configs An array of raw configurations
*
* @return array A normalized array
*/
protected function process($configs)
{
$processor = new Processor();

return $processor->processConfiguration(new Configuration(), $configs);
}
}
0