1 | <?php |
||
14 | class Util |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | public const DATE_FORMAT = 'YmdHis'; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected const MIGRATION_FILE_NAME_PATTERN = '/^\d+_([a-z][a-z\d]*(?:_[a-z\d]+)*)\.php$/i'; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected const MIGRATION_FILE_NAME_NO_NAME_PATTERN = '/^[0-9]{14}\.php$/'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected const SEED_FILE_NAME_PATTERN = '/^([a-z][a-z\d]*)\.php$/i'; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected const CLASS_NAME_PATTERN = '/^(?:[A-Z][a-z\d]*)+$/'; |
||
40 | |||
41 | /** |
||
42 | * Gets the current timestamp string, in UTC. |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | public static function getCurrentTimestamp() |
||
52 | |||
53 | 15 | /** |
|
54 | * Gets an array of all the existing migration class names. |
||
55 | 15 | * |
|
56 | 15 | * @param string $path Path |
|
57 | * |
||
58 | * @return string[] |
||
59 | */ |
||
60 | public static function getExistingMigrationClassNames($path) |
||
80 | |||
81 | 14 | /** |
|
82 | * Get the version from the beginning of a file name. |
||
83 | * |
||
84 | * @param string $fileName File Name |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public static function getVersionFromFileName($fileName) |
||
95 | |||
96 | /** |
||
97 | * Turn migration names like 'CreateUserTable' into file names like |
||
98 | * '12345678901234_create_user_table.php' or 'LimitResourceNamesTo30Chars' into |
||
99 | * '12345678901234_limit_resource_names_to_30_chars.php'. |
||
100 | * |
||
101 | * @param string $className Class Name |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 14 | public static function mapClassNameToFileName($className) |
|
115 | |||
116 | /** |
||
117 | * Turn file names like '12345678901234_create_user_table.php' into class |
||
118 | * names like 'CreateUserTable'. |
||
119 | * |
||
120 | 391 | * @param string $fileName File Name |
|
121 | * |
||
122 | 391 | * @return string |
|
123 | 391 | */ |
|
124 | 391 | public static function mapFileNameToClassName(string $fileName): string |
|
137 | |||
138 | /** |
||
139 | * Check if a migration class name is unique regardless of the |
||
140 | * timestamp. |
||
141 | * |
||
142 | * This method takes a class name and a path to a migrations directory. |
||
143 | * |
||
144 | * Migration class names must be in PascalCase format but consecutive |
||
145 | 13 | * capitals are allowed. |
|
146 | * e.g: AddIndexToPostsTable or CustomHTMLTitle. |
||
147 | 13 | * |
|
148 | 13 | * @param string $className Class Name |
|
149 | * @param string $path Path |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public static function isUniqueMigrationClassName($className, $path) |
||
159 | |||
160 | /** |
||
161 | * Check if a migration/seed class name is valid. |
||
162 | 16 | * |
|
163 | * Migration & Seed class names must be in CamelCase format. |
||
164 | 16 | * e.g: CreateUserTable, AddIndexToPostsTable or UserSeeder. |
|
165 | * |
||
166 | * Single words are not allowed on their own. |
||
167 | * |
||
168 | * @param string $className Class Name |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | public static function isValidPhinxClassName($className) |
||
176 | 387 | ||
177 | /** |
||
178 | * Check if a migration file name is valid. |
||
179 | * |
||
180 | * @param string $fileName File Name |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | public static function isValidMigrationFileName(string $fileName): bool |
||
191 | |||
192 | /** |
||
193 | * Check if a seed file name is valid. |
||
194 | * |
||
195 | * @param string $fileName File Name |
||
196 | * |
||
197 | 33 | * @return bool |
|
198 | */ |
||
199 | 33 | public static function isValidSeedFileName($fileName) |
|
203 | 33 | ||
204 | /** |
||
205 | 33 | * Expands a set of paths with curly braces (if supported by the OS). |
|
206 | * |
||
207 | * @param string[] $paths Paths |
||
208 | * |
||
209 | * @return string[] |
||
210 | */ |
||
211 | public static function globAll(array $paths) |
||
212 | { |
||
213 | $result = []; |
||
214 | 431 | ||
215 | foreach ($paths as $path) { |
||
216 | 431 | $result = array_merge($result, static::glob($path)); |
|
217 | } |
||
218 | |||
219 | return $result; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Expands a path with curly braces (if supported by the OS). |
||
224 | * |
||
225 | * @param string $path Path |
||
226 | * |
||
227 | * @return string[] |
||
228 | */ |
||
229 | public static function glob($path) |
||
230 | { |
||
231 | return glob($path, defined('GLOB_BRACE') ? GLOB_BRACE : 0); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Takes the path to a php file and attempts to include it if readable |
||
236 | * |
||
237 | * @param string $filename Filename |
||
238 | * |
||
239 | * @throws \Exception |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public static function loadPhpFile($filename) |
||
265 | |||
266 | /** |
||
267 | * Given an array of paths, return all unique PHP files that are in them |
||
268 | * |
||
269 | * @param string|string[] $paths Path or array of paths to get .php files. |
||
270 | * |
||
271 | * @return string[] |
||
272 | */ |
||
273 | public static function getFiles($paths) |
||
286 | |||
287 | /** |
||
288 | * Attempt to remove the current working directory from a path for output. |
||
289 | * |
||
290 | * @param string $path Path to remove cwd prefix from |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public static function relativePath($path) |
||
308 | |||
309 | /** |
||
310 | * Parses DSN string into db config array. |
||
311 | * |
||
312 | * @param string $dsn DSN string |
||
313 | * @return array |
||
314 | */ |
||
315 | public static function parseDsn(string $dsn): array |
||
359 | } |
||
360 |