Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Image / StylesFlushCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Image\StylesFlushCommand.
6  */
7 namespace Drupal\Console\Command\Image;
8
9 use Symfony\Component\Console\Input\InputArgument;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Drupal\Console\Core\Command\Command;
13 use Drupal\Core\Entity\EntityTypeManagerInterface;
14
15 class StylesFlushCommand extends Command
16 {
17     /**
18      * @var EntityTypeManagerInterface
19      */
20     protected $entityTypeManager;
21
22     /**
23      * StylesDebugCommand constructor.
24      *
25      * @param EntityTypeManagerInterface $entityTypeManager
26      */
27     public function __construct(EntityTypeManagerInterface $entityTypeManager)
28     {
29         $this->entityTypeManager = $entityTypeManager;
30         parent::__construct();
31     }
32
33     protected function configure()
34     {
35         $this
36             ->setName('image:styles:flush')
37             ->setDescription($this->trans('commands.image.styles.flush.description'))
38             ->addArgument(
39                 'styles',
40                 InputArgument::IS_ARRAY | InputArgument::REQUIRED,
41                 $this->trans('commands.image.styles.flush.options.image-style')
42             )->setAliases(['isf']);
43     }
44
45     /**
46      * {@inheritdoc}
47      */
48     protected function interact(InputInterface $input, OutputInterface $output)
49     {
50         $styles = $input->getArgument('styles');
51         if (!$styles) {
52             $imageStyle = $this->entityTypeManager->getStorage('image_style');
53             $styleList = $imageStyle->loadMultiple();
54             $styleNames = [];
55             foreach ($styleList as $style) {
56                 $styleNames[] = $style->get('name');
57             }
58
59             $styles = $this->getIo()->choice(
60                 $this->trans('commands.image.styles.flush.questions.image-style'),
61                 $styleNames,
62                 null,
63                 true
64             );
65
66             $input->setArgument('styles', $styles);
67         }
68     }
69
70     /**
71      * {@inheritdoc}
72      */
73     protected function execute(InputInterface $input, OutputInterface $output)
74     {
75         $styles = $input->getArgument('styles');
76         $result = 0;
77
78         $imageStyle = $this->entityTypeManager->getStorage('image_style');
79         $stylesNames = [];
80         if (in_array('all', $styles)) {
81             $styles = $imageStyle->loadMultiple();
82             foreach ($styles as $style) {
83                 $stylesNames[] = $style->get('name');
84             }
85
86             $styles = $stylesNames;
87         }
88
89         foreach ($styles as $style) {
90             try {
91                 $this->getIo()->info(
92                     sprintf(
93                         $this->trans('commands.image.styles.flush.messages.executing-flush'),
94                         $style
95                     )
96                 );
97                 $imageStyle->load($style)->flush();
98             } catch (\Exception $e) {
99                 watchdog_exception('image', $e);
100                 $this->getIo()->error($e->getMessage());
101                 $result = 1;
102             }
103         }
104
105         $this->getIo()->success($this->trans('commands.image.styles.flush.messages.success'));
106
107         return $result;
108     }
109 }