Version 1
[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         $servicesData = $this->parseData($servicesFile);
71         if ($this->isValidServicesData($servicesData)) {
72             foreach ($servicesData['services'] as $key => $definition) {
73                 if (!array_key_exists('tags', $definition)) {
74                     continue;
75                 }
76                 $bootstrap = 'install';
77                 foreach ($definition['tags'] as $tags) {
78                     if (!array_key_exists('name', $tags)) {
79                         $bootstrap = null;
80                         continue;
81                     }
82                     if ($tags['name'] != 'drupal.command') {
83                         $bootstrap = null;
84                         continue;
85                     }
86                     if (array_key_exists('bootstrap', $tags)) {
87                         $bootstrap = $tags['bootstrap'];
88                     }
89                 }
90                 if ($bootstrap) {
91                     $this->servicesData[$bootstrap]['services'][$key] = $definition;
92                 }
93             }
94         }
95     }
96
97     /**
98      * init
99      */
100     private function init()
101     {
102         $this->configData = [];
103         $this->servicesData = [];
104     }
105
106     /**
107      * @param $file
108      * @return array|mixed
109      */
110     private function parseData($file)
111     {
112         if (!file_exists($file)) {
113             return [];
114         }
115
116         $data = Yaml::parse(
117             file_get_contents($file)
118         );
119
120         if (!$data) {
121             return [];
122         }
123
124         return $data;
125     }
126
127     public function processProjectPackages($directory)
128     {
129         $finder = new Finder();
130         $finder->files()
131             ->name('composer.json')
132             ->contains('drupal-console-library')
133             ->in($directory);
134
135         foreach ($finder as $file) {
136             $this->processComposerFile($file->getPathName());
137         }
138     }
139
140     /**
141      * @param $composerFile
142      */
143     private function processComposerFile($composerFile)
144     {
145         $packageDirectory = dirname($composerFile);
146
147         $configFile = $packageDirectory.'/console.config.yml';
148         $this->addConfigFile($configFile);
149
150         $servicesFile = $packageDirectory.'/console.services.yml';
151         $this->addServicesFile($servicesFile);
152     }
153
154     /**
155      * @param array $configData
156      *
157      * @return boolean
158      */
159     private function isValidConfigData($configData)
160     {
161         if (!$configData) {
162             return false;
163         }
164
165         if (!array_key_exists('application', $configData)) {
166             return false;
167         }
168
169         if (!array_key_exists('autowire', $configData['application'])) {
170             return false;
171         }
172
173         if (!array_key_exists('commands', $configData['application']['autowire'])) {
174             return false;
175         }
176
177         return true;
178     }
179
180     /**
181      * @param array $servicesData
182      *
183      * @return boolean
184      */
185     private function isValidServicesData($servicesData)
186     {
187         if (!$servicesData) {
188             return false;
189         }
190
191         if (!array_key_exists('services', $servicesData)) {
192             return false;
193         }
194
195         return true;
196     }
197
198     /**
199      * @return array
200      */
201     public function getConfigData()
202     {
203         return $this->configData;
204     }
205
206     /**
207      * @return array
208      */
209     public function getServicesData()
210     {
211         return $this->servicesData;
212     }
213 }