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