Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Application.php
1 <?php
2
3 namespace Drupal\Console;
4
5 use Doctrine\Common\Annotations\AnnotationRegistry;
6 use Symfony\Component\Console\Input\InputInterface;
7 use Symfony\Component\Console\Output\OutputInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 use Drupal\Console\Annotations\DrupalCommandAnnotationReader;
10 use Drupal\Console\Utils\AnnotationValidator;
11 use Drupal\Console\Core\Application as BaseApplication;
12
13 /**
14  * Class Application
15  *
16  * @package Drupal\Console
17  */
18 class Application extends BaseApplication
19 {
20     /**
21      * @var string
22      */
23     const NAME = 'Drupal Console';
24
25     /**
26      * @var string
27      */
28     const VERSION = '1.6.1';
29
30     public function __construct(ContainerInterface $container)
31     {
32         parent::__construct($container, $this::NAME, $this::VERSION);
33     }
34
35     /**
36      * Returns the long version of the application.
37      *
38      * @return string The long application version
39      */
40     public function getLongVersion()
41     {
42         $output = '';
43
44         if ('UNKNOWN' !== $this->getName()) {
45             if ('UNKNOWN' !== $this->getVersion()) {
46                 $output .= sprintf('<info>%s</info> version <comment>%s</comment>', $this->getName(), $this->getVersion());
47             } else {
48                 $output .= sprintf('<info>%s</info>', $this->getName());
49             }
50         } else {
51             $output .= '<info>Drupal Console</info>';
52         }
53
54         return $output;
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     public function doRun(InputInterface $input, OutputInterface $output)
61     {
62         $this->validateCommands();
63
64         return parent::doRun($input, $output);
65     }
66
67     public function validateCommands()
68     {
69         $consoleCommands = $this->container
70             ->findTaggedServiceIds('drupal.command');
71
72         if (!$consoleCommands) {
73             return;
74         }
75
76         $serviceDefinitions = $this->container->getDefinitions();
77
78         if (!$serviceDefinitions) {
79             return;
80         }
81
82         if (!$this->container->has('console.annotation_command_reader')) {
83             return;
84         }
85
86         /**
87          * @var DrupalCommandAnnotationReader $annotationCommandReader
88          */
89         $annotationCommandReader = $this->container
90             ->get('console.annotation_command_reader');
91
92         if (!$this->container->has('console.annotation_validator')) {
93             return;
94         }
95
96         /**
97          * @var AnnotationValidator $annotationValidator
98          */
99         $annotationValidator = $this->container
100             ->get('console.annotation_validator');
101
102         $invalidCommands = [];
103
104         foreach ($consoleCommands as $name => $tags) {
105             AnnotationRegistry::reset();
106             AnnotationRegistry::registerLoader(
107                 [
108                     $this->container->get('class_loader'),
109                     "loadClass"
110                 ]
111             );
112
113             if (!$this->container->has($name)) {
114                 $invalidCommands[] = $name;
115                 continue;
116             }
117
118             if (!$serviceDefinition = $serviceDefinitions[$name]) {
119                 $invalidCommands[] = $name;
120                 continue;
121             }
122
123             if (!$annotationValidator->isValidCommand(
124                 $serviceDefinition->getClass()
125             )
126             ) {
127                 $invalidCommands[] = $name;
128                 continue;
129             }
130
131             $annotation = $annotationCommandReader
132                 ->readAnnotation($serviceDefinition->getClass());
133             if ($annotation) {
134                 $this->container->get('console.translator_manager')
135                     ->addResourceTranslationsByExtension(
136                         $annotation['extension'],
137                         $annotation['extensionType']
138                     );
139             }
140         }
141
142         $this->container
143             ->get('console.key_value_storage')
144             ->set('invalid_commands', $invalidCommands);
145
146         return;
147     }
148 }