MigrationUpgradeCommand.php 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Muzich\CoreBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. //use Muzich\CoreBundle\Managers\CommentsManager;
  9. use Muzich\CoreBundle\Util\StrictCanonicalizer;
  10. class MigrationUpgradeCommand extends ContainerAwareCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('migration:upgrade')
  16. ->setDescription('Mise a jour de données nécéssaire pour migrations')
  17. ->addArgument('from', InputArgument::REQUIRED, 'version de départ')
  18. ->addArgument('to', InputArgument::REQUIRED, 'version visé')
  19. ;
  20. }
  21. protected function execute(InputInterface $input, OutputInterface $output)
  22. {
  23. $doctrine = $this->getContainer()->get('doctrine');
  24. $em = $doctrine->getEntityManager();
  25. $output->writeln('#');
  26. $output->writeln('## Outil de migration ##');
  27. $output->writeln('#');
  28. $proceeded = false;
  29. if ($input->getArgument('from') == '0.6' && $input->getArgument('to') == '0.7')
  30. {
  31. $proceeded = true;
  32. $output->writeln('<info>Mise a jour des données "commentaires" pour migration 0.6 => 0.7</info>');
  33. /**
  34. * Pour cette migration on a besoin de rajouter une valeur aux données
  35. * de commentaire d'éléments.
  36. */
  37. $elements = $em->getRepository('MuzichCoreBundle:Element')->findAll();
  38. foreach ($elements as $element)
  39. {
  40. if (count(($comments = $element->getComments())))
  41. {
  42. // Oui un for serai plus performant ...
  43. foreach ($comments as $i => $comment)
  44. {
  45. // Si il n'y as pas d'enregistrement 'a' dans le commentaire
  46. if (!array_key_exists('a', $comment))
  47. {
  48. // Il faut le rajouter
  49. $comments[$i]['a'] = array();
  50. }
  51. }
  52. $element->setComments($comments);
  53. $em->persist($element);
  54. }
  55. }
  56. $em->flush();
  57. $output->writeln('<info>Terminé !</info>');
  58. }
  59. if ($input->getArgument('from') == '0.9.8.1' && $input->getArgument('to') == '0.9.8.2')
  60. {
  61. $proceeded = true;
  62. $canonicalizer = new StrictCanonicalizer();
  63. $elements = $em->getRepository('MuzichCoreBundle:Element')->findAll();
  64. foreach ($elements as $element)
  65. {
  66. $element->setSlug($canonicalizer->canonicalize_stricter($element->getName()));
  67. $output->write('.');
  68. $em->persist($element);
  69. }
  70. $output->writeln('<info>Save in Database ...</info>');
  71. $em->flush();
  72. $output->writeln('<info>Terminé !</info>');
  73. }
  74. if (!$proceeded)
  75. {
  76. $output->writeln('<error>Versions saisies non prises en charges</error>');
  77. }
  78. }
  79. }