98a0dda18713c17cb9889f2e0b5178ada43af2bd
[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\ConfirmationTrait;
16 use Drupal\Console\Generator\PluginMailGenerator;
17 use Drupal\Console\Core\Command\ContainerAwareCommand;
18 use Drupal\Console\Extension\Manager;
19 use Drupal\Console\Core\Utils\StringConverter;
20 use Drupal\Console\Utils\Validator;
21 use Drupal\Console\Core\Utils\ChainQueue;
22
23 /**
24  * Class PluginMailCommand
25  *
26  * @package Drupal\Console\Command\Generate
27  */
28 class PluginMailCommand extends ContainerAwareCommand
29 {
30     use ServicesTrait;
31     use ModuleTrait;
32     use ConfirmationTrait;
33
34     /**
35  * @var Manager
36 */
37     protected $extensionManager;
38
39     /**
40  * @var PluginMailGenerator
41 */
42     protected $generator;
43
44     /**
45      * @var StringConverter
46      */
47     protected $stringConverter;
48
49     /**
50  * @var Validator
51 */
52     protected $validator;
53
54     /**
55      * @var ChainQueue
56      */
57     protected $chainQueue;
58
59
60     /**
61      * PluginMailCommand constructor.
62      *
63      * @param Manager             $extensionManager
64      * @param PluginMailGenerator $generator
65      * @param StringConverter     $stringConverter
66      * @param Validator           $validator
67      * @param ChainQueue          $chainQueue
68      */
69     public function __construct(
70         Manager $extensionManager,
71         PluginMailGenerator $generator,
72         StringConverter $stringConverter,
73         Validator $validator,
74         ChainQueue $chainQueue
75     ) {
76         $this->extensionManager = $extensionManager;
77         $this->generator = $generator;
78         $this->stringConverter = $stringConverter;
79         $this->validator = $validator;
80         $this->chainQueue = $chainQueue;
81         parent::__construct();
82     }
83
84     protected function configure()
85     {
86         $this
87             ->setName('generate:plugin:mail')
88             ->setDescription($this->trans('commands.generate.plugin.mail.description'))
89             ->setHelp($this->trans('commands.generate.plugin.mail.help'))
90             ->addOption(
91                 'module',
92                 null,
93                 InputOption::VALUE_REQUIRED,
94                 $this->trans('commands.common.options.module')
95             )
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             )->setAliases(['gpm']);
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         $services = $input->getOption('services');
137
138         // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
139         $build_services = $this->buildServices($services);
140
141         $this->generator->generate([
142           'module' => $module,
143           'class_name' => $class_name,
144           'label' => $label,
145           'plugin_id' => $plugin_id,
146           'services' => $build_services,
147         ]);
148
149         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
150     }
151
152     protected function interact(InputInterface $input, OutputInterface $output)
153     {
154         // --module option
155         $this->getModuleOption();
156
157         // --class option
158         $class = $input->getOption('class');
159         if (!$class) {
160             $class = $this->getIo()->ask(
161                 $this->trans('commands.generate.plugin.mail.options.class'),
162                 'HtmlFormatterMail',
163                 function ($class) {
164                     return $this->validator->validateClassName($class);
165                 }
166             );
167             $input->setOption('class', $class);
168         }
169
170         // --label option
171         $label = $input->getOption('label');
172         if (!$label) {
173             $label = $this->getIo()->ask(
174                 $this->trans('commands.generate.plugin.mail.options.label'),
175                 $this->stringConverter->camelCaseToHuman($class)
176             );
177             $input->setOption('label', $label);
178         }
179
180         // --plugin-id option
181         $pluginId = $input->getOption('plugin-id');
182         if (!$pluginId) {
183             $pluginId = $this->getIo()->ask(
184                 $this->trans('commands.generate.plugin.mail.options.plugin-id'),
185                 $this->stringConverter->camelCaseToUnderscore($class)
186             );
187             $input->setOption('plugin-id', $pluginId);
188         }
189
190         // --services option
191         // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
192         $services = $this->servicesQuestion();
193         $input->setOption('services', $services);
194     }
195 }