Version 1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / CommandCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\CommandCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Drupal\Console\Command\Shared\ExtensionTrait;
11 use Drupal\Console\Command\Shared\ServicesTrait;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Input\InputOption;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
17 use Drupal\Console\Command\Shared\ConfirmationTrait;
18 use Drupal\Console\Command\Shared\ModuleTrait;
19 use Drupal\Console\Generator\CommandGenerator;
20 use Drupal\Console\Core\Utils\StringConverter;
21 use Drupal\Console\Extension\Manager;
22 use Drupal\Console\Core\Style\DrupalStyle;
23 use Drupal\Console\Utils\Validator;
24
25 class CommandCommand extends Command
26 {
27     use ContainerAwareCommandTrait;
28     use ConfirmationTrait;
29     use ServicesTrait;
30     use ModuleTrait;
31     use ExtensionTrait;
32
33     /**
34      * @var CommandGenerator
35      */
36     protected $generator;
37
38     /**
39      * @var Manager
40      */
41     protected $extensionManager;
42
43     /**
44      * @var Validator
45      */
46     protected $validator;
47
48     /**
49      * @var StringConverter
50      */
51     protected $stringConverter;
52
53     /**
54      * CommandCommand constructor.
55      *
56      * @param CommandGenerator $generator
57      * @param Manager          $extensionManager
58      * @param Validator        $validator
59      * @param StringConverter  $stringConverter
60      */
61     public function __construct(
62         CommandGenerator $generator,
63         Manager $extensionManager,
64         Validator $validator,
65         StringConverter $stringConverter
66     ) {
67         $this->generator = $generator;
68         $this->extensionManager = $extensionManager;
69         $this->validator = $validator;
70         $this->stringConverter = $stringConverter;
71         parent::__construct();
72     }
73
74     /**
75      * {@inheritdoc}
76      */
77     protected function configure()
78     {
79         $this
80             ->setName('generate:command')
81             ->setDescription($this->trans('commands.generate.command.description'))
82             ->setHelp($this->trans('commands.generate.command.help'))
83             ->addOption(
84                 'extension',
85                 '',
86                 InputOption::VALUE_REQUIRED,
87                 $this->trans('commands.common.options.extension')
88             )
89             ->addOption(
90                 'extension-type',
91                 '',
92                 InputOption::VALUE_REQUIRED,
93                 $this->trans('commands.common.options.extension-type')
94             )
95             ->addOption(
96                 'class',
97                 '',
98                 InputOption::VALUE_REQUIRED,
99                 $this->trans('commands.generate.command.options.class')
100             )
101             ->addOption(
102                 'name',
103                 '',
104                 InputOption::VALUE_REQUIRED,
105                 $this->trans('commands.generate.command.options.name')
106             )
107             ->addOption(
108                 'container-aware',
109                 '',
110                 InputOption::VALUE_NONE,
111                 $this->trans('commands.generate.command.options.container-aware')
112             )
113             ->addOption(
114                 'services',
115                 '',
116                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
117                 $this->trans('commands.common.options.services')
118             );
119     }
120
121     /**
122      * {@inheritdoc}
123      */
124     protected function execute(InputInterface $input, OutputInterface $output)
125     {
126         $io = new DrupalStyle($input, $output);
127
128         $extension = $input->getOption('extension');
129         $extensionType = $input->getOption('extension-type');
130         $class = $input->getOption('class');
131         $name = $input->getOption('name');
132         $containerAware = $input->getOption('container-aware');
133         $services = $input->getOption('services');
134         $yes = $input->hasOption('yes')?$input->getOption('yes'):false;
135
136         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
137         if (!$this->confirmGeneration($io, $yes)) {
138             return;
139         }
140
141         // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
142         $build_services = $this->buildServices($services);
143
144         $this->generator->generate(
145             $extension,
146             $extensionType,
147             $name,
148             $class,
149             $containerAware,
150             $build_services
151         );
152     }
153
154     /**
155      * {@inheritdoc}
156      */
157     protected function interact(InputInterface $input, OutputInterface $output)
158     {
159         $io = new DrupalStyle($input, $output);
160
161         $extension = $input->getOption('extension');
162         if (!$extension) {
163             $extension = $this->extensionQuestion($io, true, true);
164             $input->setOption('extension', $extension->getName());
165             $input->setOption('extension-type', $extension->getType());
166         }
167
168         $extensionType = $input->getOption('extension-type');
169         if (!$extensionType) {
170             $extensionType = $this->extensionTypeQuestion($io);
171             $input->setOption('extension-type', $extensionType);
172         }
173
174         $name = $input->getOption('name');
175         if (!$name) {
176             $name = $io->ask(
177                 $this->trans('commands.generate.command.questions.name'),
178                 sprintf('%s:default', $extension->getName())
179             );
180             $input->setOption('name', $name);
181         }
182
183         $class = $input->getOption('class');
184         if (!$class) {
185             $class = $io->ask(
186                 $this->trans('commands.generate.command.questions.class'),
187                 'DefaultCommand',
188                 function ($class) {
189                     return $this->validator->validateCommandName($class);
190                 }
191             );
192             $input->setOption('class', $class);
193         }
194
195         $containerAware = $input->getOption('container-aware');
196         if (!$containerAware) {
197             $containerAware = $io->confirm(
198                 $this->trans('commands.generate.command.questions.container-aware'),
199                 false
200             );
201             $input->setOption('container-aware', $containerAware);
202         }
203
204         if (!$containerAware) {
205             // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
206             $services = $this->servicesQuestion($io);
207             $input->setOption('services', $services);
208         }
209     }
210 }