Yaffs site version 1.1
[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_MACHINE_NAME = '/^[a-z0-9_]+$/';
18     // This REGEX remove spaces between words
19     const REGEX_REMOVE_SPACES = '/[\\s+]/';
20
21     protected $appRoot;
22
23     /*
24      * TranslatorManager
25      */
26     protected $translatorManager;
27
28     /**
29      * Site constructor.
30      *
31      * @param Manager           $extensionManager
32      * @param TranslatorManager $translatorManager
33      */
34     public function __construct(
35         Manager $extensionManager,
36         TranslatorManager $translatorManager
37     ) {
38         $this->extensionManager = $extensionManager;
39         $this->translatorManager = $translatorManager;
40     }
41
42     public function validateModuleName($module)
43     {
44         if (!empty($module)) {
45             return $module;
46         } else {
47             throw new \InvalidArgumentException(sprintf('Module name "%s" is invalid.', $module));
48         }
49     }
50
51     public function validateClassName($class_name)
52     {
53         if (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
54             return $class_name;
55         } else {
56             throw new \InvalidArgumentException(
57                 sprintf(
58                     'Class name "%s" is invalid, it must starts with a letter or underscore, followed by any number of letters, numbers, or underscores.',
59                     $class_name
60                 )
61             );
62         }
63     }
64
65     public function validateBundleTitle($bundle_title)
66     {
67         if (!empty($bundle_title)) {
68             return $bundle_title;
69         } else {
70             throw new \InvalidArgumentException(sprintf('Bundle title "%s" is invalid.', $bundle_title));
71         }
72     }
73
74     public function validateCommandName($class_name)
75     {
76         if (preg_match(self::REGEX_COMMAND_CLASS_NAME, $class_name)) {
77             return $class_name;
78         } elseif (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
79             throw new \InvalidArgumentException(
80                 sprintf(
81                     'Command name "%s" is invalid, it must end with the word \'Command\'',
82                     $class_name
83                 )
84             );
85         } else {
86             throw new \InvalidArgumentException(
87                 sprintf(
88                     '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\'.',
89                     $class_name
90                 )
91             );
92         }
93     }
94
95     public function validateMachineName($machine_name)
96     {
97         if (preg_match(self::REGEX_MACHINE_NAME, $machine_name)) {
98             return $machine_name;
99         } else {
100             throw new \InvalidArgumentException(
101                 sprintf(
102                     'Machine name "%s" is invalid, it must contain only lowercase letters, numbers and underscores.',
103                     $machine_name
104                 )
105             );
106         }
107     }
108
109     public function validateModulePath($module_path, $create = false)
110     {
111         if (!is_dir($module_path)) {
112             if ($create && mkdir($module_path, 0755, true)) {
113                 return $module_path;
114             }
115
116             throw new \InvalidArgumentException(
117                 sprintf(
118                     'Module path "%s" is invalid. You need to provide a valid path.',
119                     $module_path
120                 )
121             );
122         }
123
124         return $module_path;
125     }
126
127     public function validateMachineNameList($list)
128     {
129         $list_checked = [
130           'success' => [],
131           'fail' => [],
132         ];
133
134         if (empty($list)) {
135             return [];
136         }
137
138         $list = explode(',', $this->removeSpaces($list));
139         foreach ($list as $key => $module) {
140             if (!empty($module)) {
141                 if (preg_match(self::REGEX_MACHINE_NAME, $module)) {
142                     $list_checked['success'][] = $module;
143                 } else {
144                     $list_checked['fail'][] = $module;
145                 }
146             }
147         }
148
149         return $list_checked;
150     }
151
152     /**
153      * Validate if service name exist.
154      *
155      * @param string $service  Service name
156      * @param array  $services Array of services
157      *
158      * @return string
159      */
160     public function validateServiceExist($service, $services)
161     {
162         if ($service == '') {
163             return null;
164         }
165
166         if (!in_array($service, array_values($services))) {
167             throw new \InvalidArgumentException(sprintf('Service "%s" is invalid.', $service));
168         }
169
170         return $service;
171     }
172
173     /**
174      * Validate if service name exist.
175      *
176      * @param string $service  Service name
177      * @param array  $services Array of services
178      *
179      * @return string
180      */
181     public function validatePluginManagerServiceExist($service, $services)
182     {
183         if ($service == '') {
184             return null;
185         }
186
187         if (!in_array($service, array_values($services))) {
188             throw new \InvalidArgumentException(sprintf('Plugin "%s" is invalid.', $service));
189         }
190
191         return $service;
192     }
193
194     /**
195      * Validate if event name exist.
196      *
197      * @param string $event  Event name
198      * @param array  $events Array of events
199      *
200      * @return string
201      */
202     public function validateEventExist($event, $events)
203     {
204         if ($event == '') {
205             return null;
206         }
207
208         if (!in_array($event, array_values($events))) {
209             throw new \InvalidArgumentException(sprintf('Event "%s" is invalid.', $event));
210         }
211
212         return $event;
213     }
214
215     /**
216      * Validates if class name have spaces between words.
217      *
218      * @param string $name
219      *
220      * @return string
221      */
222     public function validateSpaces($name)
223     {
224         $string = $this->removeSpaces($name);
225         if ($string == $name) {
226             return $name;
227         } else {
228             throw new \InvalidArgumentException(
229                 sprintf(
230                     'The name "%s" is invalid, spaces between words are not allowed.',
231                     $name
232                 )
233             );
234         }
235     }
236
237     public function removeSpaces($name)
238     {
239         return preg_replace(self::REGEX_REMOVE_SPACES, '', $name);
240     }
241
242     /**
243      * @param $moduleList
244      * @return array
245      */
246     public function getMissingModules($moduleList)
247     {
248         $modules = $this->extensionManager->discoverModules()
249             ->showInstalled()
250             ->showUninstalled()
251             ->showNoCore()
252             ->showCore()
253             ->getList(true);
254
255         return array_diff($moduleList, $modules);
256     }
257
258     /**
259      * @param $moduleList
260      * @return array
261      */
262     public function getUninstalledModules($moduleList)
263     {
264         $modules = $this->extensionManager->discoverModules()
265             ->showInstalled()
266             ->showNoCore()
267             ->showCore()
268             ->getList(true);
269
270         return array_diff($moduleList, $modules);
271     }
272
273     /**
274      * @param  string      $extensions_list
275      * @param  string      $type
276      * @param  DrupalStyle $io
277      *
278      * @return array
279      */
280     public function validateExtensions($extensions_list, $type, DrupalStyle $io)
281     {
282         $extensions = $this->validateMachineNameList($extensions_list);
283         // Check if all extensions are available
284         if ($extensions) {
285             $checked_extensions = $this->extensionManager->checkExtensions($extensions['success'], $type);
286             if (!empty($checked_extensions['no_extensions'])) {
287                 $io->warning(
288                     sprintf(
289                         $this->translatorManager->trans('validator.warnings.extension-unavailable'),
290                         implode(', ', $checked_extensions['no_extensions'])
291                     )
292                 );
293             }
294             $extensions = $extensions['success'];
295         }
296
297         return $extensions;
298     }
299 }