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

[WIP] Add the contao.slugify service #569

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 4.3.0-beta1 (2016-XX-XX)

* Add the contao.slugify service to romanize aliases (see #569).
* Provide an image service to handle image and picture elements (see #342).
* Allow to adjust the Contao configuration by adding settings under "contao.localconfig" (see #521).
* Make script combination optional in case the website supports HTTP/2 (see #484).
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Edit your `app/config/config.yml` file and add the following:
# Contao configuration
contao:
# Required parameters
prepend_locale: true
prepend_locale: false
folder_urls: false
encryption_key: "%kernel.secret%"

# Optional parameters
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"league/uri": "~4.0",
"matthiasmullie/minify": "~1.3",
"michelf/php-markdown": "~1.4",
"cocur/slugify": "~2.3",
"oyejorge/less.php": "~1.7",
"patchwork/utf8": "~1.2",
"phpspec/php-diff": "~1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/ContaoCoreBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Contao\CoreBundle\DependencyInjection\Compiler\AddPackagesPass;
use Contao\CoreBundle\DependencyInjection\Compiler\AddResourcesPathsPass;
use Contao\CoreBundle\DependencyInjection\Compiler\AddSessionBagsPass;
use Contao\CoreBundle\DependencyInjection\Compiler\AddSlugifyRegexpPass;
use Contao\CoreBundle\DependencyInjection\ContaoCoreExtension;
use Patchwork\Utf8\Bootup;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -60,5 +61,6 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new AddSessionBagsPass());
$container->addCompilerPass(new AddResourcesPathsPass());
$container->addCompilerPass(new AddImagineClassPass());
$container->addCompilerPass(new AddSlugifyRegexpPass());
}
}
39 changes: 39 additions & 0 deletions src/DependencyInjection/Compiler/AddSlugifyRegexpPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of Contao.
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @license LGPL-3.0+
*/

namespace Contao\CoreBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Adds the Slugify regexp depending on the folder_urls setting.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class AddSlugifyRegexpPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (false === $container->getParameter('contao.folder_urls')) {
return;
}

$definition = $container->findDefinition('cocur_slugify');

$arguments = $definition->getArguments();
$arguments[0]['regexp'] = '#([^A-Za-z0-9/.]|-)+#';

$definition->setArguments($arguments);
}
}
3 changes: 3 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function getConfigTreeBuilder()
->booleanNode('prepend_locale')
->defaultFalse()
->end()
->booleanNode('folder_urls')
->defaultFalse()
->end()
->scalarNode('encryption_key')
->isRequired()
->cannotBeEmpty()
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/ContaoCoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
}

$container->setParameter('contao.prepend_locale', $mergedConfig['prepend_locale']);
$container->setParameter('contao.folder_urls', $mergedConfig['folder_urls']);
$container->setParameter('contao.encryption_key', $mergedConfig['encryption_key']);
$container->setParameter('contao.url_suffix', $mergedConfig['url_suffix']);
$container->setParameter('contao.upload_path', $mergedConfig['upload_path']);
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,8 @@ services:
- "_contao_fe_attributes"
calls:
- ["setName", ["contao_frontend"]]

contao.slugify:
class: Contao\CoreBundle\Slugify\Slugify
arguments:
- "@cocur_slugify"
1 change: 1 addition & 0 deletions src/Resources/contao/library/Contao/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ protected static function loadParameters()
'smtpPort' => 'mailer_port',
'smtpEnc' => 'mailer_encryption',
'addLanguageToUrl' => 'contao.prepend_locale',
'folderUrl' => 'contao.folder_urls',
'encryptionKey' => 'contao.encryption_key',
'urlSuffix' => 'contao.url_suffix',
'uploadPath' => 'contao.upload_path',
Expand Down
126 changes: 126 additions & 0 deletions src/Slugify/Slugify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

/*
* This file is part of Contao.
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @license LGPL-3.0+
*/

namespace Contao\CoreBundle\Slugify;

use Cocur\Slugify\Slugify as CocurSlugify;

/**
* Provides the contao.slugify service.
*
* @author Yanick Witschi <https://github.com/Toflar>
* @author Leo Feyer <https://github.com/leofeyer>
*/
class Slugify implements SlugifyInterface
{
/**
* @var CocurSlugify
*/
private $slugify;

/**
* @var array
*/
private $rulesets = [
'default',
'azerbaijani',
'burmese',
'hindi',
'georgian',
'norwegian',
'vietnamese',
'ukrainian',
'latvian',
'finnish',
'greek',
'czech',
'arabic',
'turkish',
'polish',
'german',
'russian',
];

/**
* @var array
*/
private $mapper = [
'ar' => 'arabic',
'de_AT' => 'austrian',
'az' => 'azerbaijani',
'bg' => 'bulgarian',
'my' => 'burmese',
'hr' => 'croatian',
'cs' => 'czech',
'eo' => 'esperanto',
'fi' => 'finnish',
'ka' => 'georgian',
'de' => 'german',
'el' => 'greek',
'hi' => 'hindi',
'lv' => 'latvian',
'no' => 'norwegian',
'pl' => 'polish',
'ru' => 'russian',
'sv' => 'swedish',
'tr' => 'turkish',
'uk' => 'ukrainian',
'vi' => 'vietnamese',
];

/**
* Constructor.
*
* @param CocurSlugify $slugify
*/
public function __construct(CocurSlugify $slugify)
{
$this->slugify = $slugify;
}

/**
* {@inheritdoc}
*/
public function slugify($string, $language = null)
{
if (null === $language) {
foreach ($this->rulesets as $ruleset) {
$this->slugify->activateRuleSet($ruleset);
}
} else {
$ruleset = $this->getRulesetForLanguage($language);

if (null !== $ruleset) {
$this->slugify->activateRuleSet($ruleset);
}
}

return $this->slugify->slugify($string);
}

/**
* {@inheritdoc}
*/
public function getRulesetForLanguage($language)
{
if (null === $language) {
return null;
}

if (isset($this->mapper[$language])) {
return $this->mapper[$language];
}

// Shorten e.g. de_DE to de
$lang = explode('_', $language)[0];

return isset($this->mapper[$lang]) ? $this->mapper[$lang] : null;
}
}
39 changes: 39 additions & 0 deletions src/Slugify/SlugifyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of Contao.
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @license LGPL-3.0+
*/

namespace Contao\CoreBundle\Slugify;

/**
* Returns an URL safe version of a string.
*
* @author Yanick Witschi <https://github.com/Toflar>
* @author Leo Feyer <https://github.com/leofeyer>
*/
interface SlugifyInterface
{
/**
* Returns an URL safe version of a string.
*
* @param string $string
* @param string|null $language
*
* @return string
*/
public function slugify($string, $language = null);

/**
* Returns the Slugify ruleset for a language.
*
* @param string $language
*
* @return string
*/
public function getRulesetForLanguage($language);
}
60 changes: 60 additions & 0 deletions tests/Slugify/SlugifyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of Contao.
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @license LGPL-3.0+
*/

namespace Contao\CoreBundle\Test\Slugify;

use Cocur\Slugify\Slugify as CocurSlugify;
use Contao\CoreBundle\Slugify\Slugify;

/**
* Tests the Slugify class.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class SlugifyTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests the object instantiation.
*/
public function testInstantiation()
{
$slugify = new Slugify(new CocurSlugify());

$this->assertInstanceOf('Contao\CoreBundle\Slugify\Slugify', $slugify);
}

/**
* Tests the slugify() method.
*/
public function testSlugify()
{
$slugify = new Slugify(new CocurSlugify());

$this->assertEquals('fuer', $slugify->slugify('für', 'de'));
$this->assertEquals('fur', $slugify->slugify('für', 'tr'));
$this->assertEquals('aepfel-und-birnen', $slugify->slugify('Äpfel und Birnen', 'de'));
$this->assertEquals('groesse', $slugify->slugify('Größe', 'de'));
$this->assertEquals('groesze', $slugify->slugify('Größe', 'de_AT'));
}

/**
* Tests the getRulesetForLanguage() method.
*/
public function getRulesetForLanguage()
{
$slugify = new Slugify(new CocurSlugify());

$this->assertEquals('german', $slugify->getRulesetForLanguage('de'));
$this->assertEquals('german', $slugify->getRulesetForLanguage('de_DE'));
$this->assertEquals('german', $slugify->getRulesetForLanguage('de_CH'));
$this->assertEquals('austrian', $slugify->getRulesetForLanguage('de_AT'));
$this->assertEquals('polish', $slugify->getRulesetForLanguage('pl'));
}
}