Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Utils / AnnotationValidator.php
1 <?php
2
3 namespace Drupal\Console\Utils;
4
5 use Drupal\Console\Annotations\DrupalCommandAnnotationReader;
6 use Drupal\Console\Extension\Manager;
7
8 /**
9  * Class AnnotationValidator
10  *
11  * @package Drupal\Console\Utils
12  */
13 class AnnotationValidator
14 {
15     /**
16      * @var DrupalCommandAnnotationReader
17      */
18     protected $annotationCommandReader;
19     /**
20      * @var Manager
21      */
22     protected $extensionManager;
23     /**
24      * @var array
25      */
26     private $extensions = [];
27
28     /**
29      * AnnotationValidator constructor.
30      *
31      * @param DrupalCommandAnnotationReader $annotationCommandReader
32      * @param Manager                       $extensionManager
33      */
34     public function __construct(
35         DrupalCommandAnnotationReader $annotationCommandReader,
36         Manager $extensionManager
37     ) {
38         $this->annotationCommandReader = $annotationCommandReader;
39         $this->extensionManager = $extensionManager;
40     }
41
42     /**
43      * @param $class
44      * @return bool
45      */
46     public function isValidCommand($class)
47     {
48         $annotation = $this->annotationCommandReader->readAnnotation($class);
49         if (!$annotation) {
50             return true;
51         }
52
53         $dependencies = $this->extractDependencies($annotation);
54
55         if (!$dependencies) {
56             return true;
57         }
58
59         foreach ($dependencies as $dependency) {
60             if (!$this->isExtensionInstalled($dependency)) {
61                 return false;
62             }
63         }
64
65         return true;
66     }
67
68     /**
69      * @param $extension
70      * @return bool
71      */
72     protected function isExtensionInstalled($extension)
73     {
74         if (!$this->extensions) {
75             $modules = $this->extensionManager->discoverModules()
76                 ->showCore()
77                 ->showNoCore()
78                 ->showInstalled()
79                 ->getList(true);
80
81             $themes = $this->extensionManager->discoverThemes()
82                 ->showCore()
83                 ->showNoCore()
84                 ->showInstalled()
85                 ->getList(true);
86
87             $profiles = $this->extensionManager->discoverProfiles()
88                 ->showCore()
89                 ->showNoCore()
90                 ->showInstalled()
91                 ->getList(true);
92
93             $this->extensions = array_merge(
94                 $modules,
95                 $themes,
96                 $profiles
97             );
98         }
99
100         return in_array($extension, $this->extensions);
101     }
102
103     /**
104      * @param $annotation
105      * @return array
106      */
107     protected function extractDependencies($annotation)
108     {
109         $dependencies = [];
110         if (array_key_exists('extension', $annotation)) {
111             $dependencies[] = $annotation['extension'];
112         }
113         if (array_key_exists('dependencies', $annotation)) {
114             $dependencies = array_merge($dependencies, $annotation['dependencies']);
115         }
116
117         return $dependencies;
118     }
119 }