Browse Source

Ajout de la commande de mise a jour des slugs de tags (tagengine:update-slugs).

bastien 13 years ago
parent
commit
9aa1634a17

+ 55 - 0
src/Muzich/CoreBundle/Command/UpdateTagSlugsCommand.php View File

@@ -0,0 +1,55 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Command;
4
+
5
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
+use Symfony\Component\Console\Input\InputArgument;
7
+use Symfony\Component\Console\Input\InputInterface;
8
+use Symfony\Component\Console\Input\InputOption;
9
+use Symfony\Component\Console\Output\OutputInterface;
10
+
11
+use Muzich\CoreBundle\ElementFactory\ElementManager;
12
+
13
+class UpdateTagSlugsCommand extends ContainerAwareCommand
14
+{
15
+  protected function configure()
16
+  {
17
+    $this
18
+      ->setName('tagengine:update-slugs')
19
+      ->setDescription('Actualise les slugs')
20
+      // Dans l'avenir on pourra préciser:
21
+      // - le type
22
+      // - l'élément
23
+//      ->addOption('sites', null, InputOption::VALUE_REQUIRED, 'Liste exhaustive des site a traiter')
24
+//      ->addArgument('sites', InputArgument::OPTIONAL, 'Liste exhaustive des site a traiter')
25
+//      ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
26
+    ;
27
+  }
28
+
29
+  protected function execute(InputInterface $input, OutputInterface $output)
30
+  {
31
+    $doctrine = $this->getContainer()->get('doctrine');
32
+    $em = $doctrine->getEntityManager();
33
+
34
+    $output->writeln('#');
35
+    $output->writeln('## Script de mise a jour des slugs tags ##');
36
+    $output->writeln('#');
37
+ 
38
+    // On récupère les tags
39
+    $tags = $em->getRepository('MuzichCoreBundle:Tag')->findAll();
40
+    $tag_manager = $this->getContainer()->get('muzich_tag_manager');
41
+    
42
+    $output->writeln('<info>Nombre de tags a traiter: '.count($tags).'</info>');
43
+    $output->writeln('<info>Début du traitement ...</info>');
44
+    
45
+    foreach ($tags as $tag)
46
+    {
47
+      $tag_manager->updateSlug($tag);
48
+      $em->persist($tag);
49
+    }
50
+    
51
+    $output->writeln('<info>Traitement terminé, enregistrement en base ...</info>');
52
+    $em->flush();
53
+    $output->writeln('<info>Enregistrement terminé !</info>');
54
+  }
55
+}

+ 6 - 18
src/Muzich/CoreBundle/Managers/TagManager.php View File

@@ -5,6 +5,7 @@ namespace Muzich\CoreBundle\Managers;
5 5
 use Muzich\CoreBundle\Entity\Tag;
6 6
 use Doctrine\ORM\EntityManager;
7 7
 use Symfony\Component\DependencyInjection\Container;
8
+use FOS\UserBundle\Util\CanonicalizerInterface;
8 9
 
9 10
 /**
10 11
  * 
@@ -14,29 +15,16 @@ use Symfony\Component\DependencyInjection\Container;
14 15
 class TagManager
15 16
 {
16 17
   
17
-  protected $em;
18
+  protected $nameCanonicalizer;
18 19
   
19
-  public function __construct(EntityManager $em)
20
+  public function __construct(CanonicalizerInterface $nameCanonicalizer)
20 21
   {
21
-    $this->em = $em;
22
-    
23
-    // Slug stuff
24
-    $evm = new \Doctrine\Common\EventManager();
25
-    // ORM and ODM
26
-    $sluggableListener = new \Gedmo\Sluggable\SluggableListener();
27
-    $evm->addEventSubscriber($sluggableListener);
28
-    // now this event manager should be passed to entity manager constructor
29
-    $this->em->getEventManager()->addEventSubscriber($sluggableListener);
22
+    $this->nameCanonicalizer = $nameCanonicalizer;
30 23
   }
31 24
   
32
-  public function flush()
25
+  public function updateSlug(Tag $tag)
33 26
   {
34
-    $this->em->flush();
35
-  }
36
-  
37
-  public function persist(Tag $tag)
38
-  {
39
-    $this->em->persist($tag);
27
+    $tag->setSlug($this->nameCanonicalizer->canonicalize($tag->getName()));
40 28
   }
41 29
   
42 30
 }