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_MACHINE_NAME = '/^[a-z0-9_]+$/';
18 // This REGEX remove spaces between words
19 const REGEX_REMOVE_SPACES = '/[\\s+]/';
26 protected $translatorManager;
31 * @param Manager $extensionManager
32 * @param TranslatorManager $translatorManager
34 public function __construct(
35 Manager $extensionManager,
36 TranslatorManager $translatorManager
38 $this->extensionManager = $extensionManager;
39 $this->translatorManager = $translatorManager;
42 public function validateModuleName($module)
44 if (!empty($module)) {
47 throw new \InvalidArgumentException(sprintf('Module name "%s" is invalid.', $module));
51 public function validateClassName($class_name)
53 if (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
56 throw new \InvalidArgumentException(
58 'Class name "%s" is invalid, it must starts with a letter or underscore, followed by any number of letters, numbers, or underscores.',
65 public function validateBundleTitle($bundle_title)
67 if (!empty($bundle_title)) {
70 throw new \InvalidArgumentException(sprintf('Bundle title "%s" is invalid.', $bundle_title));
74 public function validateCommandName($class_name)
76 if (preg_match(self::REGEX_COMMAND_CLASS_NAME, $class_name)) {
78 } elseif (preg_match(self::REGEX_CLASS_NAME, $class_name)) {
79 throw new \InvalidArgumentException(
81 'Command name "%s" is invalid, it must end with the word \'Command\'',
86 throw new \InvalidArgumentException(
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\'.',
95 public function validateMachineName($machine_name)
97 if (preg_match(self::REGEX_MACHINE_NAME, $machine_name)) {
100 throw new \InvalidArgumentException(
102 'Machine name "%s" is invalid, it must contain only lowercase letters, numbers and underscores.',
109 public function validateModulePath($module_path, $create = false)
111 if (!is_dir($module_path)) {
112 if ($create && mkdir($module_path, 0755, true)) {
116 throw new \InvalidArgumentException(
118 'Module path "%s" is invalid. You need to provide a valid path.',
127 public function validateMachineNameList($list)
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;
144 $list_checked['fail'][] = $module;
149 return $list_checked;
153 * Validate if service name exist.
155 * @param string $service Service name
156 * @param array $services Array of services
160 public function validateServiceExist($service, $services)
162 if ($service == '') {
166 if (!in_array($service, array_values($services))) {
167 throw new \InvalidArgumentException(sprintf('Service "%s" is invalid.', $service));
174 * Validate if service name exist.
176 * @param string $service Service name
177 * @param array $services Array of services
181 public function validatePluginManagerServiceExist($service, $services)
183 if ($service == '') {
187 if (!in_array($service, array_values($services))) {
188 throw new \InvalidArgumentException(sprintf('Plugin "%s" is invalid.', $service));
195 * Validate if event name exist.
197 * @param string $event Event name
198 * @param array $events Array of events
202 public function validateEventExist($event, $events)
208 if (!in_array($event, array_values($events))) {
209 throw new \InvalidArgumentException(sprintf('Event "%s" is invalid.', $event));
216 * Validates if class name have spaces between words.
218 * @param string $name
222 public function validateSpaces($name)
224 $string = $this->removeSpaces($name);
225 if ($string == $name) {
228 throw new \InvalidArgumentException(
230 'The name "%s" is invalid, spaces between words are not allowed.',
237 public function removeSpaces($name)
239 return preg_replace(self::REGEX_REMOVE_SPACES, '', $name);
246 public function getMissingModules($moduleList)
248 $modules = $this->extensionManager->discoverModules()
255 return array_diff($moduleList, $modules);
262 public function getUninstalledModules($moduleList)
264 $modules = $this->extensionManager->discoverModules()
270 return array_diff($moduleList, $modules);
274 * @param string $extensions_list
275 * @param string $type
276 * @param DrupalStyle $io
280 public function validateExtensions($extensions_list, $type, DrupalStyle $io)
282 $extensions = $this->validateMachineNameList($extensions_list);
283 // Check if all extensions are available
285 $checked_extensions = $this->extensionManager->checkExtensions($extensions['success'], $type);
286 if (!empty($checked_extensions['no_extensions'])) {
289 $this->translatorManager->trans('validator.warnings.extension-unavailable'),
290 implode(', ', $checked_extensions['no_extensions'])
294 $extensions = $extensions['success'];