Skip to content

Commit 5e0b275

Browse files
committed
feature #1055 Use the String component (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #1055). Discussion ---------- Use the String component Commits ------- e39f440 Use the String component
2 parents 1120209 + e39f440 commit 5e0b275

File tree

16 files changed

+244
-109
lines changed

16 files changed

+244
-109
lines changed

.travis.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ cache:
1010
env:
1111
global:
1212
- SYMFONY_PHPUNIT_DIR=./bin/.phpunit
13-
- SYMFONY_DEPRECATIONS_HELPER=0
13+
- SYMFONY_DEPRECATIONS_HELPER=9
1414
- ACTION="install"
1515

1616
matrix:
@@ -20,10 +20,7 @@ matrix:
2020
- php: 7.3
2121
env: SYMFONY="5.0.*"
2222
ACTION="update"
23-
# 'php: nightly' is PHP 8.0
24-
- php: 7.4snapshot
25-
allow_failures:
26-
- php: 7.4snapshot
23+
- php: 7.4
2724

2825
before_install:
2926
- '[[ "$TRAVIS_PHP_VERSION" == "7.4snapshot" ]] || phpenv config-rm xdebug.ini'
@@ -44,7 +41,7 @@ install:
4441
script:
4542
- ./bin/phpunit
4643
# this checks that the source code follows the Symfony Code Syntax rules
47-
- '[[ "$TRAVIS_PHP_VERSION" == "7.4snapshot" ]] || ./vendor/bin/php-cs-fixer fix --diff --dry-run -v'
44+
- '[[ "$TRAVIS_PHP_VERSION" == "7.4" ]] || ./vendor/bin/php-cs-fixer fix --diff --dry-run -v'
4845
# this checks that the YAML config files contain no syntax errors
4946
- ./bin/console lint:yaml config --parse-tags
5047
# this checks that the Twig template files contain no syntax errors

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"symfony/polyfill-intl-messageformatter": "^1.12",
2525
"symfony/polyfill-php72": "^1.8",
2626
"symfony/security-bundle": "5.0.*",
27+
"symfony/string": "5.0.*",
2728
"symfony/swiftmailer-bundle": "^3.1",
2829
"symfony/translation": "5.0.*",
2930
"symfony/twig-bundle": "5.0.*",

composer.lock

Lines changed: 188 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Command/AddUserCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Console\Style\SymfonyStyle;
2525
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
2626
use Symfony\Component\Stopwatch\Stopwatch;
27+
use function Symfony\Component\String\u;
2728

2829
/**
2930
* A console command that creates users and stores them in the database.
@@ -138,7 +139,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
138139
// Ask for the password if it's not defined
139140
$password = $input->getArgument('password');
140141
if (null !== $password) {
141-
$this->io->text(' > <info>Password</info>: '.str_repeat('*', mb_strlen($password)));
142+
$this->io->text(' > <info>Password</info>: '.u('*')->repeat(u($password)->length()));
142143
} else {
143144
$password = $this->io->askHidden('Password (your type will be hidden)', [$this->validator, 'validatePassword']);
144145
$input->setArgument('password', $password);

src/Controller/Admin/BlogController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
use App\Form\PostType;
1616
use App\Repository\PostRepository;
1717
use App\Security\PostVoter;
18-
use App\Utils\Slugger;
1918
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
2019
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2120
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
2221
use Symfony\Component\HttpFoundation\Request;
2322
use Symfony\Component\HttpFoundation\Response;
2423
use Symfony\Component\Routing\Annotation\Route;
24+
use Symfony\Component\String\Slugger\SluggerInterface;
2525

2626
/**
2727
* Controller used to manage blog contents in the backend.
@@ -70,7 +70,7 @@ public function index(PostRepository $posts): Response
7070
* to constraint the HTTP methods each controller responds to (by default
7171
* it responds to all methods).
7272
*/
73-
public function new(Request $request): Response
73+
public function new(Request $request, SluggerInterface $slugger): Response
7474
{
7575
$post = new Post();
7676
$post->setAuthor($this->getUser());
@@ -86,7 +86,7 @@ public function new(Request $request): Response
8686
// However, we explicitly add it to improve code readability.
8787
// See https://symfony.com/doc/current/best_practices/forms.html#handling-form-submits
8888
if ($form->isSubmitted() && $form->isValid()) {
89-
$post->setSlug(Slugger::slugify($post->getTitle()));
89+
$post->setSlug($slugger->slug($post->getTitle())->lower());
9090

9191
$em = $this->getDoctrine()->getManager();
9292
$em->persist($post);
@@ -133,13 +133,13 @@ public function show(Post $post): Response
133133
* @Route("/{id<\d+>}/edit",methods={"GET", "POST"}, name="admin_post_edit")
134134
* @IsGranted("edit", subject="post", message="Posts can only be edited by their authors.")
135135
*/
136-
public function edit(Request $request, Post $post): Response
136+
public function edit(Request $request, Post $post, SluggerInterface $slugger): Response
137137
{
138138
$form = $this->createForm(PostType::class, $post);
139139
$form->handleRequest($request);
140140

141141
if ($form->isSubmitted() && $form->isValid()) {
142-
$post->setSlug(Slugger::slugify($post->getTitle()));
142+
$post->setSlug($slugger->slug($post->getTitle())->lower());
143143
$this->getDoctrine()->getManager()->flush();
144144

145145
$this->addFlash('success', 'post.updated_successfully');

src/DataFixtures/AppFixtures.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@
1515
use App\Entity\Post;
1616
use App\Entity\Tag;
1717
use App\Entity\User;
18-
use App\Utils\Slugger;
1918
use Doctrine\Bundle\FixturesBundle\Fixture;
2019
use Doctrine\Common\Persistence\ObjectManager;
2120
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
21+
use Symfony\Component\String\Slugger\SluggerInterface;
22+
use function Symfony\Component\String\u;
2223

2324
class AppFixtures extends Fixture
2425
{
2526
private $passwordEncoder;
27+
private $slugger;
2628

27-
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
29+
public function __construct(UserPasswordEncoderInterface $passwordEncoder, SluggerInterface $slugger)
2830
{
2931
$this->passwordEncoder = $passwordEncoder;
32+
$this->slugger = $slugger;
3033
}
3134

3235
public function load(ObjectManager $manager): void
@@ -125,7 +128,7 @@ private function getPostData()
125128
// $postData = [$title, $slug, $summary, $content, $publishedAt, $author, $tags, $comments];
126129
$posts[] = [
127130
$title,
128-
Slugger::slugify($title),
131+
$this->slugger->slug($title)->lower(),
129132
$this->getRandomText(),
130133
$this->getPostContent(),
131134
new \DateTime('now - '.$i.'days'),
@@ -179,9 +182,10 @@ private function getRandomText(int $maxLength = 255): string
179182
$phrases = $this->getPhrases();
180183
shuffle($phrases);
181184

182-
while (mb_strlen($text = implode('. ', $phrases).'.') > $maxLength) {
185+
do {
186+
$text = u('. ')->join($phrases)->append('.');
183187
array_pop($phrases);
184-
}
188+
} while ($text->length() > $maxLength);
185189

186190
return $text;
187191
}

src/Entity/Comment.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace App\Entity;
1313

1414
use Doctrine\ORM\Mapping as ORM;
15+
use function Symfony\Component\String\u;
1516
use Symfony\Component\Validator\Constraints as Assert;
1617

1718
/**
@@ -85,7 +86,7 @@ public function __construct()
8586
*/
8687
public function isLegitComment(): bool
8788
{
88-
$containsInvalidCharacters = false !== mb_strpos($this->content, '@');
89+
$containsInvalidCharacters = null !== u($this->content)->indexOf('@');
8990

9091
return !$containsInvalidCharacters;
9192
}

0 commit comments

Comments
 (0)