vendor/sonata-project/media-bundle/src/Command/AddMassMediaCommand.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\MediaBundle\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16.  * @final since sonata-project/media-bundle 3.21.0
  17.  */
  18. class AddMassMediaCommand extends BaseCommand
  19. {
  20.     /**
  21.      * @var string[]
  22.      */
  23.     protected $setters;
  24.     /**
  25.      * {@inheritdoc}
  26.      */
  27.     public function configure()
  28.     {
  29.         $this->setName('sonata:media:add-multiple')
  30.             ->setDescription('Add medias in mass into the database')
  31.             ->setDefinition([
  32.                 new InputOption('file'nullInputOption::VALUE_OPTIONAL'The file to parse'),
  33.                 new InputOption('delimiter'nullInputOption::VALUE_OPTIONAL'Set the field delimiter (one character only)'','),
  34.                 new InputOption('enclosure'nullInputOption::VALUE_OPTIONAL'Set the field enclosure character (one character only).''"'),
  35.                 new InputOption('escape'nullInputOption::VALUE_OPTIONAL'Set the escape character (one character only). Defaults as a backslash''\\'),
  36.             ]);
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function execute(InputInterface $inputOutputInterface $output)
  42.     {
  43.         $fp $this->getFilePointer($input$output);
  44.         $imported = -1;
  45.         while (!feof($fp)) {
  46.             $data fgetcsv($fpnull$input->getOption('delimiter'), $input->getOption('enclosure'), $input->getOption('escape'));
  47.             if (-=== $imported) {
  48.                 $this->setters $data;
  49.                 ++$imported;
  50.                 continue;
  51.             }
  52.             if (!\is_array($data)) {
  53.                 continue;
  54.             }
  55.             ++$imported;
  56.             $this->insertMedia($data$output);
  57.             $this->optimize();
  58.         }
  59.         $output->writeln('Done!');
  60.     }
  61.     /**
  62.      * @return resource
  63.      */
  64.     protected function getFilePointer(InputInterface $inputOutputInterface $output)
  65.     {
  66.         if (false !== ftell(STDIN)) {
  67.             return STDIN;
  68.         }
  69.         if (!$input->getOption('file')) {
  70.             throw new \RuntimeException('Please provide a CSV file argument or CSV input stream');
  71.         }
  72.         return fopen($input->getOption('file'), 'r');
  73.     }
  74.     protected function insertMedia(array $dataOutputInterface $output)
  75.     {
  76.         $media $this->getMediaManager()->create();
  77.         foreach ($this->setters as $pos => $name) {
  78.             $media->{'set'.$name}($data[$pos]);
  79.         }
  80.         try {
  81.             $this->getMediaManager()->save($media);
  82.             $output->writeln(sprintf(' > %s - %s'$media->getId(), $media->getName()));
  83.         } catch (\Exception $e) {
  84.             $output->writeln(sprintf('<error>%s</error> : %s'$e->getMessage(), json_encode($data)));
  85.         }
  86.     }
  87.     protected function optimize()
  88.     {
  89.         if ($this->getContainer()->has('doctrine')) {
  90.             $this->getContainer()->get('doctrine')->getManager()->getUnitOfWork()->clear();
  91.         }
  92.     }
  93. }