Yaffs site version 1.1
[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 Symfony\Component\Console\Command\Command;
13 use Drupal\Core\Entity\EntityTypeManagerInterface;
14 use Drupal\Console\Core\Command\Shared\CommandTrait;
15 use Drupal\Console\Core\Style\DrupalStyle;
16
17 class StylesFlushCommand extends Command
18 {
19     use CommandTrait;
20
21     /**
22      * @var EntityTypeManagerInterface
23      */
24     protected $entityTypeManager;
25
26     /**
27      * StylesDebugCommand constructor.
28      *
29      * @param EntityTypeManagerInterface $entityTypeManager
30      */
31     public function __construct(EntityTypeManagerInterface $entityTypeManager)
32     {
33         $this->entityTypeManager = $entityTypeManager;
34         parent::__construct();
35     }
36
37     protected function configure()
38     {
39         $this
40             ->setName('image:styles:flush')
41             ->setDescription($this->trans('commands.image.styles.flush.description'))
42             ->addArgument(
43                 'styles',
44                 InputArgument::IS_ARRAY | InputArgument::REQUIRED,
45                 $this->trans('commands.image.styles.flush.options.image-style')
46             );
47     }
48
49     /**
50      * {@inheritdoc}
51      */
52     protected function interact(InputInterface $input, OutputInterface $output)
53     {
54         $io = new DrupalStyle($input, $output);
55         $styles = $input->getArgument('styles');
56         if (!$styles) {
57             $imageStyle = $this->entityTypeManager->getStorage('image_style');
58             $styleList = $imageStyle->loadMultiple();
59             $styleNames = [];
60             foreach ($styleList as $style) {
61                 $styleNames[] = $style->get('name');
62             }
63
64             $styles = $io->choice(
65                 $this->trans('commands.image.styles.flush.questions.image-style'),
66                 $styleNames,
67                 null,
68                 true
69             );
70
71             $input->setArgument('styles', $styles);
72         }
73     }
74
75     /**
76      * {@inheritdoc}
77      */
78     protected function execute(InputInterface $input, OutputInterface $output)
79     {
80         $io = new DrupalStyle($input, $output);
81         $styles = $input->getArgument('styles');
82         $result = 0;
83
84         $imageStyle = $this->entityTypeManager->getStorage('image_style');
85         $stylesNames = [];
86         if (in_array('all', $styles)) {
87             $styles = $imageStyle->loadMultiple();
88             foreach ($styles as $style) {
89                 $stylesNames[] = $style->get('name');
90             }
91
92             $styles = $stylesNames;
93         }
94
95         foreach ($styles as $style) {
96             try {
97                 $io->info(
98                     sprintf(
99                         $this->trans('commands.image.styles.flush.messages.executing-flush'),
100                         $style
101                     )
102                 );
103                 $imageStyle->load($style)->flush();
104             } catch (\Exception $e) {
105                 watchdog_exception('image', $e);
106                 $io->error($e->getMessage());
107                 $result = 1;
108             }
109         }
110
111         $io->success($this->trans('commands.image.styles.flush.messages.success'));
112
113         return $result;
114     }
115 }