df71f6f32216f5808c2fc4719b79d99fc057a025
[yaffs-website] / vendor / drush / drush / src / Drupal / Commands / core / ImageCommands.php
1 <?php
2
3 namespace Drush\Drupal\Commands\core;
4
5 use Consolidation\AnnotatedCommand\AnnotationData;
6 use Drupal\image\Entity\ImageStyle;
7 use Drush\Commands\DrushCommands;
8 use Drush\Utils\StringUtils;
9 use Symfony\Component\Console\Input\InputInterface;
10
11 class ImageCommands extends DrushCommands
12 {
13
14     /**
15      * Flush all derived images for a given style.
16      *
17      * @command image:flush
18      * @param $style_names A comma delimited list of image style machine names. If not provided, user may choose from a list of names.
19      * @option all Flush all derived images
20      * @usage drush image:flush
21      *   Pick an image style and then delete its derivatives.
22      * @usage drush image:flush thumbnail,large
23      *   Delete all thumbnail and large derivatives.
24      * @usage drush image:flush --all
25      *   Flush all derived images. They will be regenerated on demand.
26      * @validate-entity-load image_style style_names
27      * @validate-module-enabled image
28      * @aliases if,image-flush
29      */
30     public function flush($style_names, $options = ['all' => false])
31     {
32         foreach (ImageStyle::loadMultiple(StringUtils::csvToArray($style_names)) as $style_name => $style) {
33             $style->flush();
34             $this->logger()->success(dt('Image style !style_name flushed', ['!style_name' => $style_name]));
35         }
36     }
37
38     /**
39      * @hook interact image-flush
40      */
41     public function interactFlush($input, $output)
42     {
43         $styles = array_keys(ImageStyle::loadMultiple());
44         $style_names = $input->getArgument('style_names');
45
46         if (empty($style_names)) {
47             $styles_all = $styles;
48             array_unshift($styles_all, 'all');
49             $choices = array_combine($styles_all, $styles_all);
50             $style_names = $this->io()->choice(dt("Choose a style to flush"), $choices, 'all');
51             if ($style_names == 'all') {
52                 $style_names = implode(',', $styles);
53             }
54             $input->setArgument('style_names', $style_names);
55         }
56     }
57
58     /**
59      * @hook init image-flush
60      */
61     public function initFlush(InputInterface $input, AnnotationData $annotationData)
62     {
63         // Needed for non-interactive calls.
64         if ($input->getOption('all')) {
65             $styles = array_keys(ImageStyle::loadMultiple());
66             $input->setArgument('style_names', implode(",", $styles));
67         }
68     }
69
70     /**
71      * Create an image derivative.
72      *
73      * @command image:derive
74      * @param $style_name An image style machine name.
75      * @param $source Path to a source image. Optionally prepend stream wrapper scheme.
76      * @usage drush image:derive thumbnail core/themes/bartik/screenshot.png
77      *   Save thumbnail sized derivative of logo image.
78      * @validate-file-exists source
79      * @validate-entity-load image_style style_name
80      * @validate-module-enabled image
81      * @aliases id,image-derive
82      */
83     public function derive($style_name, $source)
84     {
85         $image_style = ImageStyle::load($style_name);
86         $derivative_uri = $image_style->buildUri($source);
87         if ($image_style->createDerivative($source, $derivative_uri)) {
88             return $derivative_uri;
89         }
90     }
91 }