RefreshEmbedsCommand.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\ElementFactory\ElementManager;
  9. class RefreshEmbedsCommand extends ContainerAwareCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('elementengine:refresh-embeds')
  15. ->setDescription('Actualise les embeds')
  16. // Dans l'avenir on pourra préciser:
  17. // - le type
  18. // - l'élément
  19. ->addOption('sites', null, InputOption::VALUE_REQUIRED, 'Liste exhaustive des site a traiter')
  20. // ->addArgument('sites', InputArgument::OPTIONAL, 'Liste exhaustive des site a traiter')
  21. // ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
  22. ;
  23. }
  24. protected function execute(InputInterface $input, OutputInterface $output)
  25. {
  26. $doctrine = $this->getContainer()->get('doctrine');
  27. $em = $doctrine->getEntityManager();
  28. $output->writeln('#');
  29. $output->writeln('## Script de mise a jour des code embeds ##');
  30. $output->writeln('#');
  31. $filter_sites = array();
  32. if (($sites = $input->getOption('sites')))
  33. {
  34. foreach (explode(',', $sites) as $site)
  35. {
  36. $filter_sites[] = trim($site);
  37. }
  38. }
  39. // On récupère les éléments
  40. if (count($filter_sites))
  41. {
  42. $elements = $em->createQuery(
  43. "SELECT e FROM MuzichCoreBundle:Element e "
  44. . " WHERE e.type IN (:types)"
  45. )->setParameter('types', $filter_sites)
  46. ->getResult()
  47. ;
  48. $output->writeln('<comment>Utilisation de filtre par site ('.$input->getOption('sites').')</comment>');
  49. }
  50. else
  51. {
  52. $elements = $em->getRepository('MuzichCoreBundle:Element')->findAll();
  53. }
  54. $output->writeln('<info>Nombre d\'éléments a traiter: '.count($elements).'</info>');
  55. $output->writeln('<info>Début du traitement ...</info>');
  56. foreach ($elements as $element)
  57. {
  58. $output->writeln('<info>Prise en charge de "'.$element->getUrl().'" ...</info>');
  59. $factory = new ElementManager($element, $em, $this->getContainer());
  60. $factory->proceedExtraFields();
  61. $em->persist($element);
  62. }
  63. $output->writeln('<info>Traitement terminé, enregistrement en base ...</info>');
  64. $em->flush();
  65. $output->writeln('<info>Enregistrement terminé !</info>');
  66. }
  67. }