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
18 * @package Drupal\Console\Core\EventSubscriber
20 class DefaultValueEventListener implements EventSubscriberInterface
23 * @var ConfigurationManager
25 protected $configurationManager;
30 private $skipCommands = [
38 * DefaultValueEventListener constructor.
39 * @param ConfigurationManager $configurationManager
41 public function __construct(
42 ConfigurationManager $configurationManager
44 $this->configurationManager = $configurationManager;
48 * @param ConsoleCommandEvent $event
50 public function setDefaultValues(ConsoleCommandEvent $event)
52 /* @var Command $command */
53 $command = $event->getCommand();
54 $configuration = $this->configurationManager
57 if (in_array($command->getName(), $this->skipCommands)) {
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()),
69 $defaultValue = $configuration->get($defaultOption);
71 $option->setDefault($defaultValue);
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()),
82 $defaultValue = $configuration->get($defaultArgument);
84 $argument->setDefault($defaultValue);
92 public static function getSubscribedEvents()
94 return [ConsoleEvents::COMMAND => 'setDefaultValues'];