5 * Contains \Drupal\Console\Utils\Validator.
8 namespace Drupal\Console\Utils;
10 use Drupal\Console\Extension\Manager;
11 use Drupal\Console\Core\Style\DrupalStyle;
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+]/';
27 protected $translatorManager;
32 * @param Manager $extensionManager
33 * @param TranslatorManager $translatorManager
35 public function __construct(
36 Manager $extensionManager,
37 TranslatorManager $translatorManager
39 $this->extensionManager = $extensionManager;
40 $this->translatorManager = $translatorManager;
43 public function validateModuleName($module)
45 if (!empty($module)) {
48 throw new \InvalidArgumentException(sprintf('Module name "%s" is invalid.', $module));
52 public function validateClassName($class_name)
54 if (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
57 throw new \InvalidArgumentException(
59 'Class name "%s" is invalid, it must starts with a letter or underscore, followed by any number of letters, numbers, or underscores.',
66 public function validateBundleTitle($bundle_title)
68 if (!empty($bundle_title)) {
71 throw new \InvalidArgumentException(sprintf('Bundle title "%s" is invalid.', $bundle_title));
75 public function validateCommandName($class_name)
77 if (preg_match(self::REGEX_COMMAND_CLASS_NAME, $class_name)) {
79 } elseif (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
80 throw new \InvalidArgumentException(
82 'Command name "%s" is invalid, it must end with the word \'Command\'',
87 throw new \InvalidArgumentException(
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\'.',
96 public function validateControllerName($class_name)
98 if (preg_match(self::REGEX_CONTROLLER_CLASS_NAME, $class_name)) {
100 } elseif (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
101 throw new \InvalidArgumentException(
103 'Controller name "%s" is invalid, it must end with the word \'Controller\'',
108 throw new \InvalidArgumentException(
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\'.',
117 public function validateMachineName($machine_name)
119 if (preg_match(self::REGEX_MACHINE_NAME, $machine_name)) {
120 return $machine_name;
122 throw new \InvalidArgumentException(
124 'Machine name "%s" is invalid, it must contain only lowercase letters, numbers and underscores.',
131 public function validateModulePath($module_path, $create = false)
133 if (strlen($module_path) > 1 && $module_path[strlen($module_path)-1] == "/") {
134 $module_path = substr($module_path, 0, -1);
137 if (is_dir($module_path)) {
138 chmod($module_path, 0755);
143 if ($create && mkdir($module_path, 0755, true)) {
147 throw new \InvalidArgumentException(
149 'Path "%s" is invalid. You need to provide a valid path.',
155 public function validateMachineNameList($list)
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;
172 $list_checked['fail'][] = $module;
177 return $list_checked;
181 * Validate if service name exist.
183 * @param string $service Service name
184 * @param array $services Array of services
188 public function validateServiceExist($service, $services)
190 if ($service == '') {
194 if (!in_array($service, array_values($services))) {
195 throw new \InvalidArgumentException(sprintf('Service "%s" is invalid.', $service));
202 * Validate if service name exist.
204 * @param string $service Service name
205 * @param array $services Array of services
209 public function validatePluginManagerServiceExist($service, $services)
211 if ($service == '') {
215 if (!in_array($service, array_values($services))) {
216 throw new \InvalidArgumentException(sprintf('Plugin "%s" is invalid.', $service));
223 * Validate if event name exist.
225 * @param string $event Event name
226 * @param array $events Array of events
230 public function validateEventExist($event, $events)
236 if (!in_array($event, array_values($events))) {
237 throw new \InvalidArgumentException(sprintf('Event "%s" is invalid.', $event));
244 * Validates if class name have spaces between words.
246 * @param string $name
250 public function validateSpaces($name)
252 $string = $this->removeSpaces($name);
253 if ($string == $name) {
256 throw new \InvalidArgumentException(
258 'The name "%s" is invalid, spaces between words are not allowed.',
265 public function removeSpaces($name)
267 return preg_replace(self::REGEX_REMOVE_SPACES, '', $name);
274 public function getMissingModules($moduleList)
276 $modules = $this->extensionManager->discoverModules()
283 return array_diff($moduleList, $modules);
290 public function getUninstalledModules($moduleList)
292 $modules = $this->extensionManager->discoverModules()
298 return array_diff($moduleList, $modules);
302 * @param string $extensions_list
303 * @param string $type
304 * @param DrupalStyle $io
308 public function validateExtensions($extensions_list, $type, DrupalStyle $io)
310 $extensions = $this->validateMachineNameList($extensions_list);
311 // Check if all extensions are available
313 $checked_extensions = $this->extensionManager->checkExtensions($extensions['success'], $type);
314 if (!empty($checked_extensions['no_extensions'])) {
317 $this->translatorManager->trans('validator.warnings.extension-unavailable'),
318 implode(', ', $checked_extensions['no_extensions'])
322 $extensions = $extensions['success'];
329 * Validate if http methods exist.
331 * @param array $httpMethods Array http methods.
332 * @param array $availableHttpMethods Array of available http methods.
336 public function validateHttpMethods($httpMethods, $availableHttpMethods)
338 if (empty($httpMethods)) {
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)));
351 * Validates role existence or non existence.
353 * @param string $role
355 * @param array $roles
356 * Array of available roles.
357 * @param bool $checkExistence
358 * To check existence or non existence.
360 * @return string|null
363 private function validateRole($role, $roles, $checkExistence = true)
369 $roleExists = array_key_exists($role, $roles);
370 $condition = $checkExistence ? !$roleExists : $roleExists;
372 $errorMessage = $checkExistence ? "Role %s doesn't exist" : 'Role %s already exists';
373 throw new \InvalidArgumentException(sprintf($errorMessage, $role));
380 * Validate if the role already exists.
382 * @param string $role
384 * @param array $roles
385 * Array of available roles.
387 * @return string|null
390 public function validateRoleExistence($role, $roles) {
391 return $this->validateRole($role, $roles, true);
395 * Validate if the role doesn't exist.
397 * @param string $role
399 * @param array $roles
400 * Array of available roles.
402 * @return string|null
405 public function validateRoleNotExistence($role, $roles) {
406 return $this->validateRole($role, $roles, false);