6f6e14dd70ff93b7c0e6b8a88dff56c61d79893b
[yaffs-website] / vendor / drupal / console-core / src / EventSubscriber / ValidateExecutionListener.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\EventSubscriber\ValidateDependenciesListener.
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 use Drupal\Console\Core\Utils\TranslatorManagerInterface;
16 use Drupal\Console\Core\Style\DrupalStyle;
17
18 /**
19  * Class ValidateExecutionListener
20  *
21  * @package Drupal\Console\Core\EventSubscriber
22  */
23 class ValidateExecutionListener implements EventSubscriberInterface
24 {
25     /**
26      * @var TranslatorManagerInterface
27      */
28     protected $translator;
29
30     /**
31      * @var ConfigurationManager
32      */
33     protected $configurationManager;
34
35     /**
36      * ValidateExecutionListener constructor.
37      *
38      * @param TranslatorManagerInterface $translator
39      * @param ConfigurationManager       $configurationManager
40      */
41     public function __construct(
42         TranslatorManagerInterface $translator,
43         ConfigurationManager $configurationManager
44     ) {
45         $this->translator = $translator;
46         $this->configurationManager = $configurationManager;
47     }
48
49     /**
50      * @param ConsoleCommandEvent $event
51      */
52     public function validateExecution(ConsoleCommandEvent $event)
53     {
54         /* @var Command $command */
55         $command = $event->getCommand();
56         /* @var DrupalStyle $io */
57         $io = new DrupalStyle($event->getInput(), $event->getOutput());
58
59         $configuration = $this->configurationManager->getConfiguration();
60
61         $mapping = $configuration->get('application.disable.commands')?:[];
62         if (array_key_exists($command->getName(), $mapping)) {
63             $extra = $mapping[$command->getName()];
64             $message[] = sprintf(
65                 $this->translator->trans('application.messages.disable.command.error'),
66                 $command->getName()
67             );
68             if ($extra) {
69                 $message[] =  sprintf(
70                     $this->translator->trans('application.messages.disable.command.extra'),
71                     $extra
72                 );
73             }
74             $io->commentBlock($message);
75         }
76     }
77
78     /**
79      * @{@inheritdoc}
80      */
81     public static function getSubscribedEvents()
82     {
83         return [ConsoleEvents::COMMAND => 'validateExecution'];
84     }
85 }