From df3b2bc61d0ba5f095f7518dbfcba011e547d9bc Mon Sep 17 00:00:00 2001 From: Hugo Briand Date: Wed, 22 Oct 2014 17:13:16 +0200 Subject: [PATCH] Add support for Cocur Slugify library --- DependencyInjection/Configuration.php | 4 +- DependencyInjection/SonataPageExtension.php | 1 + Model/Page.php | 49 ++++++++++---- Resources/doc/reference/installation.rst | 3 +- SonataPageBundle.php | 15 +++++ .../DependencyInjection/ConfigurationTest.php | 4 ++ Tests/SonataPageBundleTest.php | 65 +++++++++++++++++++ 7 files changed, 126 insertions(+), 15 deletions(-) create mode 100644 Tests/SonataPageBundleTest.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4a108d1ff..ded7d2463 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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', diff --git a/DependencyInjection/SonataPageExtension.php b/DependencyInjection/SonataPageExtension.php index 6f824b49a..e3c2dcb63 100644 --- a/DependencyInjection/SonataPageExtension.php +++ b/DependencyInjection/SonataPageExtension.php @@ -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') diff --git a/Model/Page.php b/Model/Page.php index a83b4bde6..cc78faef3 100755 --- a/Model/Page.php +++ b/Model/Page.php @@ -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} */ @@ -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); } /** diff --git a/Resources/doc/reference/installation.rst b/Resources/doc/reference/installation.rst index 1319ddb3d..6acf53197 100644 --- a/Resources/doc/reference/installation.rst +++ b/Resources/doc/reference/installation.rst @@ -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' } diff --git a/SonataPageBundle.php b/SonataPageBundle.php index 378a55b2e..3b7a5438a 100644 --- a/SonataPageBundle.php +++ b/SonataPageBundle.php @@ -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); + }); + } } diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index a8e0eab61..66799f1c3 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -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); @@ -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); diff --git a/Tests/SonataPageBundleTest.php b/Tests/SonataPageBundleTest.php new file mode 100644 index 000000000..c646ded9d --- /dev/null +++ b/Tests/SonataPageBundleTest.php @@ -0,0 +1,65 @@ +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') + ); + } +}