Skip to content

Commit

Permalink
Use the String component
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Dec 20, 2019
1 parent 1120209 commit e39f440
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 109 deletions.
9 changes: 3 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cache:
env:
global:
- SYMFONY_PHPUNIT_DIR=./bin/.phpunit
- SYMFONY_DEPRECATIONS_HELPER=0
- SYMFONY_DEPRECATIONS_HELPER=9
- ACTION="install"

matrix:
Expand All @@ -20,10 +20,7 @@ matrix:
- php: 7.3
env: SYMFONY="5.0.*"
ACTION="update"
# 'php: nightly' is PHP 8.0
- php: 7.4snapshot
allow_failures:
- php: 7.4snapshot
- php: 7.4

before_install:
- '[[ "$TRAVIS_PHP_VERSION" == "7.4snapshot" ]] || phpenv config-rm xdebug.ini'
Expand All @@ -44,7 +41,7 @@ install:
script:
- ./bin/phpunit
# this checks that the source code follows the Symfony Code Syntax rules
- '[[ "$TRAVIS_PHP_VERSION" == "7.4snapshot" ]] || ./vendor/bin/php-cs-fixer fix --diff --dry-run -v'
- '[[ "$TRAVIS_PHP_VERSION" == "7.4" ]] || ./vendor/bin/php-cs-fixer fix --diff --dry-run -v'
# this checks that the YAML config files contain no syntax errors
- ./bin/console lint:yaml config --parse-tags
# this checks that the Twig template files contain no syntax errors
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"symfony/polyfill-intl-messageformatter": "^1.12",
"symfony/polyfill-php72": "^1.8",
"symfony/security-bundle": "5.0.*",
"symfony/string": "5.0.*",
"symfony/swiftmailer-bundle": "^3.1",
"symfony/translation": "5.0.*",
"symfony/twig-bundle": "5.0.*",
Expand Down
189 changes: 188 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/Command/AddUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use function Symfony\Component\String\u;

/**
* A console command that creates users and stores them in the database.
Expand Down Expand Up @@ -138,7 +139,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
// Ask for the password if it's not defined
$password = $input->getArgument('password');
if (null !== $password) {
$this->io->text(' > <info>Password</info>: '.str_repeat('*', mb_strlen($password)));
$this->io->text(' > <info>Password</info>: '.u('*')->repeat(u($password)->length()));
} else {
$password = $this->io->askHidden('Password (your type will be hidden)', [$this->validator, 'validatePassword']);
$input->setArgument('password', $password);
Expand Down
10 changes: 5 additions & 5 deletions src/Controller/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
use App\Form\PostType;
use App\Repository\PostRepository;
use App\Security\PostVoter;
use App\Utils\Slugger;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\String\Slugger\SluggerInterface;

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

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

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

$this->addFlash('success', 'post.updated_successfully');
Expand Down
14 changes: 9 additions & 5 deletions src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@
use App\Entity\Post;
use App\Entity\Tag;
use App\Entity\User;
use App\Utils\Slugger;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\String\Slugger\SluggerInterface;
use function Symfony\Component\String\u;

class AppFixtures extends Fixture
{
private $passwordEncoder;
private $slugger;

public function __construct(UserPasswordEncoderInterface $passwordEncoder)
public function __construct(UserPasswordEncoderInterface $passwordEncoder, SluggerInterface $slugger)
{
$this->passwordEncoder = $passwordEncoder;
$this->slugger = $slugger;
}

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

while (mb_strlen($text = implode('. ', $phrases).'.') > $maxLength) {
do {
$text = u('. ')->join($phrases)->append('.');
array_pop($phrases);
}
} while ($text->length() > $maxLength);

return $text;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use function Symfony\Component\String\u;
use Symfony\Component\Validator\Constraints as Assert;

/**
Expand Down Expand Up @@ -85,7 +86,7 @@ public function __construct()
*/
public function isLegitComment(): bool
{
$containsInvalidCharacters = false !== mb_strpos($this->content, '@');
$containsInvalidCharacters = null !== u($this->content)->indexOf('@');

return !$containsInvalidCharacters;
}
Expand Down
Loading

0 comments on commit e39f440

Please sign in to comment.