Version 1
[yaffs-website] / vendor / drupal / console-core / src / EventSubscriber / DefaultValueEventListener.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\EventSubscriber\DefaultValueEventListener.
6  */
7
8 namespace Drupal\Console\Core\EventSubscriber;
9
10 use Symfony\Component\Console\ConsoleEvents;
11 use Symfony\Component\Console\Event\ConsoleCommandEvent;
12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Core\Utils\ConfigurationManager;
15
16 /**
17  * Class DefaultValueEventListener
18  * @package Drupal\Console\Core\EventSubscriber
19  */
20 class DefaultValueEventListener implements EventSubscriberInterface
21 {
22     /**
23      * @var ConfigurationManager
24      */
25     protected $configurationManager;
26
27     /**
28      * @var array
29      */
30     private $skipCommands = [
31         'self-update',
32         'list',
33         'chain',
34         'drush'
35     ];
36
37     /**
38      * DefaultValueEventListener constructor.
39      * @param ConfigurationManager $configurationManager
40      */
41     public function __construct(
42         ConfigurationManager $configurationManager
43     ) {
44         $this->configurationManager = $configurationManager;
45     }
46
47     /**
48      * @param ConsoleCommandEvent $event
49      */
50     public function setDefaultValues(ConsoleCommandEvent $event)
51     {
52         /* @var Command $command */
53         $command = $event->getCommand();
54         $configuration = $this->configurationManager
55             ->getConfiguration();
56
57         if (in_array($command->getName(), $this->skipCommands)) {
58             return;
59         }
60
61         $input = $command->getDefinition();
62         $options = $input->getOptions();
63         foreach ($options as $key => $option) {
64             $defaultOption = sprintf(
65                 'application.default.commands.%s.options.%s',
66                 str_replace(':', '.', $command->getName()),
67                 $key
68             );
69             $defaultValue = $configuration->get($defaultOption);
70             if ($defaultValue) {
71                 $option->setDefault($defaultValue);
72             }
73         }
74
75         $arguments = $input->getArguments();
76         foreach ($arguments as $key => $argument) {
77             $defaultArgument = sprintf(
78                 'application.default.commands.%s.arguments.%s',
79                 str_replace(':', '.', $command->getName()),
80                 $key
81             );
82             $defaultValue = $configuration->get($defaultArgument);
83             if ($defaultValue) {
84                 $argument->setDefault($defaultValue);
85             }
86         }
87     }
88
89     /**
90      * @{@inheritdoc}
91      */
92     public static function getSubscribedEvents()
93     {
94         return [ConsoleEvents::COMMAND => 'setDefaultValues'];
95     }
96 }