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