3 namespace Drupal\Console\Core;
5 use Drupal\Console\Core\Utils\TranslatorManagerInterface;
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7 use Symfony\Component\EventDispatcher\EventDispatcher;
8 use Symfony\Component\Console\Output\OutputInterface;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Application as BaseApplication;
12 use Drupal\Console\Core\EventSubscriber\DefaultValueEventListener;
13 use Drupal\Console\Core\EventSubscriber\ShowGenerateChainListener;
14 use Drupal\Console\Core\EventSubscriber\ShowTipsListener;
15 use Drupal\Console\Core\EventSubscriber\ShowWelcomeMessageListener;
16 use Drupal\Console\Core\EventSubscriber\ValidateExecutionListener;
17 use Drupal\Console\Core\EventSubscriber\ShowGeneratedFilesListener;
18 use Drupal\Console\Core\EventSubscriber\ShowGenerateInlineListener;
19 use Drupal\Console\Core\EventSubscriber\CallCommandListener;
20 use Drupal\Console\Core\Utils\ConfigurationManager;
21 use Drupal\Console\Core\Style\DrupalStyle;
22 use Drupal\Console\Core\Utils\ChainDiscovery;
23 use Drupal\Console\Core\Command\Chain\ChainCustomCommand;
28 * @package Drupal\Console
30 class Application extends BaseApplication
33 * @var ContainerInterface
40 protected $commandName;
43 * ConsoleApplication constructor.
45 * @param ContainerInterface $container
47 * @param string $version
49 public function __construct(
50 ContainerInterface$container,
54 $this->container = $container;
55 parent::__construct($name, $version);
60 * @return TranslatorManagerInterface
62 public function getTranslator()
64 if ($this->container) {
65 return $this->container->get('console.translator_manager');
76 public function trans($key)
78 if ($this->getTranslator()) {
79 return $this->getTranslator()->trans($key);
88 public function doRun(InputInterface $input, OutputInterface $output)
90 $io = new DrupalStyle($input, $output);
91 if ($commandName = $this->getCommandName($input)) {
92 $this->commandName = $commandName;
94 $this->registerEvents();
95 $this->registerExtendCommands();
96 $this->registerCommandsFromAutoWireConfiguration();
97 $this->registerChainCommands();
100 * @var ConfigurationManager $configurationManager
102 $configurationManager = $this->container
103 ->get('console.configuration_manager');
105 if ($commandName && !$this->has($commandName)) {
108 $this->trans('application.errors.invalid-command'),
116 $code = parent::doRun(
121 if ($this->commandName != 'init' && $configurationManager->getMissingConfigurationFiles()) {
122 $io->warning($this->trans('application.site.errors.missing-config-file'));
123 $io->listing($configurationManager->getMissingConfigurationFiles());
125 $this->trans('application.site.errors.missing-config-file-command')
129 if ($this->getCommandName($input) == 'list' && $this->container->hasParameter('console.warning')) {
131 $this->trans($this->container->getParameter('console.warning'))
141 private function registerEvents()
143 $dispatcher = new EventDispatcher();
144 /* @todo Register listeners as services */
145 $dispatcher->addSubscriber(
146 new ValidateExecutionListener(
147 $this->container->get('console.translator_manager'),
148 $this->container->get('console.configuration_manager')
151 $dispatcher->addSubscriber(
152 new ShowWelcomeMessageListener(
153 $this->container->get('console.translator_manager')
156 $dispatcher->addSubscriber(
157 new DefaultValueEventListener(
158 $this->container->get('console.configuration_manager')
161 $dispatcher->addSubscriber(
162 new ShowTipsListener(
163 $this->container->get('console.translator_manager')
166 $dispatcher->addSubscriber(
167 new CallCommandListener(
168 $this->container->get('console.chain_queue')
171 $dispatcher->addSubscriber(
172 new ShowGeneratedFilesListener(
173 $this->container->get('console.file_queue'),
174 $this->container->get('console.show_file')
177 $dispatcher->addSubscriber(
178 new ShowGenerateInlineListener(
179 $this->container->get('console.translator_manager')
182 $dispatcher->addSubscriber(
183 new ShowGenerateChainListener(
184 $this->container->get('console.translator_manager')
188 $this->setDispatcher($dispatcher);
194 private function addOptions()
196 $this->getDefinition()->addOption(
200 InputOption::VALUE_OPTIONAL,
201 $this->trans('application.options.env'), 'prod'
204 $this->getDefinition()->addOption(
208 InputOption::VALUE_OPTIONAL,
209 $this->trans('application.options.root')
212 $this->getDefinition()->addOption(
216 InputOption::VALUE_NONE,
217 $this->trans('application.options.debug')
220 $this->getDefinition()->addOption(
224 InputOption::VALUE_NONE,
225 $this->trans('application.options.learning')
228 $this->getDefinition()->addOption(
232 InputOption::VALUE_NONE,
233 $this->trans('application.options.generate-chain')
236 $this->getDefinition()->addOption(
240 InputOption::VALUE_NONE,
241 $this->trans('application.options.generate-inline')
244 $this->getDefinition()->addOption(
248 InputOption::VALUE_NONE,
249 $this->trans('application.options.generate-doc')
252 $this->getDefinition()->addOption(
256 InputOption::VALUE_OPTIONAL,
257 $this->trans('application.options.target')
260 $this->getDefinition()->addOption(
264 InputOption::VALUE_REQUIRED,
265 $this->trans('application.options.uri')
268 $this->getDefinition()->addOption(
272 InputOption::VALUE_NONE,
273 $this->trans('application.options.yes')
279 * registerExtendCommands
281 private function registerExtendCommands()
283 $this->container->get('console.configuration_manager')
284 ->loadExtendConfiguration();
288 * registerCommandsFromAutoWireConfiguration
290 private function registerCommandsFromAutoWireConfiguration()
292 $configuration = $this->container->get('console.configuration_manager')
293 ->getConfiguration();
295 $autoWireForcedCommands = $configuration
296 ->get('application.autowire.commands.forced');
298 if (!is_array($autoWireForcedCommands)) {
302 foreach ($autoWireForcedCommands as $autoWireForcedCommand) {
304 if (!$autoWireForcedCommand['class']) {
308 $reflectionClass = new \ReflectionClass(
309 $autoWireForcedCommand['class']
313 if (array_key_exists('arguments', $autoWireForcedCommand)) {
314 foreach ($autoWireForcedCommand['arguments'] as $argument) {
315 $argument = substr($argument, 1);
316 $arguments[] = $this->container->get($argument);
320 $command = $reflectionClass->newInstanceArgs($arguments);
322 if (method_exists($command, 'setTranslator')) {
323 $command->setTranslator(
324 $this->container->get('console.translator_manager')
327 if (method_exists($command, 'setContainer')) {
328 $command->setContainer(
329 $this->container->get('service_container')
333 $this->add($command);
334 } catch (\Exception $e) {
335 echo $e->getMessage() . PHP_EOL;
340 $autoWireNameCommand = $configuration->get(
342 'application.autowire.commands.name.%s',
347 if ($autoWireNameCommand) {
350 if (array_key_exists('arguments', $autoWireNameCommand)) {
351 foreach ($autoWireNameCommand['arguments'] as $argument) {
352 $argument = substr($argument, 1);
353 $arguments[] = $this->container->get($argument);
357 $reflectionClass = new \ReflectionClass(
358 $autoWireNameCommand['class']
360 $command = $reflectionClass->newInstanceArgs($arguments);
362 if (method_exists($command, 'setTranslator')) {
363 $command->setTranslator(
364 $this->container->get('console.translator_manager')
367 if (method_exists($command, 'setContainer')) {
368 $command->setContainer(
369 $this->container->get('service_container')
373 $this->add($command);
374 } catch (\Exception $e) {
375 echo $e->getMessage() . PHP_EOL;
381 * registerChainCommands
383 public function registerChainCommands()
386 * @var ChainDiscovery $chainDiscovery
388 $chainDiscovery = $this->container->get('console.chain_discovery');
389 $chainCommands = $chainDiscovery->getChainCommands();
391 foreach ($chainCommands as $name => $chainCommand) {
393 $file = $chainCommand['file'];
394 $description = $chainCommand['description'];
395 $command = new ChainCustomCommand($name, $description, $file);
396 $this->add($command);
397 } catch (\Exception $e) {
398 echo $e->getMessage() . PHP_EOL;
404 * Finds a command by name or alias.
406 * @param string $name A command name or a command alias
408 * @return mixed A Command instance
410 * Override parent find method to avoid name collisions with automatically
411 * generated command abbreviations.
412 * Command name validation was previously done at doRun method.
414 public function find($name)
416 return $this->get($name);