Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Utils / Validator.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Utils\Validator.
6  */
7
8 namespace Drupal\Console\Utils;
9
10 use Drupal\Console\Extension\Manager;
11 use Drupal\Console\Core\Style\DrupalStyle;
12
13 class Validator
14 {
15     const REGEX_CLASS_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/';
16     const REGEX_COMMAND_CLASS_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+Command$/';
17     const REGEX_CONTROLLER_CLASS_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+Controller$/';
18     const REGEX_MACHINE_NAME = '/^[a-z0-9_]+$/';
19     // This REGEX remove spaces between words
20     const REGEX_REMOVE_SPACES = '/[\\s+]/';
21
22     protected $appRoot;
23
24     /*
25      * TranslatorManager
26      */
27     protected $translatorManager;
28
29     /**
30      * Site constructor.
31      *
32      * @param Manager           $extensionManager
33      * @param TranslatorManager $translatorManager
34      */
35     public function __construct(
36         Manager $extensionManager,
37         TranslatorManager $translatorManager
38     ) {
39         $this->extensionManager = $extensionManager;
40         $this->translatorManager = $translatorManager;
41     }
42
43     public function validateModuleName($module)
44     {
45         if (!empty($module)) {
46             return $module;
47         } else {
48             throw new \InvalidArgumentException(sprintf('Module name "%s" is invalid.', $module));
49         }
50     }
51
52     public function validateClassName($class_name)
53     {
54         if (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
55             return $class_name;
56         } else {
57             throw new \InvalidArgumentException(
58                 sprintf(
59                     'Class name "%s" is invalid, it must starts with a letter or underscore, followed by any number of letters, numbers, or underscores.',
60                     $class_name
61                 )
62             );
63         }
64     }
65
66     public function validateBundleTitle($bundle_title)
67     {
68         if (!empty($bundle_title)) {
69             return $bundle_title;
70         } else {
71             throw new \InvalidArgumentException(sprintf('Bundle title "%s" is invalid.', $bundle_title));
72         }
73     }
74
75     public function validateCommandName($class_name)
76     {
77         if (preg_match(self::REGEX_COMMAND_CLASS_NAME, $class_name)) {
78             return $class_name;
79         } elseif (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
80             throw new \InvalidArgumentException(
81                 sprintf(
82                     'Command name "%s" is invalid, it must end with the word \'Command\'',
83                     $class_name
84                 )
85             );
86         } else {
87             throw new \InvalidArgumentException(
88                 sprintf(
89                     'Command name "%s" is invalid, it must starts with a letter or underscore, followed by any number of letters, numbers, or underscores and then with the word \'Command\'.',
90                     $class_name
91                 )
92             );
93         }
94     }
95
96     public function validateControllerName($class_name)
97     {
98         if (preg_match(self::REGEX_CONTROLLER_CLASS_NAME, $class_name)) {
99             return $class_name;
100         } elseif (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
101             throw new \InvalidArgumentException(
102                 sprintf(
103                     'Controller name "%s" is invalid, it must end with the word \'Controller\'',
104                     $class_name
105                 )
106             );
107         } else {
108             throw new \InvalidArgumentException(
109                 sprintf(
110                     'Controller name "%s" is invalid, it must starts with a letter or underscore, followed by any number of letters, numbers, or underscores and then with the word \'Controller\'.',
111                     $class_name
112                 )
113             );
114         }
115     }
116
117     public function validateMachineName($machine_name)
118     {
119         if (preg_match(self::REGEX_MACHINE_NAME, $machine_name)) {
120             return $machine_name;
121         } else {
122             throw new \InvalidArgumentException(
123                 sprintf(
124                     'Machine name "%s" is invalid, it must contain only lowercase letters, numbers and underscores.',
125                     $machine_name
126                 )
127             );
128         }
129     }
130
131     public function validateModulePath($module_path, $create = false)
132     {
133         if (strlen($module_path) > 1 && $module_path[strlen($module_path)-1] == "/") {
134             $module_path = substr($module_path, 0, -1);
135         }
136
137         if (is_dir($module_path)) {
138             chmod($module_path, 0755);
139             return $module_path;
140         }
141
142
143         if ($create && mkdir($module_path, 0755, true)) {
144             return $module_path;
145         }
146
147         throw new \InvalidArgumentException(
148             sprintf(
149                 'Path "%s" is invalid. You need to provide a valid path.',
150                 $module_path
151             )
152         );
153     }
154
155     public function validateMachineNameList($list)
156     {
157         $list_checked = [
158             'success' => [],
159             'fail' => [],
160         ];
161
162         if (empty($list)) {
163             return [];
164         }
165
166         $list = explode(',', $this->removeSpaces($list));
167         foreach ($list as $key => $module) {
168             if (!empty($module)) {
169                 if (preg_match(self::REGEX_MACHINE_NAME, $module)) {
170                     $list_checked['success'][] = $module;
171                 } else {
172                     $list_checked['fail'][] = $module;
173                 }
174             }
175         }
176
177         return $list_checked;
178     }
179
180     /**
181      * Validate if service name exist.
182      *
183      * @param string $service  Service name
184      * @param array  $services Array of services
185      *
186      * @return string
187      */
188     public function validateServiceExist($service, $services)
189     {
190         if ($service == '') {
191             return null;
192         }
193
194         if (!in_array($service, array_values($services))) {
195             throw new \InvalidArgumentException(sprintf('Service "%s" is invalid.', $service));
196         }
197
198         return $service;
199     }
200
201     /**
202      * Validate if service name exist.
203      *
204      * @param string $service  Service name
205      * @param array  $services Array of services
206      *
207      * @return string
208      */
209     public function validatePluginManagerServiceExist($service, $services)
210     {
211         if ($service == '') {
212             return null;
213         }
214
215         if (!in_array($service, array_values($services))) {
216             throw new \InvalidArgumentException(sprintf('Plugin "%s" is invalid.', $service));
217         }
218
219         return $service;
220     }
221
222     /**
223      * Validate if event name exist.
224      *
225      * @param string $event  Event name
226      * @param array  $events Array of events
227      *
228      * @return string
229      */
230     public function validateEventExist($event, $events)
231     {
232         if ($event == '') {
233             return null;
234         }
235
236         if (!in_array($event, array_values($events))) {
237             throw new \InvalidArgumentException(sprintf('Event "%s" is invalid.', $event));
238         }
239
240         return $event;
241     }
242
243     /**
244      * Validates if class name have spaces between words.
245      *
246      * @param string $name
247      *
248      * @return string
249      */
250     public function validateSpaces($name)
251     {
252         $string = $this->removeSpaces($name);
253         if ($string == $name) {
254             return $name;
255         } else {
256             throw new \InvalidArgumentException(
257                 sprintf(
258                     'The name "%s" is invalid, spaces between words are not allowed.',
259                     $name
260                 )
261             );
262         }
263     }
264
265     public function removeSpaces($name)
266     {
267         return preg_replace(self::REGEX_REMOVE_SPACES, '', $name);
268     }
269
270     /**
271      * @param $moduleList
272      * @return array
273      */
274     public function getMissingModules($moduleList)
275     {
276         $modules = $this->extensionManager->discoverModules()
277             ->showInstalled()
278             ->showUninstalled()
279             ->showNoCore()
280             ->showCore()
281             ->getList(true);
282
283         return array_diff($moduleList, $modules);
284     }
285
286     /**
287      * @param $moduleList
288      * @return array
289      */
290     public function getUninstalledModules($moduleList)
291     {
292         $modules = $this->extensionManager->discoverModules()
293             ->showInstalled()
294             ->showNoCore()
295             ->showCore()
296             ->getList(true);
297
298         return array_diff($moduleList, $modules);
299     }
300
301     /**
302      * @param  string      $extensions_list
303      * @param  string      $type
304      * @param  DrupalStyle $io
305      *
306      * @return array
307      */
308     public function validateExtensions($extensions_list, $type, DrupalStyle $io)
309     {
310         $extensions = $this->validateMachineNameList($extensions_list);
311         // Check if all extensions are available
312         if ($extensions) {
313             $checked_extensions = $this->extensionManager->checkExtensions($extensions['success'], $type);
314             if (!empty($checked_extensions['no_extensions'])) {
315                 $io->warning(
316                     sprintf(
317                         $this->translatorManager->trans('validator.warnings.extension-unavailable'),
318                         implode(', ', $checked_extensions['no_extensions'])
319                     )
320                 );
321             }
322             $extensions = $extensions['success'];
323         }
324
325         return $extensions;
326     }
327
328     /**
329    * Validate if http methods exist.
330    *
331    * @param array $httpMethods          Array http methods.
332    * @param array $availableHttpMethods Array of available http methods.
333    *
334    * @return string
335    */
336     public function validateHttpMethods($httpMethods, $availableHttpMethods)
337     {
338         if (empty($httpMethods)) {
339             return null;
340         }
341
342         $missing_methods = array_diff(array_values($httpMethods), array_keys($availableHttpMethods));
343         if (!empty($missing_methods)) {
344             throw new \InvalidArgumentException(sprintf('HTTP methods "%s" are invalid.', implode(', ', $missing_methods)));
345         }
346
347         return $httpMethods;
348     }
349
350     /**
351      * Validates role existence or non existence.
352      *
353      * @param string $role
354      *   Role machine name.
355      * @param array $roles
356      *   Array of available roles.
357      * @param bool $checkExistence
358      *   To check existence or non existence.
359      *
360      * @return string|null
361      *   Role machine name.
362      */
363     private function validateRole($role, $roles, $checkExistence = true)
364     {
365         if (empty($roles)) {
366             return null;
367         }
368
369         $roleExists = array_key_exists($role, $roles);
370         $condition =  $checkExistence ? !$roleExists : $roleExists;
371         if ($condition) {
372             $errorMessage = $checkExistence ? "Role %s doesn't exist" : 'Role %s already exists';
373             throw new \InvalidArgumentException(sprintf($errorMessage, $role));
374         }
375
376         return $role;
377     }
378
379     /**
380      * Validate if the role already exists.
381      *
382      * @param string $role
383      *   Role machine name.
384      * @param array $roles
385      *   Array of available roles.
386      *
387      * @return string|null
388      *   Role machine name.
389      */
390     public function validateRoleExistence($role, $roles) {
391         return $this->validateRole($role, $roles, true);
392     }
393
394     /**
395      * Validate if the role doesn't exist.
396      *
397      * @param string $role
398      *   Role machine name.
399      * @param array $roles
400      *   Array of available roles.
401      *
402      * @return string|null
403      *   Role machine name.
404      */
405     public function validateRoleNotExistence($role, $roles) {
406         return $this->validateRole($role, $roles, false);
407     }
408 }