Yaffs site version 1.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             ->ignoreUnreadableDirs();
135
136         foreach ($finder as $file) {
137             $this->processComposerFile($file->getPathName());
138         }
139     }
140
141     /**
142      * @param $composerFile
143      */
144     private function processComposerFile($composerFile)
145     {
146         $packageDirectory = dirname($composerFile);
147
148         $configFile = $packageDirectory.'/console.config.yml';
149         $this->addConfigFile($configFile);
150
151         $servicesFile = $packageDirectory.'/console.services.yml';
152         $this->addServicesFile($servicesFile);
153     }
154
155     /**
156      * @param array $configData
157      *
158      * @return boolean
159      */
160     private function isValidConfigData($configData)
161     {
162         if (!$configData) {
163             return false;
164         }
165
166         if (!array_key_exists('application', $configData)) {
167             return false;
168         }
169
170         if (!array_key_exists('autowire', $configData['application'])) {
171             return false;
172         }
173
174         if (!array_key_exists('commands', $configData['application']['autowire'])) {
175             return false;
176         }
177
178         return true;
179     }
180
181     /**
182      * @param array $servicesData
183      *
184      * @return boolean
185      */
186     private function isValidServicesData($servicesData)
187     {
188         if (!$servicesData) {
189             return false;
190         }
191
192         if (!array_key_exists('services', $servicesData)) {
193             return false;
194         }
195
196         return true;
197     }
198
199     /**
200      * @return array
201      */
202     public function getConfigData()
203     {
204         return $this->configData;
205     }
206
207     /**
208      * @return array
209      */
210     public function getServicesData()
211     {
212         return $this->servicesData;
213     }
214 }