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 = [
51 private $skipArguments = [
57 * ShowGenerateInlineListener constructor.
59 * @param TranslatorManagerInterface $translator
61 public function __construct(
62 TranslatorManagerInterface $translator
64 $this->translator = $translator;
68 * @param ConsoleTerminateEvent $event
70 public function showGenerateInline(ConsoleTerminateEvent $event)
72 if ($event->getExitCode() != 0) {
76 /* @var Command $command */
77 $command = $event->getCommand();
78 /* @var DrupalStyle $io */
79 $io = new DrupalStyle($event->getInput(), $event->getOutput());
81 $command_name = $command->getName();
83 $this->skipArguments[] = $command_name;
85 if (in_array($command->getName(), $this->skipCommands)) {
89 $input = $event->getInput();
90 if ($input->getOption('generate-inline')) {
91 $options = array_filter($input->getOptions());
92 foreach ($this->skipOptions as $remove_option) {
93 unset($options[$remove_option]);
96 $arguments = array_filter($input->getArguments());
97 foreach ($this->skipArguments as $remove_argument) {
98 unset($arguments[$remove_argument]);
102 foreach ($arguments as $argument_id => $argument) {
103 if (is_array($argument)) {
104 $argument = implode(" ", $argument);
105 } elseif (strstr($argument, ' ')) {
106 $argument = '"' . $argument . '"';
109 $inline .= " $argument";
112 // Refactor and remove nested levels. Then apply to arguments.
113 foreach ($options as $optionName => $optionValue) {
114 if (is_array($optionValue)) {
115 foreach ($optionValue as $optionItem) {
116 if (is_array($optionItem)) {
117 $inlineValue = implode(
120 return $k . ':' . $v;
123 array_keys($optionItem)
127 $inlineValue = $optionItem;
129 $inline .= ' --' . $optionName . '="' . $inlineValue . '"';
132 if (is_bool($optionValue)) {
133 $inline.= ' --' . $optionName;
135 $inline.= ' --' . $optionName . '="' . $optionValue . '"';
140 // Print YML output and message
142 $this->translator->trans('application.messages.inline.generated')
158 public static function getSubscribedEvents()
160 return [ConsoleEvents::TERMINATE => 'showGenerateInline'];