5 * Contains \Drupal\Console\Core\EventSubscriber\ShowGenerateInlineListener.
8 namespace Drupal\Console\Core\EventSubscriber;
10 use Symfony\Component\Console\ConsoleEvents;
11 use Symfony\Component\Console\Event\ConsoleTerminateEvent;
12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Core\Utils\TranslatorManagerInterface;
15 use Drupal\Console\Core\Style\DrupalStyle;
18 * Class ShowGenerateInlineListener
20 * @package Drupal\Console\Core\EventSubscriber
22 class ShowGenerateInlineListener implements EventSubscriberInterface
25 * @var TranslatorManagerInterface
27 protected $translator;
32 private $skipCommands = [
42 private $skipOptions = [
52 private $skipArguments = [
58 * ShowGenerateInlineListener constructor.
60 * @param TranslatorManagerInterface $translator
62 public function __construct(
63 TranslatorManagerInterface $translator
65 $this->translator = $translator;
69 * @param ConsoleTerminateEvent $event
71 public function showGenerateInline(ConsoleTerminateEvent $event)
73 if ($event->getExitCode() != 0) {
77 /* @var Command $command */
78 $command = $event->getCommand();
79 /* @var DrupalStyle $io */
80 $io = new DrupalStyle($event->getInput(), $event->getOutput());
82 $command_name = $command->getName();
84 $this->skipArguments[] = $command_name;
86 if (in_array($command->getName(), $this->skipCommands)) {
90 $input = $event->getInput();
91 if ($input->getOption('generate-inline')) {
92 $options = array_filter($input->getOptions());
93 foreach ($this->skipOptions as $remove_option) {
94 unset($options[$remove_option]);
97 $arguments = array_filter($input->getArguments());
98 foreach ($this->skipArguments as $remove_argument) {
99 unset($arguments[$remove_argument]);
103 foreach ($arguments as $argument_id => $argument) {
104 if (is_array($argument)) {
105 $argument = implode(" ", $argument);
106 } elseif (strstr($argument, ' ')) {
107 $argument = '"' . $argument . '"';
110 $inline .= " $argument";
113 // Refactor and remove nested levels. Then apply to arguments.
114 foreach ($options as $optionName => $optionValue) {
115 if (is_array($optionValue)) {
116 foreach ($optionValue as $optionItem) {
117 if (is_array($optionItem)) {
118 $inlineValue = implode(
121 return '"'.$k . '":"' . $v . '"';
124 array_keys($optionItem)
128 $inlineValue = $optionItem;
130 $inline .= ' --' . $optionName . '=\'' . $inlineValue . '\'';
133 if (is_bool($optionValue)) {
134 $inline.= ' --' . $optionName;
136 $inline.= ' --' . $optionName . '="' . $optionValue . '"';
141 // Print YML output and message
143 $this->translator->trans('application.messages.inline.generated')
148 '$ drupal %s %s --no-interaction',
159 public static function getSubscribedEvents()
161 return [ConsoleEvents::TERMINATE => 'showGenerateInline'];