Version 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                 '',
82                 InputOption::VALUE_REQUIRED,
83                 $this->trans('commands.common.options.module')
84             )
85             ->addOption(
86                 'class',
87                 '',
88                 InputOption::VALUE_REQUIRED,
89                 $this->trans('commands.generate.plugin.ckeditorbutton.options.class')
90             )
91             ->addOption(
92                 'label',
93                 '',
94                 InputOption::VALUE_REQUIRED,
95                 $this->trans('commands.generate.plugin.ckeditorbutton.options.label')
96             )
97             ->addOption(
98                 'plugin-id',
99                 '',
100                 InputOption::VALUE_REQUIRED,
101                 $this->trans('commands.generate.plugin.ckeditorbutton.options.plugin-id')
102             )
103             ->addOption(
104                 'button-name',
105                 '',
106                 InputOption::VALUE_REQUIRED,
107                 $this->trans('commands.generate.plugin.ckeditorbutton.options.button-name')
108             )
109             ->addOption(
110                 'button-icon-path',
111                 '',
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;
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
143     protected function interact(InputInterface $input, OutputInterface $output)
144     {
145         $io = new DrupalStyle($input, $output);
146
147         // --module option
148         $module = $input->getOption('module');
149         if (!$module) {
150             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
151             $module = $this->moduleQuestion($io);
152             $input->setOption('module', $module);
153         }
154
155         // --class option
156         $class_name = $input->getOption('class');
157         if (!$class_name) {
158             $class_name = $io->ask(
159                 $this->trans('commands.generate.plugin.ckeditorbutton.questions.class'),
160                 'DefaultCKEditorButton'
161             );
162             $input->setOption('class', $class_name);
163         }
164
165         // --label option
166         $label = $input->getOption('label');
167         if (!$label) {
168             $label = $io->ask(
169                 $this->trans('commands.generate.plugin.ckeditorbutton.questions.label'),
170                 $this->stringConverter->camelCaseToHuman($class_name)
171             );
172             $input->setOption('label', $label);
173         }
174
175         // --plugin-id option
176         $plugin_id = $input->getOption('plugin-id');
177         if (!$plugin_id) {
178             $plugin_id = $io->ask(
179                 $this->trans('commands.generate.plugin.ckeditorbutton.questions.plugin-id'),
180                 $this->stringConverter->camelCaseToLowerCase($label)
181             );
182             $input->setOption('plugin-id', $plugin_id);
183         }
184
185         // --button-name option
186         $button_name = $input->getOption('button-name');
187         if (!$button_name) {
188             $button_name = $io->ask(
189                 $this->trans('commands.generate.plugin.ckeditorbutton.questions.button-name'),
190                 $this->stringConverter->anyCaseToUcFirst($plugin_id)
191             );
192             $input->setOption('button-name', $button_name);
193         }
194
195         // --button-icon-path option
196         $button_icon_path = $input->getOption('button-icon-path');
197         if (!$button_icon_path) {
198             $button_icon_path = $io->ask(
199                 $this->trans('commands.generate.plugin.ckeditorbutton.questions.button-icon-path'),
200                 drupal_get_path('module', $module) . '/js/plugins/' . $plugin_id . '/images/icon.png'
201             );
202             $input->setOption('button-icon-path', $button_icon_path);
203         }
204     }
205 }