Skip to content

Commit

Permalink
Merge pull request #404 from woutervandamme/feature/slugifier
Browse files Browse the repository at this point in the history
[AdminBundle,GeneratorBundle,NodeBundle,UtilitiesBundle] Change slugifier into service
  • Loading branch information
Roderik van der Veer committed May 26, 2015
2 parents ba02507 + 1541fce commit 92ebd88
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Kunstmaan/AdminBundle/Helper/FormWidgets/Tabs/TabPane.php
Expand Up @@ -55,6 +55,7 @@ public function __construct($identifier, Request $request, FormFactoryInterface
$this->identifier = $identifier;
$this->formFactory = $formFactory;

$this->slugifier = new Slugifier();
if ($request->request->get('currenttab')) {
$this->activeTab = $request->request->get('currenttab');
} elseif ($request->get('currenttab')) {
Expand Down Expand Up @@ -107,7 +108,7 @@ public function persist(EntityManager $em)
*/
private function generateIdentifier(TabInterface $tab)
{
return Slugifier::slugify($tab->getTitle());
return $this->slugifier->slugify($tab->getTitle());
}

/**
Expand Down Expand Up @@ -276,5 +277,4 @@ public function getExtraParams(Request $request)

return $extraParams;
}

}
Expand Up @@ -57,8 +57,9 @@ public function load(ObjectManager $manager)
$title = '{{ entity_class }}';
$translations[] = array('language' => $lang, 'callback' => function($page, $translation, $seo) use ($title) {
$translation->setTitle($title);
$translation->setSlug(Slugifier::slugify($title));
$translation->setWeight(30);
$slugifier = $this->container->get('kunstmaan_utilities.slugifier');
$translation->setSlug($slugifier->slugify($title));
});
}

Expand Down Expand Up @@ -99,8 +100,9 @@ public function load(ObjectManager $manager)

$translations[] = array('language' => $lang, 'callback' => function($page, $translation, $seo) use ($title, $i) {
$translation->setTitle($title);
$translation->setSlug(Slugifier::slugify($title));
$translation->setWeight(100 + $i);
$slugifier = $this->container->get('kunstmaan_utilities.slugifier');
$translation->setSlug($slugifier->slugify($title));
});
}

Expand Down
Expand Up @@ -58,8 +58,9 @@ public function load(ObjectManager $manager)

$translations[] = array('language' => $lang, 'callback' => function($page, $translation, $seo) use($title) {
$translation->setTitle($title);
$translation->setSlug(Slugifier::slugify($title));
$translation->setWeight(50);
$slugifier = $this->container->get('kunstmaan_utilities.slugifier');
$translation->setSlug($slugifier->slugify($title));
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Kunstmaan/NodeBundle/Entity/NodeTranslation.php
Expand Up @@ -218,7 +218,7 @@ public function getTitle()
*/
public function setSlug($slug)
{
$this->slug = Slugifier::slugify($slug, '');
$this->slug = $slug;

return $this;
}
Expand Down
Expand Up @@ -11,6 +11,7 @@

use Kunstmaan\NodeBundle\Entity\NodeVersion;
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
use Kunstmaan\UtilitiesBundle\Helper\SlugifierInterface;
use Symfony\Bridge\Monolog\Logger;
use Symfony\Component\HttpFoundation\Session\Session;

Expand All @@ -25,15 +26,21 @@ class NodeTranslationListener
private $logger;
private $nodeTranslations;

/**
* @var SlugifierInterface
*/
private $slugifier;

/**
* @param Session $session The session
* @param Logger $logger The logger
*/
public function __construct(Session $session, $logger)
public function __construct(Session $session, $logger, SlugifierInterface $slugifier)
{
$this->nodeTranslations = array();
$this->session = $session;
$this->logger = $logger;
$this->slugifier = $slugifier;
}

/**
Expand Down Expand Up @@ -200,7 +207,7 @@ private function ensureUniqueUrl(NodeTranslation &$translation, EntityManager $e

if (count($translations) > 0) {
$oldUrl = $translation->getFullSlug();
$translation->setSlug($this->IncrementString($translation->getSlug()));
$translation->setSlug($this->slugifier->slugify($this->IncrementString($translation->getSlug())));
$newUrl = $translation->getFullSlug();

$message = 'The URL of the page has been changed from ' . $oldUrl . ' to ' . $newUrl . ' since another page already uses this URL.';
Expand Down
3 changes: 2 additions & 1 deletion src/Kunstmaan/NodeBundle/Form/Type/SlugType.php
Expand Up @@ -2,6 +2,7 @@

namespace Kunstmaan\NodeBundle\Form\Type;

use Proxies\__CG__\Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Symfony\Component\Form\FormInterface;

use Symfony\Component\Form\FormView;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function getName()
public function buildView(FormView $view, FormInterface $form, array $options)
{
$nodeTranslation = $form->getParent()->getData();
$view->vars['reset'] = Slugifier::slugify($nodeTranslation->getTitle(), '');
$view->vars['reset'] = $nodeTranslation->getSlug();
$parentNode = $nodeTranslation->getNode()->getParent();
if ($parentNode !== null) {
$nodeTranslation = $parentNode->getNodeTranslation($nodeTranslation->getLang(), true);
Expand Down
Expand Up @@ -296,7 +296,7 @@ public function createNodeTranslationFor(HasNodeInterface $hasNode, $lang, Node
->setNode($node)
->setLang($lang)
->setTitle($hasNode->getTitle())
->setSlug(Slugifier::slugify(!$hasNode->isStructureNode() ? $hasNode->getTitle(): null, ''))
->setSlug(!$hasNode->isStructureNode() ? $hasNode->getTitle(): null)
->setOnline(false)
->setWeight(0);

Expand Down
2 changes: 1 addition & 1 deletion src/Kunstmaan/NodeBundle/Resources/config/services.yml
Expand Up @@ -5,7 +5,7 @@ parameters:
services:
kunstmaan_node.nodetranslation.listener:
class: Kunstmaan\NodeBundle\EventListener\NodeTranslationListener
arguments: ["@session", "@kunstmaan_admin.logger"]
arguments: ["@session", "@kunstmaan_admin.logger" , "@kunstmaan_utilities.slugifier"]
tags:
- { name: 'doctrine.event_listener', event: 'onFlush', method: 'onFlush' }
- { name: 'doctrine.event_listener', event: 'postFlush', method: 'postFlush' }
Expand Down
4 changes: 2 additions & 2 deletions src/Kunstmaan/UtilitiesBundle/Helper/Slugifier.php
Expand Up @@ -5,7 +5,7 @@
/**
* Sulgifier is a helper to slugify a certain string
*/
class Slugifier
class Slugifier implements SlugifierInterface
{
/**
* Slugify a string
Expand All @@ -15,7 +15,7 @@ class Slugifier
*
* @return string
*/
public static function slugify($text, $default = 'n-a', $replace = array("'"), $delimiter = '-')
public function slugify($text, $default = 'n-a', $replace = array("'"), $delimiter = '-')
{
if (!empty($replace)) {
$text = str_replace($replace, ' ', $text);
Expand Down
21 changes: 21 additions & 0 deletions src/Kunstmaan/UtilitiesBundle/Helper/SlugifierInterface.php
@@ -0,0 +1,21 @@
<?php

namespace Kunstmaan\UtilitiesBundle\Helper;


/**
* Interface SlugifierInterface
* @package Kunstmaan\UtilitiesBundle\Helper
*/
interface SlugifierInterface {

/**
* @param $text
* @param $default
* @param $replace
* @param $delimiter
* @return mixed
*/
public function slugify($text, $default = 'n-a', $replace = array("'"), $delimiter = '-');

}
7 changes: 7 additions & 0 deletions src/Kunstmaan/UtilitiesBundle/Resources/config/services.yml
@@ -1,4 +1,5 @@
parameters:
kunstmaan_utilities.slugifier.class: 'Kunstmaan\UtilitiesBundle\Helper\Slugifier'
kunstmaan_utilities.shell.class: 'Kunstmaan\UtilitiesBundle\Helper\Shell\Shell'
kunstmaan_utilities.cipher.class: 'Kunstmaan\UtilitiesBundle\Helper\Cipher\UrlSafeCipher'
kunstmaan_utilities.cipher.secret: ''
Expand All @@ -11,7 +12,13 @@ services:
class: "%kunstmaan_utilities.cipher.class%"
arguments: ["%kunstmaan_utilities.cipher.secret%"]

kunstmaan_utilities.slugifier:
class: "%kunstmaan_utilities.slugifier.class%"

kunstmaan_utilities.twig.extension:
class: Kunstmaan\UtilitiesBundle\Twig\UtilitiesTwigExtension
arguments: [ "@kunstmaan_utilities.slugifier" ]
tags:
- { name: twig.extension }


29 changes: 25 additions & 4 deletions src/Kunstmaan/UtilitiesBundle/Tests/Helper/SlugifierTest.php
@@ -1,28 +1,41 @@
<?php

namespace Kunstmaan\NodeBundle\Tests;

use Kunstmaan\UtilitiesBundle\Helper\Slugifier;
use Kunstmaan\UtilitiesBundle\Helper\SlugifierInterface;


/**
* SlugifierTest
*/
class SlugifierTest extends \PHPUnit_Framework_TestCase
{

/**
* @var SlugifierInterface
*/
private $slugifier;

/**
* {@inheritDoc}
*/
public function setUp()
{
$this->slugifier = new Slugifier();
}
/**
* @param string $text The text to slugify
* @param string $default The default alternative
* @param string $result The slug it should generate
*
* @dataProvider getSlugifyData
* @covers Kunstmaan\UtilitiesBundle\Helper\Slugifier::slugify
*/
public function testSlugify($text, $default, $result)
{
if (!is_null($default)) {
$this->assertEquals($result, Slugifier::slugify($text, $default));
$this->assertEquals($result, $this->slugifier->slugify($text, $default));
} else {
$this->assertEquals($result, Slugifier::slugify($text));
$this->assertEquals($result, $this->slugifier->slugify($text));
}
}

Expand All @@ -43,4 +56,12 @@ public function getSlugifyData()
array('áàäåéèëíìïóòöúùüñßæ', null, 'aaaaeeeiiiooouuunssae'),
);
}

/**
* {@inheritDoc}
*/
protected function tearDown()
{
parent::tearDown();
}
}
16 changes: 14 additions & 2 deletions src/Kunstmaan/UtilitiesBundle/Twig/UtilitiesTwigExtension.php
Expand Up @@ -2,12 +2,24 @@

namespace Kunstmaan\UtilitiesBundle\Twig;

use Kunstmaan\UtilitiesBundle\Helper\Slugifier;
use Kunstmaan\UtilitiesBundle\Helper\SlugifierInterface;
use Twig_Extension;


class UtilitiesTwigExtension extends Twig_Extension
{
/**
* @var SlugifierInterface
*/
private $slugifier;

/**
* @param $slugifier
*/
function __construct($slugifier)
{
$this->slugifier = $slugifier;
}

/**
* Returns a list of filters.
Expand All @@ -28,7 +40,7 @@ public function getFilters()
*/
public function slugify($text)
{
return Slugifier::slugify($text, '');
return $this->slugifier->slugify($text, '');
}

/**
Expand Down

0 comments on commit 92ebd88

Please sign in to comment.