Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / ServiceCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\ServiceCommand.
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\Generator\ServiceGenerator;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Symfony\Component\Console\Command\Command;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Core\Utils\ChainQueue;
22 use Drupal\Console\Core\Utils\StringConverter;
23
24 /**
25  * Class ServiceCommand
26  *
27  * @package Drupal\Console\Command\Generate
28  */
29 class ServiceCommand extends Command
30 {
31     use ServicesTrait;
32     use ModuleTrait;
33     use ConfirmationTrait;
34     use ContainerAwareCommandTrait;
35
36     /**
37  * @var Manager
38 */
39     protected $extensionManager;
40
41     /**
42  * @var ServiceGenerator
43 */
44     protected $generator;
45
46     /**
47      * @var StringConverter
48      */
49     protected $stringConverter;
50
51     /**
52      * @var ChainQueue
53      */
54     protected $chainQueue;
55
56     /**
57      * ServiceCommand constructor.
58      *
59      * @param Manager          $extensionManager
60      * @param ServiceGenerator $generator
61      * @param StringConverter  $stringConverter
62      * @param ChainQueue       $chainQueue
63      */
64     public function __construct(
65         Manager $extensionManager,
66         ServiceGenerator $generator,
67         StringConverter $stringConverter,
68         ChainQueue $chainQueue
69     ) {
70         $this->extensionManager = $extensionManager;
71         $this->generator = $generator;
72         $this->stringConverter = $stringConverter;
73         $this->chainQueue = $chainQueue;
74         parent::__construct();
75     }
76
77
78     /**
79      * {@inheritdoc}
80      */
81     protected function configure()
82     {
83         $this
84             ->setName('generate:service')
85             ->setDescription($this->trans('commands.generate.service.description'))
86             ->setHelp($this->trans('commands.generate.service.description'))
87             ->addOption(
88                 'module',
89                 null,
90                 InputOption::VALUE_REQUIRED,
91                 $this->trans('commands.common.options.module')
92             )
93             ->addOption(
94                 'name',
95                 null,
96                 InputOption::VALUE_REQUIRED,
97                 $this->trans('commands.generate.service.options.name')
98             )
99             ->addOption(
100                 'class',
101                 null,
102                 InputOption::VALUE_REQUIRED,
103                 $this->trans('commands.generate.service.options.class')
104             )
105             ->addOption(
106                 'interface',
107                 null,
108                 InputOption::VALUE_NONE,
109                 $this->trans('commands.common.service.options.interface')
110             )
111             ->addOption(
112                 'interface_name',
113                 null,
114                 InputOption::VALUE_OPTIONAL,
115                 $this->trans('commands.common.service.options.interface_name')
116             )
117             ->addOption(
118                 'services',
119                 null,
120                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
121                 $this->trans('commands.common.options.services')
122             )
123             ->addOption(
124                 'path_service',
125                 null,
126                 InputOption::VALUE_OPTIONAL,
127                 $this->trans('commands.generate.service.options.path')
128             );
129     }
130
131     /**
132      * {@inheritdoc}
133      */
134     protected function execute(InputInterface $input, OutputInterface $output)
135     {
136         $io = new DrupalStyle($input, $output);
137
138         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
139         if (!$this->confirmGeneration($io)) {
140             return 1;
141         }
142
143         $module = $input->getOption('module');
144         $name = $input->getOption('name');
145         $class = $input->getOption('class');
146         $interface = $input->getOption('interface');
147         $interface_name = $input->getOption('interface_name');
148         $services = $input->getOption('services');
149         $path_service = $input->getOption('path_service');
150
151         $available_services = $this->container->getServiceIds();
152
153         if (in_array($name, array_values($available_services))) {
154             throw new \Exception(
155                 sprintf(
156                     $this->trans('commands.generate.service.messages.service-already-taken'),
157                     $module
158                 )
159             );
160         }
161
162         // @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
163         $build_services = $this->buildServices($services);
164         $this->generator->generate($module, $name, $class, $interface, $interface_name, $build_services, $path_service);
165
166         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
167
168         return 0;
169     }
170
171     /**
172      * {@inheritdoc}
173      */
174     protected function interact(InputInterface $input, OutputInterface $output)
175     {
176         $io = new DrupalStyle($input, $output);
177
178         // --module option
179         $module = $input->getOption('module');
180         if (!$module) {
181             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
182             $module = $this->moduleQuestion($io);
183             $input->setOption('module', $module);
184         }
185
186         //--name option
187         $name = $input->getOption('name');
188         if (!$name) {
189             $name = $io->ask(
190                 $this->trans('commands.generate.service.questions.service-name'),
191                 $module.'.default'
192             );
193             $input->setOption('name', $name);
194         }
195
196         // --class option
197         $class = $input->getOption('class');
198         if (!$class) {
199             $class = $io->ask(
200                 $this->trans('commands.generate.service.questions.class'),
201                 'DefaultService'
202             );
203             $input->setOption('class', $class);
204         }
205
206         // --interface option
207         $interface = $input->getOption('interface');
208         if (!$interface) {
209             $interface = $io->confirm(
210                 $this->trans('commands.generate.service.questions.interface'),
211                 true
212             );
213             $input->setOption('interface', $interface);
214         }
215
216         // --interface_name option
217         $interface_name = $input->getOption('interface_name');
218         if ($interface && !$interface_name) {
219             $interface_name = $io->askEmpty(
220                 $this->trans('commands.generate.service.questions.interface_name')
221             );
222             $input->setOption('interface_name', $interface_name);
223         }
224
225         // --services option
226         $services = $input->getOption('services');
227         if (!$services) {
228             // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
229             $services = $this->servicesQuestion($io);
230             $input->setOption('services', $services);
231         }
232
233         // --path_service option
234         $path_service = $input->getOption('path_service');
235         if (!$path_service) {
236             $path_service = $io->ask(
237                 $this->trans('commands.generate.service.questions.path'),
238                 '/modules/custom/' . $module . '/src/'
239             );
240             $input->setOption('path_service', $path_service);
241         }
242     }
243 }