Skip to content

Commit

Permalink
Remove usage of slugify method
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Jul 31, 2022
1 parent 5375886 commit 6e4be34
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 77 deletions.
1 change: 0 additions & 1 deletion docs/reference/advanced_configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Full configuration options:
# Defaults:
- /(.*)admin(.*)/
- /^_(.*)/
slugify_service: sonata.page.slugify.cocur
ignore_uri_patterns:
# Default:
- /admin(.*)/
Expand Down
1 change: 0 additions & 1 deletion docs/reference/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ SonataPageBundle Configuration
# config/packages/sonata_page.yaml
sonata_page:
slugify_service: sonata.page.slugify.cocur
multisite: host
use_streamed_response: true # set the value to false in debug mode or if the reverse proxy does not handle streamed response
ignore_route_patterns:
Expand Down
3 changes: 0 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ public function getConfigTreeBuilder(): TreeBuilder
])
->prototype('scalar')->end()
->end()
->scalarNode('slugify_service')
->defaultValue('sonata.page.slugify.cocur')
->end()
->arrayNode('ignore_routes')
->defaultValue([])
->prototype('scalar')->end()
Expand Down
5 changes: 2 additions & 3 deletions src/DependencyInjection/SonataPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public function load(array $configs, ContainerBuilder $container): void
$this->configurePageServices($container, $config);

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

$container->setParameter('sonata.page.skip_redirection', $config['skip_redirection']);
$container->setParameter('sonata.page.hide_disabled_blocks', $config['hide_disabled_blocks']);
Expand Down Expand Up @@ -121,14 +120,14 @@ public function configurePageDefaults(ContainerBuilder $container, array $config
];

$container->getDefinition('sonata.page.manager.page')
->replaceArgument(2, $defaults);
->replaceArgument(3, $defaults);

foreach ($config['page_defaults'] as $name => $pageDefaults) {
$config['page_defaults'][$name] = array_merge($defaults, $pageDefaults);
}

$container->getDefinition('sonata.page.manager.page')
->replaceArgument(3, $config['page_defaults']);
->replaceArgument(4, $config['page_defaults']);
}

/**
Expand Down
27 changes: 20 additions & 7 deletions src/Entity/PageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,46 @@

namespace Sonata\PageBundle\Entity;

use Cocur\Slugify\SlugifyInterface;
use Doctrine\Persistence\ManagerRegistry;
use Sonata\Doctrine\Entity\BaseEntityManager;
use Sonata\PageBundle\Model\Page;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Model\PageManagerInterface;
use Sonata\PageBundle\Model\SiteInterface;

/**
* This class manages PageInterface persistency with the Doctrine ORM.
*
* @extends BaseEntityManager<PageInterface>
*
* @author Thomas Rabaix <[email protected]>
*/
final class PageManager extends BaseEntityManager implements PageManagerInterface
{
private SlugifyInterface $slugify;

/**
* @var array<string, mixed>
*/
private array $pageDefaults;

/**
* @var array<string, mixed>
*/
private array $defaults;

/**
* @param string $class
* @param array<string, mixed> $defaults
* @param array<string, mixed> $pageDefaults
*/
public function __construct($class, ManagerRegistry $registry, array $defaults = [], array $pageDefaults = [])
{
public function __construct(
string $class,
ManagerRegistry $registry,
SlugifyInterface $slugify,
array $defaults = [],
array $pageDefaults = []
) {
parent::__construct($class, $registry);

$this->slugify = $slugify;
$this->defaults = $defaults;
$this->pageDefaults = $pageDefaults;
}
Expand Down Expand Up @@ -86,7 +99,7 @@ public function fixUrl(PageInterface $page): void
// make sure Page has a valid url
if ($page->getParent()) {
if (!$page->getSlug()) {
$page->setSlug(Page::slugify($page->getName()));
$page->setSlug($this->slugify->slugify($page->getName()));
}

if ('/' === $page->getParent()->getUrl()) {
Expand Down
38 changes: 1 addition & 37 deletions src/Model/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function getName()

public function setSlug($slug): void
{
$this->slug = self::slugify(trim((string) $slug));
$this->slug = $slug;
}

public function getSlug()
Expand Down Expand Up @@ -531,42 +531,6 @@ public function getUrl()
return $this->url;
}

/**
* source : http://snipplr.com/view/22741/slugify-a-string-in-php/.
*
* @static
*
* @param string $text
*
* @return mixed|string
*/
public static function slugify($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, '-');

// transliterate
if (\function_exists('iconv')) {
$text = iconv('UTF-8', 'ASCII//TRANSLIT', $text);
}

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

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

return $text;
}

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

/**
* Retrieve a block by code.
*
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<service id="sonata.page.manager.page" class="%sonata.page.manager.page.class%" public="true">
<argument>%sonata.page.page.class%</argument>
<argument type="service" id="doctrine"/>
<argument type="service" id="sonata.page.slugify.cocur"/>
<argument/>
<argument/>
</service>
Expand Down
1 change: 0 additions & 1 deletion tests/App/config/sonata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ sonata_block:
default_contexts: [sonata_page_bundle]

sonata_page:
slugify_service: sonata.page.slugify.cocur
multisite: host
use_streamed_response: false

Expand Down
2 changes: 0 additions & 2 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public function testPageWithMatrix(): void
0 => '(.*)admin(.*)',
1 => '^_(.*)',
],
'slugify_service' => 'sonata.page.slugify.cocur',
'ignore_routes' => [
],
'ignore_uri_patterns' => [
Expand Down Expand Up @@ -185,7 +184,6 @@ public function testPageWithoutMatrix(): void
0 => '(.*)admin(.*)',
1 => '^_(.*)',
],
'slugify_service' => 'sonata.page.slugify.cocur',
'ignore_routes' => [
],
'ignore_uri_patterns' => [
Expand Down
12 changes: 7 additions & 5 deletions tests/Entity/PageManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\PageBundle\Tests\Entity;

use Cocur\Slugify\Slugify;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\TestCase;
use Sonata\PageBundle\Entity\PageManager;
Expand All @@ -23,9 +24,9 @@ final class PageManagerTest extends TestCase
public function testFixUrl(): void
{
$manager = new PageManager(
'Foo\Bar',
$this->createMock(ManagerRegistry::class),
[]
Page::class,
$this->createStub(ManagerRegistry::class),
new Slugify()
);

$page1 = new Page();
Expand Down Expand Up @@ -67,9 +68,9 @@ public function testFixUrl(): void
public function testWithSlashAtTheEnd(): void
{
$manager = new PageManager(
'Foo\Bar',
Page::class,
$this->createMock(ManagerRegistry::class),
[]
new Slugify()
);

$homepage = new Page();
Expand All @@ -96,6 +97,7 @@ public function testCreateWithGlobalDefaults(): void
$manager = new PageManager(
Page::class,
$this->createMock(ManagerRegistry::class),
new Slugify(),
[],
['my_route' => ['decorate' => false, 'name' => 'Salut!']]
);
Expand Down
17 changes: 0 additions & 17 deletions tests/Model/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,6 @@

final class PageTest extends TestCase
{
public function testSlugify(): void
{
setlocale(\LC_ALL, 'en_US.utf8');

$reflectionClass = new \ReflectionClass(Page::class);
$property = $reflectionClass->getProperty('slugifyMethod');
$property->setAccessible(true);
$property->setValue(null);

static::assertSame(Page::slugify('test'), 'test');
static::assertSame(Page::slugify('S§!@@#$#$alut'), 's-alut');
static::assertSame(Page::slugify('Symfony2'), 'symfony2');
static::assertSame(Page::slugify('test'), 'test');
static::assertSame(Page::slugify('c\'est bientôt l\'été'), 'c-est-bientot-l-ete');
static::assertSame(Page::slugify(urldecode('%2Fc\'est+bientôt+l\'été')), 'c-est-bientot-l-ete');
}

public function testHeader(): void
{
$expectedHeaders = [
Expand Down

0 comments on commit 6e4be34

Please sign in to comment.