Skip to content

Commit

Permalink
Add support for Cocur Slugify library
Browse files Browse the repository at this point in the history
  • Loading branch information
Bladrak committed Oct 22, 2014
1 parent 76cbe67 commit df3b2bc
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 15 deletions.
4 changes: 3 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public function getConfigTreeBuilder()
))
->prototype('scalar')->end()
->end()

->scalarNode('slugify_service')
->defaultValue('sonata.core.slugify.native') # you should use: sonata.core.slugify.cocur
->end()
->arrayNode('ignore_routes')
->defaultValue(array(
'sonata_page_cache_esi',
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/SonataPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->configureClassesToCompile();

$container->setParameter('sonata.page.assets', $config['assets']);
$container->setParameter('sonata.page.slugify_service', $config['slugify_service']);

$container->setParameter('sonata.page.is_inline_edition_on', $config['is_inline_edition_on']);
$container->getDefinition('sonata.page.decorator_strategy')
Expand Down
49 changes: 36 additions & 13 deletions Model/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ abstract class Page implements PageInterface

protected $edited;

protected static $slugifyMethod;

/**
* @return mixed
*/
public static function getSlugifyMethod()
{
return self::$slugifyMethod;
}

/**
* @param mixed $slugifyMethod
*/
public static function setSlugifyMethod(\Closure $slugifyMethod)
{
self::$slugifyMethod = $slugifyMethod;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -678,24 +696,29 @@ public function getUrl()
*/
public static function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// this code is for BC
if (!self::$slugifyMethod) {
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);

// trim
$text = trim($text, '-');
// trim
$text = trim($text, '-');

// transliterate
if (function_exists('iconv')) {
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}
// transliterate
if (function_exists('iconv')) {
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}

// lowercase
$text = strtolower($text);

// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);

// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
return $text;
}

return $text;
return call_user_func(self::$slugifyMethod, $text);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Resources/doc/reference/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Before we can go on with generating our Application files trough the ``EasyExten
.. code-block:: yaml
sonata_page:
multisite: host
slugify_method: sonata.core.slugify.cocur # old BC value is sonata.core.slugify.native
multisite: host
default_template: default # template key from templates section, used as default for
templates:
default: { path: 'SonataPageBundle::layout.html.twig', name: 'default' }
Expand Down
15 changes: 15 additions & 0 deletions SonataPageBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,19 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new GlobalVariablesCompilerPass());
$container->addCompilerPass(new PageServiceCompilerPass());
}

/**
* {@inheritdoc}
*/
public function boot()
{
$container = $this->container;
$class = $this->container->getParameter('sonata.page.page.class');

call_user_func(array($class, 'setSlugifyMethod'), function($text) use ($container) {
$service = $container->get($container->getParameter('sonata.page.slugify_service'));

return $service->slugify($text);
});
}
}
4 changes: 4 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public function testPageWithMatrix()
'block' => 'Application\\Sonata\\PageBundle\\Entity\\Block',
'site' => 'Application\\Sonata\\PageBundle\\Entity\\Site',
),

'slugify_service' => 'sonata.core.slugify.native'
);

$this->assertEquals($expected, $config);
Expand Down Expand Up @@ -185,6 +187,8 @@ public function testPageWithoutMatrix()
'block' => 'Application\\Sonata\\PageBundle\\Entity\\Block',
'site' => 'Application\\Sonata\\PageBundle\\Entity\\Site',
),

'slugify_service' => 'sonata.core.slugify.native'
);

$this->assertEquals($expected, $config);
Expand Down
65 changes: 65 additions & 0 deletions Tests/SonataPageBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of sonata-project.
*
* (c) 2010 Thomas Rabaix
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\PageBundle\Tests;

use Cocur\Slugify\Slugify;
use Sonata\PageBundle\SonataPageBundle;

class Page extends \Sonata\PageBundle\Model\Page {
/**
* Returns the id
*
* @return mixed
*/
public function getId()
{
// TODO: Implement getId() method.
}
}

class SonataPageBundleTest extends \PHPUnit_Framework_TestCase
{

/**
* @dataProvider getSlug
*/
public function testBoot($text, $expected)
{
$bundle = new SonataPageBundle();
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->exactly(2))->method('getParameter')->will($this->returnCallback(function($value) {
if ($value == 'sonata.page.page.class') {
return 'Sonata\PageBundle\Tests\Page';
}

if ($value == 'sonata.page.slugify_service') {
return 'slug_service';
}
}));
$container->expects($this->once())->method('get')->will($this->returnValue(Slugify::create()));

$bundle->setContainer($container);
$bundle->boot();

$page = new Page;
$page->setSlug($text);
$this->assertEquals($page->getSlug(), $expected);
}

public function getSlug()
{
return array(
array("Salut comment ca va ?", 'salut-comment-ca-va'),
array("òüì", 'ouei')
);
}
}

0 comments on commit df3b2bc

Please sign in to comment.