Browse Source

ajout des slugs + commande pour initialiser avant suite

Sangfroid 2 months ago
parent
commit
503b10207b

+ 31 - 0
migrations/Version20241025153356.php

@@ -0,0 +1,31 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DoctrineMigrations;
+
+use Doctrine\DBAL\Schema\Schema;
+use Doctrine\Migrations\AbstractMigration;
+
+/**
+ * Auto-generated Migration: Please modify to your needs!
+ */
+final class Version20241025153356 extends AbstractMigration
+{
+    public function getDescription(): string
+    {
+        return '';
+    }
+
+    public function up(Schema $schema): void
+    {
+        // this up() migration is auto-generated, please modify it to your needs
+        $this->addSql('ALTER TABLE article ADD slug VARCHAR(100) NOT NULL');
+    }
+
+    public function down(Schema $schema): void
+    {
+        // this down() migration is auto-generated, please modify it to your needs
+        $this->addSql('ALTER TABLE article DROP slug');
+    }
+}

+ 42 - 0
src/Command/BlogSlugInitCommand.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace App\Command;
+
+use App\Repository\ArticleRepository;
+use App\Service\SluggerService;
+use Symfony\Component\Console\Attribute\AsCommand;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+
+#[AsCommand(
+    name: 'blog:slug:init',
+    description: 'Add a short description for your command',
+)]
+class BlogSlugInitCommand extends Command
+{
+    public function __construct(
+        protected readonly SluggerService $sluggerService,
+        protected readonly ArticleRepository $articleRepository
+    )
+    {
+        parent::__construct();
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $io = new SymfonyStyle($input, $output);
+        
+        $this->sluggerService->generateSlugsForExistingArticles();
+
+        $articles = $this->articleRepository->findAll();
+        foreach($articles as $article) {
+            $io->text(sprintf('Article : %s / Slug : %s', $article->getTitle(), $article->getSlug()));
+        }
+
+        $io->success('Les slugs sont générés');
+
+        return Command::SUCCESS;
+    }
+}

+ 16 - 0
src/Entity/Article.php

@@ -5,6 +5,7 @@ namespace App\Entity;
 use App\Repository\ArticleRepository;
 use Doctrine\DBAL\Types\Types;
 use Doctrine\ORM\Mapping as ORM;
+use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
 
 #[ORM\Entity(repositoryClass: ArticleRepository::class)]
 class Article
@@ -30,6 +31,9 @@ class Article
     #[ORM\Column(length: 64)]
     private ?string $state = null;
 
+    #[ORM\Column(length: 100, unique: false)]
+    private ?string $slug = null;
+
     public function __construct(User $author)
     {
         $this->setAuthor($author);
@@ -100,4 +104,16 @@ class Article
 
         return $this;
     }
+
+    public function getSlug(): ?string
+    {
+        return $this->slug;
+    }
+
+    public function setSlug(string $slug): static
+    {
+        $this->slug = $slug;
+
+        return $this;
+    }
 }

+ 1 - 0
src/Form/ArticleType.php

@@ -19,6 +19,7 @@ class ArticleType extends AbstractType
     {
         $builder
             ->add('title')
+            ->add('slug')
             ->add('content', TextareaType::class, [
                 'attr'  =>  [
                     'data-controller'   => 'easymde'

+ 40 - 0
src/Service/SluggerService.php

@@ -0,0 +1,40 @@
+<?php
+
+// src/Service/SluggerService.php
+
+namespace App\Service;
+
+use App\Entity\Article;
+use Doctrine\ORM\EntityManagerInterface;
+use Symfony\Component\String\Slugger\SluggerInterface;
+
+class SluggerService
+{
+    public function __construct(
+        protected readonly EntityManagerInterface $entityManager,
+        protected readonly SluggerInterface $slugger)
+    {
+    }
+
+    public function generateSlugsForExistingArticles()
+    {
+        $articles = $this->entityManager->getRepository(Article::class)->findAll();
+
+        foreach ($articles as $article) {
+            $slug = $this->slugger->slug($article->getTitle())->lower();
+            $existingSlug = $slug;
+
+            // Vérification des conflits
+            $count = 1;
+            while ($this->entityManager->getRepository(Article::class)->findOneBy(['slug' => $existingSlug])) {
+                $existingSlug = $slug . '-' . $count;
+                $count++;
+            }
+
+            $article->setSlug($existingSlug);
+            $this->entityManager->persist($article);
+        }
+
+        $this->entityManager->flush();
+    }
+}