Skip to content

Commit 056d8ce

Browse files
committed
[String] Introduce a locale-aware Slugger in the String component with FrameworkBundle wiring
1 parent 4a547c5 commit 056d8ce

File tree

13 files changed

+324
-12
lines changed

13 files changed

+324
-12
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CHANGELOG
1919
* Removed `SecurityUserValueResolver`, use `UserValueResolver` instead
2020
* Removed `routing.loader.service`.
2121
* Service route loaders must be tagged with `routing.route_loader`.
22+
* Added `slugger` service and `SluggerInterface` alias
2223

2324
4.4.0
2425
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
106106
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
107107
use Symfony\Component\Stopwatch\Stopwatch;
108+
use Symfony\Component\String\Slugger\SluggerInterface;
108109
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
109110
use Symfony\Component\Translation\Translator;
110111
use Symfony\Component\Validator\ConstraintValidatorInterface;
@@ -194,6 +195,12 @@ public function load(array $configs, ContainerBuilder $container)
194195
}
195196
}
196197

198+
// If the slugger is used but the String component is not available, we should throw an error
199+
if (!class_exists(SluggerInterface::class)) {
200+
$container->register('slugger', 'stdClass')
201+
->addError('You cannot use the "slugger" since the String component is not installed. Try running "composer require symfony/string".');
202+
}
203+
197204
if (isset($config['secret'])) {
198205
$container->setParameter('kernel.secret', $config['secret']);
199206
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,11 @@
120120
<argument type="service" id="request_stack" />
121121
<tag name="kernel.event_subscriber" />
122122
</service>
123+
124+
<service id="slugger" class="Symfony\Component\String\Slugger\AsciiSlugger">
125+
<argument>%kernel.default_locale%</argument>
126+
<tag name="kernel.locale_aware" />
127+
</service>
128+
<service id="Symfony\Component\String\Slugger\SluggerInterface" alias="slugger" />
123129
</services>
124130
</container>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Slugger;
13+
14+
use Symfony\Component\String\Slugger\SluggerInterface;
15+
16+
class SlugConstructArgService
17+
{
18+
private $slugger;
19+
20+
public function __construct(SluggerInterface $slugger)
21+
{
22+
$this->slugger = $slugger;
23+
}
24+
25+
public function hello(): string
26+
{
27+
return $this->slugger->slug('Стойността трябва да бъде лъжа');
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
/**
15+
* @group functional
16+
*/
17+
class SluggerLocaleAwareTest extends AbstractWebTestCase
18+
{
19+
public function testLocalizedSlugger()
20+
{
21+
$kernel = static::createKernel(['test_case' => 'Slugger', 'root_config' => 'config.yml']);
22+
$kernel->boot();
23+
24+
$service = $kernel->getContainer()->get('Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Slugger\SlugConstructArgService');
25+
26+
$this->assertSame('Stoinostta-tryabva-da-bude-luzha', $service->hello());
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
14+
15+
return [
16+
new FrameworkBundle(),
17+
new TestBundle(),
18+
];
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
- { resource: services.yml }
4+
5+
framework:
6+
secret: '%secret%'
7+
default_locale: '%env(LOCALE)%'
8+
translator:
9+
fallbacks:
10+
- '%env(LOCALE)%'
11+
12+
parameters:
13+
env(LOCALE): bg
14+
secret: test
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
_defaults:
3+
public: true
4+
5+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Slugger\SlugConstructArgService:
6+
arguments: ['@slugger']

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"symfony/security-http": "^4.4|^5.0",
5151
"symfony/serializer": "^4.4|^5.0",
5252
"symfony/stopwatch": "^4.4|^5.0",
53+
"symfony/string": "^5.0",
5354
"symfony/translation": "^5.0",
5455
"symfony/twig-bundle": "^4.4|^5.0",
5556
"symfony/validator": "^4.4|^5.0",

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,6 @@ public function replaceMatches(string $fromPattern, $to): parent
331331
return $str;
332332
}
333333

334-
/**
335-
* @return static
336-
*/
337-
public function slug(string $separator = '-'): self
338-
{
339-
return $this
340-
->ascii()
341-
->replace('@', $separator.'at'.$separator)
342-
->replaceMatches('/[^A-Za-z0-9]++/', $separator)
343-
->trim($separator);
344-
}
345-
346334
public function snake(): parent
347335
{
348336
$str = $this->camel()->title();

0 commit comments

Comments
 (0)