Mercurial > hg > cmmr2012-drupal-site
comparison vendor/chi-teck/drupal-code-generator/src/Utils.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace DrupalCodeGenerator; | |
4 | |
5 use Symfony\Component\Console\Question\Question; | |
6 | |
7 /** | |
8 * Helper methods for code generators. | |
9 */ | |
10 class Utils { | |
11 | |
12 /** | |
13 * Creates default plugin ID. | |
14 */ | |
15 public static function defaultPluginId(array $vars) { | |
16 return $vars['machine_name'] . '_' . self::human2machine($vars['plugin_label']); | |
17 } | |
18 | |
19 /** | |
20 * Transforms a machine name to human name. | |
21 */ | |
22 public static function machine2human($machine_name) { | |
23 return ucfirst(trim(str_replace('_', ' ', $machine_name))); | |
24 } | |
25 | |
26 /** | |
27 * Transforms a human name to machine name. | |
28 */ | |
29 public static function human2machine($human_name) { | |
30 return trim(preg_replace( | |
31 ['/^[0-9]+/', '/[^a-z0-9_]+/'], | |
32 '_', | |
33 strtolower($human_name) | |
34 ), '_'); | |
35 } | |
36 | |
37 /** | |
38 * Camelize a string. | |
39 */ | |
40 public static function camelize($string, $upper_camel = TRUE) { | |
41 $output = preg_replace('/([^A-Z])([A-Z])/', '$1 $2', $string); | |
42 $output = strtolower($output); | |
43 $output = preg_replace('/[^a-z0-9]/', ' ', $output); | |
44 $output = trim($output); | |
45 $output = ucwords($output); | |
46 $output = str_replace(' ', '', $output); | |
47 return $upper_camel ? $output : lcfirst($output); | |
48 } | |
49 | |
50 /** | |
51 * Machine name validator. | |
52 */ | |
53 public static function validateMachineName($value) { | |
54 if (!preg_match('/^[a-z][a-z0-9_]*[a-z0-9]$/', $value)) { | |
55 throw new \UnexpectedValueException('The value is not correct machine name.'); | |
56 } | |
57 return $value; | |
58 } | |
59 | |
60 /** | |
61 * Class name validator. | |
62 * | |
63 * @see http://php.net/manual/en/language.oop5.basic.php | |
64 */ | |
65 public static function validateClassName($value) { | |
66 if (!preg_match('/^[A-Z][a-zA-Z0-9]+$/', $value)) { | |
67 throw new \UnexpectedValueException('The value is not correct class name.'); | |
68 } | |
69 return $value; | |
70 } | |
71 | |
72 /** | |
73 * Required value validator. | |
74 */ | |
75 public static function validateRequired($value) { | |
76 // FALSE is not considered as empty value because question helper use | |
77 // it as negative answer on confirmation questions. | |
78 if ($value === NULL || $value === '') { | |
79 throw new \UnexpectedValueException('The value is required.'); | |
80 } | |
81 return $value; | |
82 } | |
83 | |
84 /** | |
85 * Returns normalized file path. | |
86 * | |
87 * @codeCoverageIgnore | |
88 * @deprecated | |
89 */ | |
90 public static function normalizePath($path) { | |
91 $parts = []; | |
92 $path = str_replace('\\', '/', $path); | |
93 $path = preg_replace('/\/+/', '/', $path); | |
94 $segments = explode('/', $path); | |
95 foreach ($segments as $segment) { | |
96 if ($segment != '.') { | |
97 $test = array_pop($parts); | |
98 if (is_null($test)) { | |
99 $parts[] = $segment; | |
100 } | |
101 elseif ($segment == '..') { | |
102 if ($test == '..') { | |
103 $parts[] = $test; | |
104 } | |
105 if ($test == '..' || $test == '') { | |
106 $parts[] = $segment; | |
107 } | |
108 } | |
109 else { | |
110 $parts[] = $test; | |
111 $parts[] = $segment; | |
112 } | |
113 } | |
114 } | |
115 return implode('/', $parts); | |
116 } | |
117 | |
118 /** | |
119 * Returns default questions for module generators. | |
120 * | |
121 * @return \Symfony\Component\Console\Question\Question[] | |
122 * Array of default questions. | |
123 */ | |
124 public static function defaultQuestions() { | |
125 $questions['name'] = new Question('Module name'); | |
126 $questions['name']->setValidator([Utils::class, 'validateRequired']); | |
127 $questions['machine_name'] = new Question('Module machine name'); | |
128 $questions['machine_name']->setValidator([Utils::class, 'validateMachineName']); | |
129 return $questions; | |
130 } | |
131 | |
132 /** | |
133 * Returns default questions for plugin generators. | |
134 * | |
135 * @return \Symfony\Component\Console\Question\Question[] | |
136 * Array of default questions. | |
137 */ | |
138 public static function defaultPluginQuestions() { | |
139 $questions = Utils::defaultQuestions(); | |
140 $questions['plugin_label'] = new Question('Plugin label', 'Example'); | |
141 $questions['plugin_label']->setValidator([Utils::class, 'validateRequired']); | |
142 $questions['plugin_id'] = new Question('Plugin ID', [Utils::class, 'defaultPluginId']); | |
143 $questions['plugin_id']->setValidator([Utils::class, 'validateMachineName']); | |
144 return $questions; | |
145 } | |
146 | |
147 /** | |
148 * Returns extension root. | |
149 * | |
150 * @return string|bool | |
151 * Extension root directory or false if it was not found. | |
152 */ | |
153 public static function getExtensionRoot($directory) { | |
154 $extension_root = FALSE; | |
155 for ($i = 1; $i <= 5; $i++) { | |
156 $info_file = $directory . '/' . basename($directory) . '.info'; | |
157 if (file_exists($info_file) || file_exists($info_file . '.yml')) { | |
158 $extension_root = $directory; | |
159 break; | |
160 } | |
161 $directory = dirname($directory); | |
162 } | |
163 return $extension_root; | |
164 } | |
165 | |
166 /** | |
167 * Removes a given number of lines from the beginning of the string. | |
168 */ | |
169 public static function removeHeader($content, $header_size) { | |
170 return implode("\n", array_slice(explode("\n", $content), $header_size)); | |
171 } | |
172 | |
173 /** | |
174 * Return the user's home directory. | |
175 */ | |
176 public static function getHomeDirectory() { | |
177 return isset($_SERVER['HOME']) ? $_SERVER['HOME'] : getenv('HOME'); | |
178 } | |
179 | |
180 /** | |
181 * Replaces all tokens in a given string with appropriate values. | |
182 * | |
183 * @param string $text | |
184 * A string potentially containing replaceable tokens. | |
185 * @param array $data | |
186 * An array where keys are token names and values are replacements. | |
187 * | |
188 * @return string | |
189 * Text with tokens replaced. | |
190 */ | |
191 public static function tokenReplace($text, array $data) { | |
192 $tokens = []; | |
193 foreach ($data as $var_name => $var) { | |
194 if (is_string($var)) { | |
195 $tokens['{' . $var_name . '}'] = $var; | |
196 } | |
197 } | |
198 return str_replace(array_keys($tokens), array_values($tokens), $text); | |
199 } | |
200 | |
201 } |