comparison vendor/chi-teck/drupal-code-generator/src/Utils.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
6 6
7 /** 7 /**
8 * Helper methods for code generators. 8 * Helper methods for code generators.
9 */ 9 */
10 class Utils { 10 class Utils {
11
12 use LegacyUtilsTrait;
11 13
12 /** 14 /**
13 * Creates default plugin ID. 15 * Creates default plugin ID.
14 */ 16 */
15 public static function defaultPluginId(array $vars) { 17 public static function defaultPluginId(array $vars) {
97 } 99 }
98 return $value; 100 return $value;
99 } 101 }
100 102
101 /** 103 /**
102 * Returns normalized file path. 104 * Returns a validator for allowed options.
103 * 105 *
104 * @codeCoverageIgnore 106 * @param array $options
105 * @deprecated 107 * Allowed values.
106 */ 108 *
107 public static function normalizePath($path) { 109 * @return callable
108 $parts = []; 110 * Question validator.
109 $path = str_replace('\\', '/', $path); 111 */
110 $path = preg_replace('/\/+/', '/', $path); 112 public static function getOptionsValidator(array $options) {
111 $segments = explode('/', $path); 113 return function ($value) use ($options) {
112 foreach ($segments as $segment) { 114 if (!in_array($value, $options)) {
113 if ($segment != '.') { 115 $options_formatted = implode(', ', $options);
114 $test = array_pop($parts); 116 $error_message = sprintf('The value should be one of the following: %s.', $options_formatted);
115 if (is_null($test)) { 117 throw new \UnexpectedValueException($error_message);
116 $parts[] = $segment;
117 }
118 elseif ($segment == '..') {
119 if ($test == '..') {
120 $parts[] = $test;
121 }
122 if ($test == '..' || $test == '') {
123 $parts[] = $segment;
124 }
125 }
126 else {
127 $parts[] = $test;
128 $parts[] = $segment;
129 }
130 } 118 }
131 } 119 return $value;
132 return implode('/', $parts); 120 };
133 } 121 }
134 122
135 /** 123 /**
136 * Returns default questions for module generators. 124 * Returns default questions for module generators.
137 * 125 *
138 * @return \Symfony\Component\Console\Question\Question[] 126 * @return \Symfony\Component\Console\Question\Question[]
139 * Array of default questions. 127 * Array of module questions.
140 */ 128 */
141 public static function defaultQuestions() { 129 public static function moduleQuestions() {
142 $questions['name'] = new Question('Module name'); 130 $questions['name'] = new Question('Module name');
143 $questions['name']->setValidator([Utils::class, 'validateRequired']); 131 $questions['name']->setValidator([Utils::class, 'validateRequired']);
144 $questions['machine_name'] = new Question('Module machine name'); 132 $questions['machine_name'] = new Question('Module machine name');
145 $questions['machine_name']->setValidator([Utils::class, 'validateMachineName']); 133 $questions['machine_name']->setValidator([Utils::class, 'validateMachineName']);
146 return $questions; 134 return $questions;
148 136
149 /** 137 /**
150 * Returns default questions for plugin generators. 138 * Returns default questions for plugin generators.
151 * 139 *
152 * @return \Symfony\Component\Console\Question\Question[] 140 * @return \Symfony\Component\Console\Question\Question[]
153 * Array of default questions. 141 * Array of plugin questions.
154 */ 142 */
155 public static function defaultPluginQuestions() { 143 public static function pluginQuestions($class_suffix = '') {
156 $questions = Utils::defaultQuestions();
157 $questions['plugin_label'] = new Question('Plugin label', 'Example'); 144 $questions['plugin_label'] = new Question('Plugin label', 'Example');
158 $questions['plugin_label']->setValidator([Utils::class, 'validateRequired']); 145 $questions['plugin_label']->setValidator([Utils::class, 'validateRequired']);
159 $questions['plugin_id'] = new Question('Plugin ID', [Utils::class, 'defaultPluginId']); 146 $questions['plugin_id'] = new Question('Plugin ID', [Utils::class, 'defaultPluginId']);
160 $questions['plugin_id']->setValidator([Utils::class, 'validateMachineName']); 147 $questions['plugin_id']->setValidator([Utils::class, 'validateMachineName']);
148 $questions['class'] = static::pluginClassQuestion($class_suffix);
161 return $questions; 149 return $questions;
150 }
151
152 /**
153 * Creates plugin class question.
154 */
155 public static function pluginClassQuestion($suffix = '') {
156 $default_class = function ($vars) use ($suffix) {
157 $unprefixed_plugin_id = preg_replace('/^' . $vars['machine_name'] . '_/', '', $vars['plugin_id']);
158 return Utils::camelize($unprefixed_plugin_id) . $suffix;
159 };
160 return new Question('Plugin class', $default_class);
162 } 161 }
163 162
164 /** 163 /**
165 * Returns extension root. 164 * Returns extension root.
166 * 165 *
203 * An array where keys are token names and values are replacements. 202 * An array where keys are token names and values are replacements.
204 * 203 *
205 * @return string 204 * @return string
206 * Text with tokens replaced. 205 * Text with tokens replaced.
207 */ 206 */
208 public static function tokenReplace($text, array $data) { 207 public static function replaceTokens($text, array $data) {
209 $tokens = []; 208 $tokens = [];
210 foreach ($data as $var_name => $var) { 209 foreach ($data as $var_name => $var) {
211 if (is_string($var)) { 210 if (is_string($var)) {
212 $tokens['{' . $var_name . '}'] = $var; 211 $tokens['{' . $var_name . '}'] = $var;
213 } 212 }