0f0c2fa5491dfb514b1ea91cade9560e0d0de959
[yaffs-website] / vendor / drupal / console / src / Command / Generate / BreakPointCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\BreakPointCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Drupal\Console\Command\Shared\ArrayInputTrait;
11 use Drupal\Console\Command\Shared\ConfirmationTrait;
12 use Drupal\Console\Command\Shared\ThemeBreakpointTrait;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Console\Core\Utils\StringConverter;
15 use Drupal\Console\Generator\BreakPointGenerator;
16 use Drupal\Console\Utils\Validator;
17 use Drupal\Core\Extension\ThemeHandler;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Output\OutputInterface;
21
22 /**
23  * Class BreakPointCommand
24  *
25  * @package Drupal\Console\Command\Generate
26  */
27 class BreakPointCommand extends Command
28 {
29     use ArrayInputTrait;
30     use ConfirmationTrait;
31     use ThemeBreakpointTrait;
32
33     /**
34      * @var BreakPointGenerator
35      */
36     protected $generator;
37
38     /**
39      * @var Site
40      */
41     protected $site;
42
43     /**
44      * @var string
45      */
46     protected $appRoot;
47
48     /**
49      * @var ThemeHandler
50      */
51     protected $themeHandler;
52
53     /**
54      * @var Validator
55      */
56     protected $validator;
57
58     /**
59      * @var StringConverter
60      */
61     protected $stringConverter;
62
63     /**
64      * BreakPointCommand constructor.
65      *
66      * @param BreakPointGenerator $generator
67      * @param string              $appRoot
68      * @param ThemeHandler        $themeHandler
69      * @param Validator           $validator
70      * @param StringConverter     $stringConverter
71      */
72     public function __construct(
73         BreakPointGenerator $generator,
74         $appRoot,
75         ThemeHandler $themeHandler,
76         Validator $validator,
77         StringConverter $stringConverter
78     ) {
79         $this->generator = $generator;
80         $this->appRoot = $appRoot;
81         $this->themeHandler = $themeHandler;
82         $this->validator = $validator;
83         $this->stringConverter = $stringConverter;
84         parent::__construct();
85     }
86
87     /**
88      * {@inheritdoc}
89      */
90     protected function configure()
91     {
92         $this
93             ->setName('generate:breakpoint')
94             ->setDescription($this->trans('commands.generate.breakpoint.description'))
95             ->setHelp($this->trans('commands.generate.breakpoint.help'))
96             ->addOption(
97                 'theme',
98                 null,
99                 InputOption::VALUE_REQUIRED,
100                 $this->trans('commands.generate.breakpoint.options.theme')
101             )
102             ->addOption(
103                 'breakpoints',
104                 null,
105                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
106                 $this->trans('commands.generate.breakpoint.options.breakpoints')
107             )->setAliases(['gb']);
108     }
109
110     /**
111      * {@inheritdoc}
112      */
113     protected function execute(InputInterface $input, OutputInterface $output)
114     {
115         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
116         if (!$this->confirmOperation()) {
117             return 1;
118         }
119
120         $validators = $this->validator;
121         // we must to ensure theme exist
122         $machine_name = $validators->validateMachineName($input->getOption('theme'));
123         $theme = $input->getOption('theme');
124         $breakpoints = $input->getOption('breakpoints');
125         $noInteraction = $input->getOption('no-interaction');
126         // Parse nested data.
127         if ($noInteraction) {
128             $breakpoints = $this->explodeInlineArray($breakpoints);
129         }
130
131         $this->generator->generate([
132             'theme' => $theme,
133             'breakpoints' => $breakpoints,
134             'machine_name' => $machine_name,
135         ]);
136
137         return 0;
138     }
139
140     /**
141      * {@inheritdoc}
142      */
143     protected function interact(InputInterface $input, OutputInterface $output)
144     {
145         // --theme option.
146         $theme = $input->getOption('theme');
147
148         if (!$theme) {
149             $themeHandler = $this->themeHandler;
150             $themes = $themeHandler->rebuildThemeData();
151             $themes['classy'] = '';
152
153             uasort($themes, 'system_sort_modules_by_info_name');
154
155             $theme = $this->getIo()->choiceNoList(
156                 $this->trans('commands.generate.breakpoint.questions.theme'),
157                 array_keys($themes)
158             );
159             $input->setOption('theme', $theme);
160         }
161
162         // --breakpoints option.
163         $breakpoints = $input->getOption('breakpoints');
164         if (!$breakpoints) {
165             $breakpoints = $this->breakpointQuestion();
166             $input->setOption('breakpoints', $breakpoints);
167         } else {
168             $breakpoints = $this->explodeInlineArray($breakpoints);
169         }
170         $input->setOption('breakpoints', $breakpoints);
171     }
172 }