Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginBlockCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\PluginBlockCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Generator\PluginBlockGenerator;
15 use Drupal\Console\Command\Shared\ServicesTrait;
16 use Drupal\Console\Command\Shared\ModuleTrait;
17 use Drupal\Console\Command\Shared\FormTrait;
18 use Drupal\Console\Command\Shared\ConfirmationTrait;
19 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Utils\Validator;
22 use Drupal\Console\Core\Utils\StringConverter;
23 use Drupal\Console\Core\Style\DrupalStyle;
24 use Drupal\Console\Core\Utils\ChainQueue;
25 use Drupal\Core\Config\ConfigFactory;
26 use Drupal\Core\Entity\EntityTypeManagerInterface;
27 use Drupal\Core\Render\ElementInfoManagerInterface;
28
29 class PluginBlockCommand extends Command
30 {
31     use ServicesTrait;
32     use ModuleTrait;
33     use FormTrait;
34     use ConfirmationTrait;
35     use ContainerAwareCommandTrait;
36
37     /**
38      * @var ConfigFactory
39      */
40     protected $configFactory;
41
42     /**
43      * @var ChainQueue
44      */
45     protected $chainQueue;
46
47     /**
48      * @var PluginBlockGenerator
49      */
50     protected $generator;
51
52     /**
53      * @var EntityTypeManagerInterface
54      */
55     protected $entityTypeManager;
56
57     /**
58      * @var Manager
59      */
60     protected $extensionManager;
61
62     /**
63      * @var Validator
64      */
65     protected $validator;
66
67     /**
68      * @var StringConverter
69      */
70     protected $stringConverter;
71
72     /**
73      * @var ElementInfoManagerInterface
74      */
75     protected $elementInfoManager;
76
77     /**
78      * PluginBlockCommand constructor.
79      *
80      * @param ConfigFactory               $configFactory
81      * @param ChainQueue                  $chainQueue
82      * @param PluginBlockGenerator        $generator
83      * @param EntityTypeManagerInterface  $entityTypeManager
84      * @param Manager                     $extensionManager
85      * @param Validator                   $validator
86      * @param StringConverter             $stringConverter
87      * @param ElementInfoManagerInterface $elementInfoManager
88      */
89     public function __construct(
90         ConfigFactory $configFactory,
91         ChainQueue $chainQueue,
92         PluginBlockGenerator $generator,
93         EntityTypeManagerInterface $entityTypeManager,
94         Manager $extensionManager,
95         Validator $validator,
96         StringConverter $stringConverter,
97         ElementInfoManagerInterface $elementInfoManager
98     ) {
99         $this->configFactory = $configFactory;
100         $this->chainQueue = $chainQueue;
101         $this->generator = $generator;
102         $this->entityTypeManager = $entityTypeManager;
103         $this->extensionManager = $extensionManager;
104         $this->validator = $validator;
105         $this->stringConverter = $stringConverter;
106         $this->elementInfoManager = $elementInfoManager;
107         parent::__construct();
108     }
109
110     protected function configure()
111     {
112         $this
113             ->setName('generate:plugin:block')
114             ->setDescription($this->trans('commands.generate.plugin.block.description'))
115             ->setHelp($this->trans('commands.generate.plugin.block.help'))
116             ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
117             ->addOption(
118                 'class',
119                 null,
120                 InputOption::VALUE_OPTIONAL,
121                 $this->trans('commands.generate.plugin.block.options.class')
122             )
123             ->addOption(
124                 'label',
125                 null,
126                 InputOption::VALUE_OPTIONAL,
127                 $this->trans('commands.generate.plugin.block.options.label')
128             )
129             ->addOption(
130                 'plugin-id',
131                 null,
132                 InputOption::VALUE_OPTIONAL,
133                 $this->trans('commands.generate.plugin.block.options.plugin-id')
134             )
135             ->addOption(
136                 'theme-region',
137                 null,
138                 InputOption::VALUE_OPTIONAL,
139                 $this->trans('commands.generate.plugin.block.options.theme-region')
140             )
141             ->addOption(
142                 'inputs',
143                 null,
144                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
145                 $this->trans('commands.common.options.inputs')
146             )
147             ->addOption(
148                 'services',
149                 null,
150                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
151                 $this->trans('commands.common.options.services')
152             );
153     }
154
155     /**
156      * {@inheritdoc}
157      */
158     protected function execute(InputInterface $input, OutputInterface $output)
159     {
160         $io = new DrupalStyle($input, $output);
161
162         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
163         if (!$this->confirmGeneration($io)) {
164             return 1;
165         }
166
167         $module = $input->getOption('module');
168         $class_name = $input->getOption('class');
169         $label = $input->getOption('label');
170         $plugin_id = $input->getOption('plugin-id');
171         $services = $input->getOption('services');
172         $theme_region = $input->getOption('theme-region');
173         $inputs = $input->getOption('inputs');
174
175         $theme = $this->configFactory->get('system.theme')->get('default');
176         $themeRegions = \system_region_list($theme, REGIONS_VISIBLE);
177
178         if (!empty($theme_region) && !isset($themeRegions[$theme_region])) {
179             $io->error(
180                 sprintf(
181                     $this->trans('commands.generate.plugin.block.messages.invalid-theme-region'),
182                     $theme_region
183                 )
184             );
185
186             return 1;
187         }
188
189         // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
190         $build_services = $this->buildServices($services);
191
192         $this->generator
193             ->generate(
194                 $module,
195                 $class_name,
196                 $label,
197                 $plugin_id,
198                 $build_services,
199                 $inputs
200             );
201
202         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
203
204         if ($theme_region) {
205             $block = $this->entityTypeManager
206                 ->getStorage('block')
207                 ->create(
208                     [
209                         'id'=> $plugin_id,
210                         'plugin' => $plugin_id,
211                         'theme' => $theme
212                     ]
213                 );
214             $block->setRegion($theme_region);
215             $block->save();
216         }
217     }
218
219     protected function interact(InputInterface $input, OutputInterface $output)
220     {
221         $io = new DrupalStyle($input, $output);
222
223         $theme = $this->configFactory->get('system.theme')->get('default');
224         $themeRegions = \system_region_list($theme, REGIONS_VISIBLE);
225
226         // --module option
227         $module = $input->getOption('module');
228         if (!$module) {
229             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
230             $module = $this->moduleQuestion($io);
231             $input->setOption('module', $module);
232         }
233
234         // --class option
235         $class = $input->getOption('class');
236         if (!$class) {
237             $class = $io->ask(
238                 $this->trans('commands.generate.plugin.block.questions.class'),
239                 'DefaultBlock',
240                 function ($class) {
241                     return $this->validator->validateClassName($class);
242                 }
243             );
244             $input->setOption('class', $class);
245         }
246
247         // --label option
248         $label = $input->getOption('label');
249         if (!$label) {
250             $label = $io->ask(
251                 $this->trans('commands.generate.plugin.block.questions.label'),
252                 $this->stringConverter->camelCaseToHuman($class)
253             );
254             $input->setOption('label', $label);
255         }
256
257         // --plugin-id option
258         $pluginId = $input->getOption('plugin-id');
259         if (!$pluginId) {
260             $pluginId = $io->ask(
261                 $this->trans('commands.generate.plugin.block.questions.plugin-id'),
262                 $this->stringConverter->camelCaseToUnderscore($class)
263             );
264             $input->setOption('plugin-id', $pluginId);
265         }
266
267         // --theme-region option
268         $themeRegion = $input->getOption('theme-region');
269         if (!$themeRegion) {
270             $themeRegion =  $io->choiceNoList(
271                 $this->trans('commands.generate.plugin.block.questions.theme-region'),
272                 array_values($themeRegions),
273                 null,
274                 true
275             );
276             $themeRegion = array_search($themeRegion, $themeRegions);
277             $input->setOption('theme-region', $themeRegion);
278         }
279
280         // --services option
281         // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
282         $services = $this->servicesQuestion($io);
283         $input->setOption('services', $services);
284
285         $output->writeln($this->trans('commands.generate.plugin.block.messages.inputs'));
286
287         // @see Drupal\Console\Command\Shared\FormTrait::formQuestion
288         $inputs = $this->formQuestion($io);
289         $input->setOption('inputs', $inputs);
290     }
291 }