More updates to stop using dev or alpha or beta versions.
[yaffs-website] / vendor / drupal / console-extend-plugin / src / ExtenderManager.php
1 <?php
2
3 namespace Drupal\Console\Composer\Plugin;
4
5 use Symfony\Component\Yaml\Yaml;
6 use Symfony\Component\Finder\Finder;
7
8 class ExtenderManager
9 {
10     /**
11      * @var array
12      */
13     protected $configData = [];
14
15     /**
16      * @var array
17      */
18     protected $servicesData = [];
19
20     /**
21      * ExtendExtensionManager constructor.
22      */
23     public function __construct()
24     {
25         $this->init();
26     }
27
28     /**
29      * @param string $composerFile
30      *
31      * @return bool
32      */
33     public function isValidPackageType($composerFile)
34     {
35         if (!is_file($composerFile)) {
36             return false;
37         }
38
39         $composerContent = json_decode(file_get_contents($composerFile), true);
40         if (!$composerContent) {
41             return false;
42         }
43
44         if (!array_key_exists('type', $composerContent)) {
45             return false;
46         }
47
48         return $composerContent['type'] === 'drupal-console-library';
49     }
50
51     /**
52      * @param string $configFile
53      */
54     public function addConfigFile($configFile)
55     {
56         $configData = $this->parseData($configFile);
57         if ($this->isValidConfigData($configData)) {
58             $this->configData = array_merge_recursive(
59                 $configData,
60                 $this->configData
61             );
62         }
63     }
64
65     /**
66      * @param string $servicesFile
67      */
68     public function addServicesFile($servicesFile)
69     {
70         $consoleTags = [
71             'drupal.command',
72             'drupal.generator'
73         ];
74         $servicesData = $this->parseData($servicesFile);
75         if ($this->isValidServicesData($servicesData)) {
76             foreach ($servicesData['services'] as $key => $definition) {
77                 if (!array_key_exists('tags', $definition)) {
78                     continue;
79                 }
80                 $bootstrap = 'install';
81                 foreach ($definition['tags'] as $tags) {
82                     if (!array_key_exists('name', $tags)) {
83                         $bootstrap = null;
84                         continue;
85                     }
86                     if (array_search($tags['name'], $consoleTags) === false) {
87                         $bootstrap = null;
88                         continue;
89                     }
90                     if (array_key_exists('bootstrap', $tags)) {
91                         $bootstrap = $tags['bootstrap'];
92                     }
93                 }
94                 if ($bootstrap) {
95                     $this->servicesData[$bootstrap]['services'][$key] = $definition;
96                 }
97             }
98         }
99     }
100
101     /**
102      * init
103      */
104     private function init()
105     {
106         $this->configData = [];
107         $this->servicesData = [];
108     }
109
110     /**
111      * @param $file
112      * @return array|mixed
113      */
114     private function parseData($file)
115     {
116         if (!file_exists($file)) {
117             return [];
118         }
119
120         $data = Yaml::parse(
121             file_get_contents($file)
122         );
123
124         if (!$data) {
125             return [];
126         }
127
128         return $data;
129     }
130
131     public function processProjectPackages($directory)
132     {
133         $finder = new Finder();
134         $finder->files()
135             ->name('composer.json')
136             ->contains('drupal-console-library')
137             ->in($directory)
138             ->ignoreUnreadableDirs();
139
140         foreach ($finder as $file) {
141             $this->processComposerFile($file->getPathName());
142         }
143     }
144
145     /**
146      * @param $composerFile
147      */
148     private function processComposerFile($composerFile)
149     {
150         $packageDirectory = dirname($composerFile);
151
152         $configFile = $packageDirectory.'/console.config.yml';
153         $this->addConfigFile($configFile);
154
155         $servicesFile = $packageDirectory.'/console.services.yml';
156         $this->addServicesFile($servicesFile);
157     }
158
159     /**
160      * @param array $configData
161      *
162      * @return boolean
163      */
164     private function isValidConfigData($configData)
165     {
166         if (!$configData) {
167             return false;
168         }
169
170         if (!array_key_exists('application', $configData)) {
171             return false;
172         }
173
174         if (!array_key_exists('autowire', $configData['application'])) {
175             return false;
176         }
177
178         if (!array_key_exists('commands', $configData['application']['autowire'])) {
179             return false;
180         }
181
182         return true;
183     }
184
185     /**
186      * @param array $servicesData
187      *
188      * @return boolean
189      */
190     private function isValidServicesData($servicesData)
191     {
192         if (!$servicesData) {
193             return false;
194         }
195
196         if (!array_key_exists('services', $servicesData)) {
197             return false;
198         }
199
200         return true;
201     }
202
203     /**
204      * @return array
205      */
206     public function getConfigData()
207     {
208         return $this->configData;
209     }
210
211     /**
212      * @return array
213      */
214     public function getServicesData()
215     {
216         return $this->servicesData;
217     }
218 }