Replace usage of behat/transliterator to generate the slug
Feature Request
The Urlizer class holding the default implementations of the slug generation callables currently delegates all its implementation to behat/transliterator.
The behat/transliterator will be marked as abandoned (see https://github.com/Behat/Transliterator/pull/41). The implementation should be replaced to implement Urlizer::transliterate and Urlizer::urlize without that dependency.
One option is to use the AsciiSlugger of symfony/string (this one works only for UTF-8 text as input AFAICT).
So switching over isn't too bad it seems like. On a quick test I wired up this patch:
diff --git a/src/Sluggable/SluggableListener.php b/src/Sluggable/SluggableListener.php
index 48580450..6716641c 100644
--- a/src/Sluggable/SluggableListener.php
+++ b/src/Sluggable/SluggableListener.php
@@ -21,6 +21,7 @@ use Gedmo\Sluggable\Handler\SlugHandlerInterface;
use Gedmo\Sluggable\Handler\SlugHandlerWithUniqueCallbackInterface;
use Gedmo\Sluggable\Mapping\Event\SluggableAdapter;
use Gedmo\Sluggable\Util\Urlizer;
+use Symfony\Component\String\Slugger\AsciiSlugger;
/**
* The SluggableListener handles the generation of slugs
@@ -121,6 +122,18 @@ class SluggableListener extends MappedEventSubscriber
*/
private array $managedFilters = [];
+ public function __construct()
+ {
+ parent::__construct();
+
+ $this->transliterator = static fn (string $text, string $separator, object $object): string => $text;
+ $this->urlizer = static fn (string $text, string $separator, object $object): string => (new AsciiSlugger())
+ ->slug($text, $separator)
+ ->lower()
+ ->toString()
+ ;
+ }
+
/**
* Specifies the list of events to listen
*
And there are only these test failures (it's a little worse without the ->lower() call):
1) Gedmo\Tests\Sluggable\Handlers\BothSlugHandlerTest::testSlugGeneration
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'web/developer/php/herzult'
+'web/developer/php/Herzult'
/tests/Gedmo/Sluggable/Handlers/BothSlugHandlerTest.php:45
2) Gedmo\Tests\Sluggable\Handlers\BothSlugHandlerTest::testSlugUpdates
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'web/developer/upd-gedi'
+'web/developer/Upd Gedi'
/tests/Gedmo/Sluggable/Handlers/BothSlugHandlerTest.php:64
3) Gedmo\Tests\Sluggable\Handlers\BothSlugHandlerTest::test1093
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'web/developer/php/herzult'
+'web/developer/php/Herzult'
/tests/Gedmo/Sluggable/Handlers/BothSlugHandlerTest.php:89
4) Gedmo\Tests\Sluggable\Handlers\RelativeSlugHandlerDocumentTest::testSlugGeneration
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'sport-test/thomas'
+'sport-test/Thomas'
/tests/Gedmo/Sluggable/Handlers/RelativeSlugHandlerDocumentTest.php:42
5) Gedmo\Tests\Sluggable\Handlers\RelativeSlugHandlerDocumentTest::testUpdateOperations
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'sport-test/ninja'
+'sport-test/Ninja'
/tests/Gedmo/Sluggable/Handlers/RelativeSlugHandlerDocumentTest.php:64
6) Gedmo\Tests\Sluggable\Handlers\RelativeSlugHandlerTest::testSlugGeneration
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'sport-test/thomas'
+'sport-test/Thomas'
/tests/Gedmo/Sluggable/Handlers/RelativeSlugHandlerTest.php:43
7) Gedmo\Tests\Sluggable\Handlers\RelativeSlugHandlerTest::testUpdateOperations
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'sport-test/ninja'
+'sport-test/Ninja'
/tests/Gedmo/Sluggable/Handlers/RelativeSlugHandlerTest.php:65
8) Gedmo\Tests\Sluggable\Handlers\UserRelativeSlugHandlerTest::testRelativeSlug
relative slug is invalid
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'knplabs/gedi'
+'knplabs/Gedi'
/tests/Gedmo/Sluggable/Handlers/UserRelativeSlugHandlerTest.php:50
9) Gedmo\Tests\Sluggable\TransliterationTest::testInsertedNewSlug
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'eto-testovyi-zagolovok-ru'
+'eto-testovyj-zagolovok-ru'
/tests/Gedmo/Sluggable/TransliterationTest.php:48
This area isn't my specialty so I don't know if separating transliteration and urlization is still needed as is the case now with the Behat package or how it'd change the API surface for the sluggable extension but it wouldn't be the worst refactoring it seems like.
Will be fixed by https://github.com/doctrine-extensions/DoctrineExtensions/pull/2944
Any news on this one?
+1. ISO requirements do not like to have abandoned projects in code.
Hi! Just checking in to see if there’s an expected timeline for when the update will be published? It would be great to know when we can expect the fix to be available. Thanks for the work you're doing on this!