Yaffs site version 1.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  *
19  * @package Drupal\Console\Core\EventSubscriber
20  */
21 class DefaultValueEventListener implements EventSubscriberInterface
22 {
23     /**
24      * @var ConfigurationManager
25      */
26     protected $configurationManager;
27
28     /**
29      * @var array
30      */
31     private $skipCommands = [
32         'self-update',
33         'list',
34         'chain',
35         'drush'
36     ];
37
38     /**
39      * DefaultValueEventListener constructor.
40      *
41      * @param ConfigurationManager $configurationManager
42      */
43     public function __construct(
44         ConfigurationManager $configurationManager
45     ) {
46         $this->configurationManager = $configurationManager;
47     }
48
49     /**
50      * @param ConsoleCommandEvent $event
51      */
52     public function setDefaultValues(ConsoleCommandEvent $event)
53     {
54         /* @var Command $command */
55         $command = $event->getCommand();
56         $configuration = $this->configurationManager
57             ->getConfiguration();
58
59         if (in_array($command->getName(), $this->skipCommands)) {
60             return;
61         }
62
63         $input = $command->getDefinition();
64         $options = $input->getOptions();
65         foreach ($options as $key => $option) {
66             $defaultOption = sprintf(
67                 'application.default.commands.%s.options.%s',
68                 str_replace(':', '.', $command->getName()),
69                 $key
70             );
71             $defaultValue = $configuration->get($defaultOption);
72             if ($defaultValue) {
73                 $option->setDefault($defaultValue);
74             }
75         }
76
77         $arguments = $input->getArguments();
78         foreach ($arguments as $key => $argument) {
79             $defaultArgument = sprintf(
80                 'application.default.commands.%s.arguments.%s',
81                 str_replace(':', '.', $command->getName()),
82                 $key
83             );
84             $defaultValue = $configuration->get($defaultArgument);
85             if ($defaultValue) {
86                 $argument->setDefault($defaultValue);
87             }
88         }
89     }
90
91     /**
92      * @{@inheritdoc}
93      */
94     public static function getSubscribedEvents()
95     {
96         return [ConsoleEvents::COMMAND => 'setDefaultValues'];
97     }
98 }