Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginCKEditorButtonCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\PluginCKEditorButtonCommand.
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\PluginCKEditorButtonGenerator;
15 use Drupal\Console\Command\Shared\ModuleTrait;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Core\Utils\ChainQueue;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Core\Utils\StringConverter;
22
23 class PluginCKEditorButtonCommand extends Command
24 {
25     use CommandTrait;
26     use ModuleTrait;
27     use ConfirmationTrait;
28
29
30     /**
31      * @var ChainQueue
32      */
33     protected $chainQueue;
34
35
36     /**
37  * @var PluginCKEditorButtonGenerator
38 */
39     protected $generator;
40
41     /**
42  * @var Manager
43 */
44     protected $extensionManager;
45
46     /**
47      * @var StringConverter
48      */
49     protected $stringConverter;
50
51
52     /**
53      * PluginCKEditorButtonCommand constructor.
54      *
55      * @param ChainQueue                    $chainQueue
56      * @param PluginCKEditorButtonGenerator $generator
57      * @param Manager                       $extensionManager
58      * @param StringConverter               $stringConverter
59      */
60     public function __construct(
61         ChainQueue $chainQueue,
62         PluginCKEditorButtonGenerator $generator,
63         Manager $extensionManager,
64         StringConverter $stringConverter
65     ) {
66         $this->chainQueue = $chainQueue;
67         $this->generator = $generator;
68         $this->extensionManager = $extensionManager;
69         $this->stringConverter = $stringConverter;
70         parent::__construct();
71     }
72
73     protected function configure()
74     {
75         $this
76             ->setName('generate:plugin:ckeditorbutton')
77             ->setDescription($this->trans('commands.generate.plugin.ckeditorbutton.description'))
78             ->setHelp($this->trans('commands.generate.plugin.ckeditorbutton.help'))
79             ->addOption(
80                 'module',
81                 null,
82                 InputOption::VALUE_REQUIRED,
83                 $this->trans('commands.common.options.module')
84             )
85             ->addOption(
86                 'class',
87                 null,
88                 InputOption::VALUE_REQUIRED,
89                 $this->trans('commands.generate.plugin.ckeditorbutton.options.class')
90             )
91             ->addOption(
92                 'label',
93                 null,
94                 InputOption::VALUE_REQUIRED,
95                 $this->trans('commands.generate.plugin.ckeditorbutton.options.label')
96             )
97             ->addOption(
98                 'plugin-id',
99                 null,
100                 InputOption::VALUE_REQUIRED,
101                 $this->trans('commands.generate.plugin.ckeditorbutton.options.plugin-id')
102             )
103             ->addOption(
104                 'button-name',
105                 null,
106                 InputOption::VALUE_REQUIRED,
107                 $this->trans('commands.generate.plugin.ckeditorbutton.options.button-name')
108             )
109             ->addOption(
110                 'button-icon-path',
111                 null,
112                 InputOption::VALUE_REQUIRED,
113                 $this->trans('commands.generate.plugin.ckeditorbutton.options.button-icon-path')
114             );
115     }
116
117     /**
118      * {@inheritdoc}
119      */
120     protected function execute(InputInterface $input, OutputInterface $output)
121     {
122         $io = new DrupalStyle($input, $output);
123
124         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
125         if (!$this->confirmGeneration($io)) {
126             return 1;
127         }
128
129         $module = $input->getOption('module');
130         $class_name = $input->getOption('class');
131         $label = $input->getOption('label');
132         $plugin_id = $input->getOption('plugin-id');
133         $button_name = $input->getOption('button-name');
134         $button_icon_path = $input->getOption('button-icon-path');
135
136         $this
137             ->generator
138             ->generate($module, $class_name, $label, $plugin_id, $button_name, $button_icon_path);
139
140         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery'], false);
141
142         return 0;
143     }
144
145     protected function interact(InputInterface $input, OutputInterface $output)
146     {
147         $io = new DrupalStyle($input, $output);
148
149         // --module option
150         $module = $input->getOption('module');
151         if (!$module) {
152             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
153             $module = $this->moduleQuestion($io);
154             $input->setOption('module', $module);
155         }
156
157         // --class option
158         $class_name = $input->getOption('class');
159         if (!$class_name) {
160             $class_name = $io->ask(
161                 $this->trans('commands.generate.plugin.ckeditorbutton.questions.class'),
162                 'DefaultCKEditorButton'
163             );
164             $input->setOption('class', $class_name);
165         }
166
167         // --label option
168         $label = $input->getOption('label');
169         if (!$label) {
170             $label = $io->ask(
171                 $this->trans('commands.generate.plugin.ckeditorbutton.questions.label'),
172                 $this->stringConverter->camelCaseToHuman($class_name)
173             );
174             $input->setOption('label', $label);
175         }
176
177         // --plugin-id option
178         $plugin_id = $input->getOption('plugin-id');
179         if (!$plugin_id) {
180             $plugin_id = $io->ask(
181                 $this->trans('commands.generate.plugin.ckeditorbutton.questions.plugin-id'),
182                 $this->stringConverter->camelCaseToLowerCase($label)
183             );
184             $input->setOption('plugin-id', $plugin_id);
185         }
186
187         // --button-name option
188         $button_name = $input->getOption('button-name');
189         if (!$button_name) {
190             $button_name = $io->ask(
191                 $this->trans('commands.generate.plugin.ckeditorbutton.questions.button-name'),
192                 $this->stringConverter->anyCaseToUcFirst($plugin_id)
193             );
194             $input->setOption('button-name', $button_name);
195         }
196
197         // --button-icon-path option
198         $button_icon_path = $input->getOption('button-icon-path');
199         if (!$button_icon_path) {
200             $button_icon_path = $io->ask(
201                 $this->trans('commands.generate.plugin.ckeditorbutton.questions.button-icon-path'),
202                 drupal_get_path('module', $module) . '/js/plugins/' . $plugin_id . '/images/icon.png'
203             );
204             $input->setOption('button-icon-path', $button_icon_path);
205         }
206     }
207 }