5 * Contains \Drupal\Console\Core\EventSubscriber\DefaultValueEventListener.
8 namespace Drupal\Console\Core\EventSubscriber;
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;
17 * Class DefaultValueEventListener
19 * @package Drupal\Console\Core\EventSubscriber
21 class DefaultValueEventListener implements EventSubscriberInterface
24 * @var ConfigurationManager
26 protected $configurationManager;
31 private $skipCommands = [
39 * DefaultValueEventListener constructor.
41 * @param ConfigurationManager $configurationManager
43 public function __construct(
44 ConfigurationManager $configurationManager
46 $this->configurationManager = $configurationManager;
50 * @param ConsoleCommandEvent $event
52 public function setDefaultValues(ConsoleCommandEvent $event)
54 /* @var Command $command */
55 $command = $event->getCommand();
56 $configuration = $this->configurationManager
59 if (in_array($command->getName(), $this->skipCommands)) {
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()),
71 $defaultValue = $configuration->get($defaultOption);
73 $option->setDefault($defaultValue);
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()),
84 $defaultValue = $configuration->get($defaultArgument);
86 $argument->setDefault($defaultValue);
94 public static function getSubscribedEvents()
96 return [ConsoleEvents::COMMAND => 'setDefaultValues'];