@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | */ |
114 | 114 | public function getPrefixes() |
115 | 115 | { |
116 | - if (!empty($this->prefixesPsr0)) { |
|
116 | + if ( ! empty($this->prefixesPsr0)) { |
|
117 | 117 | return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
118 | 118 | } |
119 | 119 | |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | */ |
184 | 184 | public function add($prefix, $paths, $prepend = false) |
185 | 185 | { |
186 | - if (!$prefix) { |
|
186 | + if ( ! $prefix) { |
|
187 | 187 | if ($prepend) { |
188 | 188 | $this->fallbackDirsPsr0 = array_merge( |
189 | 189 | (array) $paths, |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | } |
201 | 201 | |
202 | 202 | $first = $prefix[0]; |
203 | - if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
203 | + if ( ! isset($this->prefixesPsr0[$first][$prefix])) { |
|
204 | 204 | $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
205 | 205 | |
206 | 206 | return; |
@@ -232,7 +232,7 @@ discard block |
||
232 | 232 | */ |
233 | 233 | public function addPsr4($prefix, $paths, $prepend = false) |
234 | 234 | { |
235 | - if (!$prefix) { |
|
235 | + if ( ! $prefix) { |
|
236 | 236 | // Register directories for the root namespace. |
237 | 237 | if ($prepend) { |
238 | 238 | $this->fallbackDirsPsr4 = array_merge( |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | (array) $paths |
246 | 246 | ); |
247 | 247 | } |
248 | - } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
248 | + } elseif ( ! isset($this->prefixDirsPsr4[$prefix])) { |
|
249 | 249 | // Register directories for a new namespace. |
250 | 250 | $length = strlen($prefix); |
251 | 251 | if ('\\' !== $prefix[$length - 1]) { |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | */ |
280 | 280 | public function set($prefix, $paths) |
281 | 281 | { |
282 | - if (!$prefix) { |
|
282 | + if ( ! $prefix) { |
|
283 | 283 | $this->fallbackDirsPsr0 = (array) $paths; |
284 | 284 | } else { |
285 | 285 | $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | */ |
300 | 300 | public function setPsr4($prefix, $paths) |
301 | 301 | { |
302 | - if (!$prefix) { |
|
302 | + if ( ! $prefix) { |
|
303 | 303 | $this->fallbackDirsPsr4 = (array) $paths; |
304 | 304 | } else { |
305 | 305 | $length = strlen($prefix); |
@@ -493,18 +493,18 @@ discard block |
||
493 | 493 | private function findFileWithExtension($class, $ext) |
494 | 494 | { |
495 | 495 | // PSR-4 lookup |
496 | - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
496 | + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR).$ext; |
|
497 | 497 | |
498 | 498 | $first = $class[0]; |
499 | 499 | if (isset($this->prefixLengthsPsr4[$first])) { |
500 | 500 | $subPath = $class; |
501 | 501 | while (false !== $lastPos = strrpos($subPath, '\\')) { |
502 | 502 | $subPath = substr($subPath, 0, $lastPos); |
503 | - $search = $subPath . '\\'; |
|
503 | + $search = $subPath.'\\'; |
|
504 | 504 | if (isset($this->prefixDirsPsr4[$search])) { |
505 | - $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
|
505 | + $pathEnd = DIRECTORY_SEPARATOR.substr($logicalPathPsr4, $lastPos + 1); |
|
506 | 506 | foreach ($this->prefixDirsPsr4[$search] as $dir) { |
507 | - if (file_exists($file = $dir . $pathEnd)) { |
|
507 | + if (file_exists($file = $dir.$pathEnd)) { |
|
508 | 508 | return $file; |
509 | 509 | } |
510 | 510 | } |
@@ -514,7 +514,7 @@ discard block |
||
514 | 514 | |
515 | 515 | // PSR-4 fallback dirs |
516 | 516 | foreach ($this->fallbackDirsPsr4 as $dir) { |
517 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
517 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr4)) { |
|
518 | 518 | return $file; |
519 | 519 | } |
520 | 520 | } |
@@ -526,14 +526,14 @@ discard block |
||
526 | 526 | . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
527 | 527 | } else { |
528 | 528 | // PEAR-like class name |
529 | - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
529 | + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR).$ext; |
|
530 | 530 | } |
531 | 531 | |
532 | 532 | if (isset($this->prefixesPsr0[$first])) { |
533 | 533 | foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
534 | 534 | if (0 === strpos($class, $prefix)) { |
535 | 535 | foreach ($dirs as $dir) { |
536 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
536 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr0)) { |
|
537 | 537 | return $file; |
538 | 538 | } |
539 | 539 | } |
@@ -543,7 +543,7 @@ discard block |
||
543 | 543 | |
544 | 544 | // PSR-0 fallback dirs |
545 | 545 | foreach ($this->fallbackDirsPsr0 as $dir) { |
546 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
546 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr0)) { |
|
547 | 547 | return $file; |
548 | 548 | } |
549 | 549 | } |
@@ -42,519 +42,519 @@ discard block |
||
42 | 42 | */ |
43 | 43 | class ClassLoader |
44 | 44 | { |
45 | - /** @var ?string */ |
|
46 | - private $vendorDir; |
|
47 | - |
|
48 | - // PSR-4 |
|
49 | - /** |
|
50 | - * @var array[] |
|
51 | - * @psalm-var array<string, array<string, int>> |
|
52 | - */ |
|
53 | - private $prefixLengthsPsr4 = array(); |
|
54 | - /** |
|
55 | - * @var array[] |
|
56 | - * @psalm-var array<string, array<int, string>> |
|
57 | - */ |
|
58 | - private $prefixDirsPsr4 = array(); |
|
59 | - /** |
|
60 | - * @var array[] |
|
61 | - * @psalm-var array<string, string> |
|
62 | - */ |
|
63 | - private $fallbackDirsPsr4 = array(); |
|
64 | - |
|
65 | - // PSR-0 |
|
66 | - /** |
|
67 | - * @var array[] |
|
68 | - * @psalm-var array<string, array<string, string[]>> |
|
69 | - */ |
|
70 | - private $prefixesPsr0 = array(); |
|
71 | - /** |
|
72 | - * @var array[] |
|
73 | - * @psalm-var array<string, string> |
|
74 | - */ |
|
75 | - private $fallbackDirsPsr0 = array(); |
|
76 | - |
|
77 | - /** @var bool */ |
|
78 | - private $useIncludePath = false; |
|
79 | - |
|
80 | - /** |
|
81 | - * @var string[] |
|
82 | - * @psalm-var array<string, string> |
|
83 | - */ |
|
84 | - private $classMap = array(); |
|
85 | - |
|
86 | - /** @var bool */ |
|
87 | - private $classMapAuthoritative = false; |
|
88 | - |
|
89 | - /** |
|
90 | - * @var bool[] |
|
91 | - * @psalm-var array<string, bool> |
|
92 | - */ |
|
93 | - private $missingClasses = array(); |
|
94 | - |
|
95 | - /** @var ?string */ |
|
96 | - private $apcuPrefix; |
|
97 | - |
|
98 | - /** |
|
99 | - * @var self[] |
|
100 | - */ |
|
101 | - private static $registeredLoaders = array(); |
|
102 | - |
|
103 | - /** |
|
104 | - * @param ?string $vendorDir |
|
105 | - */ |
|
106 | - public function __construct($vendorDir = null) |
|
107 | - { |
|
108 | - $this->vendorDir = $vendorDir; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @return string[] |
|
113 | - */ |
|
114 | - public function getPrefixes() |
|
115 | - { |
|
116 | - if (!empty($this->prefixesPsr0)) { |
|
117 | - return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
|
118 | - } |
|
119 | - |
|
120 | - return array(); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @return array[] |
|
125 | - * @psalm-return array<string, array<int, string>> |
|
126 | - */ |
|
127 | - public function getPrefixesPsr4() |
|
128 | - { |
|
129 | - return $this->prefixDirsPsr4; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @return array[] |
|
134 | - * @psalm-return array<string, string> |
|
135 | - */ |
|
136 | - public function getFallbackDirs() |
|
137 | - { |
|
138 | - return $this->fallbackDirsPsr0; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @return array[] |
|
143 | - * @psalm-return array<string, string> |
|
144 | - */ |
|
145 | - public function getFallbackDirsPsr4() |
|
146 | - { |
|
147 | - return $this->fallbackDirsPsr4; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @return string[] Array of classname => path |
|
152 | - * @psalm-return array<string, string> |
|
153 | - */ |
|
154 | - public function getClassMap() |
|
155 | - { |
|
156 | - return $this->classMap; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @param string[] $classMap Class to filename map |
|
161 | - * @psalm-param array<string, string> $classMap |
|
162 | - * |
|
163 | - * @return void |
|
164 | - */ |
|
165 | - public function addClassMap(array $classMap) |
|
166 | - { |
|
167 | - if ($this->classMap) { |
|
168 | - $this->classMap = array_merge($this->classMap, $classMap); |
|
169 | - } else { |
|
170 | - $this->classMap = $classMap; |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Registers a set of PSR-0 directories for a given prefix, either |
|
176 | - * appending or prepending to the ones previously set for this prefix. |
|
177 | - * |
|
178 | - * @param string $prefix The prefix |
|
179 | - * @param string[]|string $paths The PSR-0 root directories |
|
180 | - * @param bool $prepend Whether to prepend the directories |
|
181 | - * |
|
182 | - * @return void |
|
183 | - */ |
|
184 | - public function add($prefix, $paths, $prepend = false) |
|
185 | - { |
|
186 | - if (!$prefix) { |
|
187 | - if ($prepend) { |
|
188 | - $this->fallbackDirsPsr0 = array_merge( |
|
189 | - (array) $paths, |
|
190 | - $this->fallbackDirsPsr0 |
|
191 | - ); |
|
192 | - } else { |
|
193 | - $this->fallbackDirsPsr0 = array_merge( |
|
194 | - $this->fallbackDirsPsr0, |
|
195 | - (array) $paths |
|
196 | - ); |
|
197 | - } |
|
198 | - |
|
199 | - return; |
|
200 | - } |
|
201 | - |
|
202 | - $first = $prefix[0]; |
|
203 | - if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
204 | - $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
205 | - |
|
206 | - return; |
|
207 | - } |
|
208 | - if ($prepend) { |
|
209 | - $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
210 | - (array) $paths, |
|
211 | - $this->prefixesPsr0[$first][$prefix] |
|
212 | - ); |
|
213 | - } else { |
|
214 | - $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
215 | - $this->prefixesPsr0[$first][$prefix], |
|
216 | - (array) $paths |
|
217 | - ); |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Registers a set of PSR-4 directories for a given namespace, either |
|
223 | - * appending or prepending to the ones previously set for this namespace. |
|
224 | - * |
|
225 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
226 | - * @param string[]|string $paths The PSR-4 base directories |
|
227 | - * @param bool $prepend Whether to prepend the directories |
|
228 | - * |
|
229 | - * @throws \InvalidArgumentException |
|
230 | - * |
|
231 | - * @return void |
|
232 | - */ |
|
233 | - public function addPsr4($prefix, $paths, $prepend = false) |
|
234 | - { |
|
235 | - if (!$prefix) { |
|
236 | - // Register directories for the root namespace. |
|
237 | - if ($prepend) { |
|
238 | - $this->fallbackDirsPsr4 = array_merge( |
|
239 | - (array) $paths, |
|
240 | - $this->fallbackDirsPsr4 |
|
241 | - ); |
|
242 | - } else { |
|
243 | - $this->fallbackDirsPsr4 = array_merge( |
|
244 | - $this->fallbackDirsPsr4, |
|
245 | - (array) $paths |
|
246 | - ); |
|
247 | - } |
|
248 | - } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
249 | - // Register directories for a new namespace. |
|
250 | - $length = strlen($prefix); |
|
251 | - if ('\\' !== $prefix[$length - 1]) { |
|
252 | - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
253 | - } |
|
254 | - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
255 | - $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
256 | - } elseif ($prepend) { |
|
257 | - // Prepend directories for an already registered namespace. |
|
258 | - $this->prefixDirsPsr4[$prefix] = array_merge( |
|
259 | - (array) $paths, |
|
260 | - $this->prefixDirsPsr4[$prefix] |
|
261 | - ); |
|
262 | - } else { |
|
263 | - // Append directories for an already registered namespace. |
|
264 | - $this->prefixDirsPsr4[$prefix] = array_merge( |
|
265 | - $this->prefixDirsPsr4[$prefix], |
|
266 | - (array) $paths |
|
267 | - ); |
|
268 | - } |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Registers a set of PSR-0 directories for a given prefix, |
|
273 | - * replacing any others previously set for this prefix. |
|
274 | - * |
|
275 | - * @param string $prefix The prefix |
|
276 | - * @param string[]|string $paths The PSR-0 base directories |
|
277 | - * |
|
278 | - * @return void |
|
279 | - */ |
|
280 | - public function set($prefix, $paths) |
|
281 | - { |
|
282 | - if (!$prefix) { |
|
283 | - $this->fallbackDirsPsr0 = (array) $paths; |
|
284 | - } else { |
|
285 | - $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
286 | - } |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Registers a set of PSR-4 directories for a given namespace, |
|
291 | - * replacing any others previously set for this namespace. |
|
292 | - * |
|
293 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
294 | - * @param string[]|string $paths The PSR-4 base directories |
|
295 | - * |
|
296 | - * @throws \InvalidArgumentException |
|
297 | - * |
|
298 | - * @return void |
|
299 | - */ |
|
300 | - public function setPsr4($prefix, $paths) |
|
301 | - { |
|
302 | - if (!$prefix) { |
|
303 | - $this->fallbackDirsPsr4 = (array) $paths; |
|
304 | - } else { |
|
305 | - $length = strlen($prefix); |
|
306 | - if ('\\' !== $prefix[$length - 1]) { |
|
307 | - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
308 | - } |
|
309 | - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
310 | - $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
311 | - } |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Turns on searching the include path for class files. |
|
316 | - * |
|
317 | - * @param bool $useIncludePath |
|
318 | - * |
|
319 | - * @return void |
|
320 | - */ |
|
321 | - public function setUseIncludePath($useIncludePath) |
|
322 | - { |
|
323 | - $this->useIncludePath = $useIncludePath; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Can be used to check if the autoloader uses the include path to check |
|
328 | - * for classes. |
|
329 | - * |
|
330 | - * @return bool |
|
331 | - */ |
|
332 | - public function getUseIncludePath() |
|
333 | - { |
|
334 | - return $this->useIncludePath; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Turns off searching the prefix and fallback directories for classes |
|
339 | - * that have not been registered with the class map. |
|
340 | - * |
|
341 | - * @param bool $classMapAuthoritative |
|
342 | - * |
|
343 | - * @return void |
|
344 | - */ |
|
345 | - public function setClassMapAuthoritative($classMapAuthoritative) |
|
346 | - { |
|
347 | - $this->classMapAuthoritative = $classMapAuthoritative; |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * Should class lookup fail if not found in the current class map? |
|
352 | - * |
|
353 | - * @return bool |
|
354 | - */ |
|
355 | - public function isClassMapAuthoritative() |
|
356 | - { |
|
357 | - return $this->classMapAuthoritative; |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
|
362 | - * |
|
363 | - * @param string|null $apcuPrefix |
|
364 | - * |
|
365 | - * @return void |
|
366 | - */ |
|
367 | - public function setApcuPrefix($apcuPrefix) |
|
368 | - { |
|
369 | - $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * The APCu prefix in use, or null if APCu caching is not enabled. |
|
374 | - * |
|
375 | - * @return string|null |
|
376 | - */ |
|
377 | - public function getApcuPrefix() |
|
378 | - { |
|
379 | - return $this->apcuPrefix; |
|
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Registers this instance as an autoloader. |
|
384 | - * |
|
385 | - * @param bool $prepend Whether to prepend the autoloader or not |
|
386 | - * |
|
387 | - * @return void |
|
388 | - */ |
|
389 | - public function register($prepend = false) |
|
390 | - { |
|
391 | - spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
392 | - |
|
393 | - if (null === $this->vendorDir) { |
|
394 | - return; |
|
395 | - } |
|
396 | - |
|
397 | - if ($prepend) { |
|
398 | - self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; |
|
399 | - } else { |
|
400 | - unset(self::$registeredLoaders[$this->vendorDir]); |
|
401 | - self::$registeredLoaders[$this->vendorDir] = $this; |
|
402 | - } |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Unregisters this instance as an autoloader. |
|
407 | - * |
|
408 | - * @return void |
|
409 | - */ |
|
410 | - public function unregister() |
|
411 | - { |
|
412 | - spl_autoload_unregister(array($this, 'loadClass')); |
|
413 | - |
|
414 | - if (null !== $this->vendorDir) { |
|
415 | - unset(self::$registeredLoaders[$this->vendorDir]); |
|
416 | - } |
|
417 | - } |
|
418 | - |
|
419 | - /** |
|
420 | - * Loads the given class or interface. |
|
421 | - * |
|
422 | - * @param string $class The name of the class |
|
423 | - * @return true|null True if loaded, null otherwise |
|
424 | - */ |
|
425 | - public function loadClass($class) |
|
426 | - { |
|
427 | - if ($file = $this->findFile($class)) { |
|
428 | - includeFile($file); |
|
429 | - |
|
430 | - return true; |
|
431 | - } |
|
432 | - |
|
433 | - return null; |
|
434 | - } |
|
435 | - |
|
436 | - /** |
|
437 | - * Finds the path to the file where the class is defined. |
|
438 | - * |
|
439 | - * @param string $class The name of the class |
|
440 | - * |
|
441 | - * @return string|false The path if found, false otherwise |
|
442 | - */ |
|
443 | - public function findFile($class) |
|
444 | - { |
|
445 | - // class map lookup |
|
446 | - if (isset($this->classMap[$class])) { |
|
447 | - return $this->classMap[$class]; |
|
448 | - } |
|
449 | - if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
|
450 | - return false; |
|
451 | - } |
|
452 | - if (null !== $this->apcuPrefix) { |
|
453 | - $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
|
454 | - if ($hit) { |
|
455 | - return $file; |
|
456 | - } |
|
457 | - } |
|
458 | - |
|
459 | - $file = $this->findFileWithExtension($class, '.php'); |
|
460 | - |
|
461 | - // Search for Hack files if we are running on HHVM |
|
462 | - if (false === $file && defined('HHVM_VERSION')) { |
|
463 | - $file = $this->findFileWithExtension($class, '.hh'); |
|
464 | - } |
|
465 | - |
|
466 | - if (null !== $this->apcuPrefix) { |
|
467 | - apcu_add($this->apcuPrefix.$class, $file); |
|
468 | - } |
|
469 | - |
|
470 | - if (false === $file) { |
|
471 | - // Remember that this class does not exist. |
|
472 | - $this->missingClasses[$class] = true; |
|
473 | - } |
|
474 | - |
|
475 | - return $file; |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * Returns the currently registered loaders indexed by their corresponding vendor directories. |
|
480 | - * |
|
481 | - * @return self[] |
|
482 | - */ |
|
483 | - public static function getRegisteredLoaders() |
|
484 | - { |
|
485 | - return self::$registeredLoaders; |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * @param string $class |
|
490 | - * @param string $ext |
|
491 | - * @return string|false |
|
492 | - */ |
|
493 | - private function findFileWithExtension($class, $ext) |
|
494 | - { |
|
495 | - // PSR-4 lookup |
|
496 | - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
497 | - |
|
498 | - $first = $class[0]; |
|
499 | - if (isset($this->prefixLengthsPsr4[$first])) { |
|
500 | - $subPath = $class; |
|
501 | - while (false !== $lastPos = strrpos($subPath, '\\')) { |
|
502 | - $subPath = substr($subPath, 0, $lastPos); |
|
503 | - $search = $subPath . '\\'; |
|
504 | - if (isset($this->prefixDirsPsr4[$search])) { |
|
505 | - $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
|
506 | - foreach ($this->prefixDirsPsr4[$search] as $dir) { |
|
507 | - if (file_exists($file = $dir . $pathEnd)) { |
|
508 | - return $file; |
|
509 | - } |
|
510 | - } |
|
511 | - } |
|
512 | - } |
|
513 | - } |
|
514 | - |
|
515 | - // PSR-4 fallback dirs |
|
516 | - foreach ($this->fallbackDirsPsr4 as $dir) { |
|
517 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
518 | - return $file; |
|
519 | - } |
|
520 | - } |
|
521 | - |
|
522 | - // PSR-0 lookup |
|
523 | - if (false !== $pos = strrpos($class, '\\')) { |
|
524 | - // namespaced class name |
|
525 | - $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
526 | - . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
527 | - } else { |
|
528 | - // PEAR-like class name |
|
529 | - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
530 | - } |
|
531 | - |
|
532 | - if (isset($this->prefixesPsr0[$first])) { |
|
533 | - foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
534 | - if (0 === strpos($class, $prefix)) { |
|
535 | - foreach ($dirs as $dir) { |
|
536 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
537 | - return $file; |
|
538 | - } |
|
539 | - } |
|
540 | - } |
|
541 | - } |
|
542 | - } |
|
543 | - |
|
544 | - // PSR-0 fallback dirs |
|
545 | - foreach ($this->fallbackDirsPsr0 as $dir) { |
|
546 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
547 | - return $file; |
|
548 | - } |
|
549 | - } |
|
550 | - |
|
551 | - // PSR-0 include paths. |
|
552 | - if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
553 | - return $file; |
|
554 | - } |
|
555 | - |
|
556 | - return false; |
|
557 | - } |
|
45 | + /** @var ?string */ |
|
46 | + private $vendorDir; |
|
47 | + |
|
48 | + // PSR-4 |
|
49 | + /** |
|
50 | + * @var array[] |
|
51 | + * @psalm-var array<string, array<string, int>> |
|
52 | + */ |
|
53 | + private $prefixLengthsPsr4 = array(); |
|
54 | + /** |
|
55 | + * @var array[] |
|
56 | + * @psalm-var array<string, array<int, string>> |
|
57 | + */ |
|
58 | + private $prefixDirsPsr4 = array(); |
|
59 | + /** |
|
60 | + * @var array[] |
|
61 | + * @psalm-var array<string, string> |
|
62 | + */ |
|
63 | + private $fallbackDirsPsr4 = array(); |
|
64 | + |
|
65 | + // PSR-0 |
|
66 | + /** |
|
67 | + * @var array[] |
|
68 | + * @psalm-var array<string, array<string, string[]>> |
|
69 | + */ |
|
70 | + private $prefixesPsr0 = array(); |
|
71 | + /** |
|
72 | + * @var array[] |
|
73 | + * @psalm-var array<string, string> |
|
74 | + */ |
|
75 | + private $fallbackDirsPsr0 = array(); |
|
76 | + |
|
77 | + /** @var bool */ |
|
78 | + private $useIncludePath = false; |
|
79 | + |
|
80 | + /** |
|
81 | + * @var string[] |
|
82 | + * @psalm-var array<string, string> |
|
83 | + */ |
|
84 | + private $classMap = array(); |
|
85 | + |
|
86 | + /** @var bool */ |
|
87 | + private $classMapAuthoritative = false; |
|
88 | + |
|
89 | + /** |
|
90 | + * @var bool[] |
|
91 | + * @psalm-var array<string, bool> |
|
92 | + */ |
|
93 | + private $missingClasses = array(); |
|
94 | + |
|
95 | + /** @var ?string */ |
|
96 | + private $apcuPrefix; |
|
97 | + |
|
98 | + /** |
|
99 | + * @var self[] |
|
100 | + */ |
|
101 | + private static $registeredLoaders = array(); |
|
102 | + |
|
103 | + /** |
|
104 | + * @param ?string $vendorDir |
|
105 | + */ |
|
106 | + public function __construct($vendorDir = null) |
|
107 | + { |
|
108 | + $this->vendorDir = $vendorDir; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @return string[] |
|
113 | + */ |
|
114 | + public function getPrefixes() |
|
115 | + { |
|
116 | + if (!empty($this->prefixesPsr0)) { |
|
117 | + return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
|
118 | + } |
|
119 | + |
|
120 | + return array(); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @return array[] |
|
125 | + * @psalm-return array<string, array<int, string>> |
|
126 | + */ |
|
127 | + public function getPrefixesPsr4() |
|
128 | + { |
|
129 | + return $this->prefixDirsPsr4; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @return array[] |
|
134 | + * @psalm-return array<string, string> |
|
135 | + */ |
|
136 | + public function getFallbackDirs() |
|
137 | + { |
|
138 | + return $this->fallbackDirsPsr0; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @return array[] |
|
143 | + * @psalm-return array<string, string> |
|
144 | + */ |
|
145 | + public function getFallbackDirsPsr4() |
|
146 | + { |
|
147 | + return $this->fallbackDirsPsr4; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @return string[] Array of classname => path |
|
152 | + * @psalm-return array<string, string> |
|
153 | + */ |
|
154 | + public function getClassMap() |
|
155 | + { |
|
156 | + return $this->classMap; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @param string[] $classMap Class to filename map |
|
161 | + * @psalm-param array<string, string> $classMap |
|
162 | + * |
|
163 | + * @return void |
|
164 | + */ |
|
165 | + public function addClassMap(array $classMap) |
|
166 | + { |
|
167 | + if ($this->classMap) { |
|
168 | + $this->classMap = array_merge($this->classMap, $classMap); |
|
169 | + } else { |
|
170 | + $this->classMap = $classMap; |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Registers a set of PSR-0 directories for a given prefix, either |
|
176 | + * appending or prepending to the ones previously set for this prefix. |
|
177 | + * |
|
178 | + * @param string $prefix The prefix |
|
179 | + * @param string[]|string $paths The PSR-0 root directories |
|
180 | + * @param bool $prepend Whether to prepend the directories |
|
181 | + * |
|
182 | + * @return void |
|
183 | + */ |
|
184 | + public function add($prefix, $paths, $prepend = false) |
|
185 | + { |
|
186 | + if (!$prefix) { |
|
187 | + if ($prepend) { |
|
188 | + $this->fallbackDirsPsr0 = array_merge( |
|
189 | + (array) $paths, |
|
190 | + $this->fallbackDirsPsr0 |
|
191 | + ); |
|
192 | + } else { |
|
193 | + $this->fallbackDirsPsr0 = array_merge( |
|
194 | + $this->fallbackDirsPsr0, |
|
195 | + (array) $paths |
|
196 | + ); |
|
197 | + } |
|
198 | + |
|
199 | + return; |
|
200 | + } |
|
201 | + |
|
202 | + $first = $prefix[0]; |
|
203 | + if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
204 | + $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
205 | + |
|
206 | + return; |
|
207 | + } |
|
208 | + if ($prepend) { |
|
209 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
210 | + (array) $paths, |
|
211 | + $this->prefixesPsr0[$first][$prefix] |
|
212 | + ); |
|
213 | + } else { |
|
214 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
215 | + $this->prefixesPsr0[$first][$prefix], |
|
216 | + (array) $paths |
|
217 | + ); |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Registers a set of PSR-4 directories for a given namespace, either |
|
223 | + * appending or prepending to the ones previously set for this namespace. |
|
224 | + * |
|
225 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
226 | + * @param string[]|string $paths The PSR-4 base directories |
|
227 | + * @param bool $prepend Whether to prepend the directories |
|
228 | + * |
|
229 | + * @throws \InvalidArgumentException |
|
230 | + * |
|
231 | + * @return void |
|
232 | + */ |
|
233 | + public function addPsr4($prefix, $paths, $prepend = false) |
|
234 | + { |
|
235 | + if (!$prefix) { |
|
236 | + // Register directories for the root namespace. |
|
237 | + if ($prepend) { |
|
238 | + $this->fallbackDirsPsr4 = array_merge( |
|
239 | + (array) $paths, |
|
240 | + $this->fallbackDirsPsr4 |
|
241 | + ); |
|
242 | + } else { |
|
243 | + $this->fallbackDirsPsr4 = array_merge( |
|
244 | + $this->fallbackDirsPsr4, |
|
245 | + (array) $paths |
|
246 | + ); |
|
247 | + } |
|
248 | + } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
249 | + // Register directories for a new namespace. |
|
250 | + $length = strlen($prefix); |
|
251 | + if ('\\' !== $prefix[$length - 1]) { |
|
252 | + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
253 | + } |
|
254 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
255 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
256 | + } elseif ($prepend) { |
|
257 | + // Prepend directories for an already registered namespace. |
|
258 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
259 | + (array) $paths, |
|
260 | + $this->prefixDirsPsr4[$prefix] |
|
261 | + ); |
|
262 | + } else { |
|
263 | + // Append directories for an already registered namespace. |
|
264 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
265 | + $this->prefixDirsPsr4[$prefix], |
|
266 | + (array) $paths |
|
267 | + ); |
|
268 | + } |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Registers a set of PSR-0 directories for a given prefix, |
|
273 | + * replacing any others previously set for this prefix. |
|
274 | + * |
|
275 | + * @param string $prefix The prefix |
|
276 | + * @param string[]|string $paths The PSR-0 base directories |
|
277 | + * |
|
278 | + * @return void |
|
279 | + */ |
|
280 | + public function set($prefix, $paths) |
|
281 | + { |
|
282 | + if (!$prefix) { |
|
283 | + $this->fallbackDirsPsr0 = (array) $paths; |
|
284 | + } else { |
|
285 | + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
286 | + } |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Registers a set of PSR-4 directories for a given namespace, |
|
291 | + * replacing any others previously set for this namespace. |
|
292 | + * |
|
293 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
294 | + * @param string[]|string $paths The PSR-4 base directories |
|
295 | + * |
|
296 | + * @throws \InvalidArgumentException |
|
297 | + * |
|
298 | + * @return void |
|
299 | + */ |
|
300 | + public function setPsr4($prefix, $paths) |
|
301 | + { |
|
302 | + if (!$prefix) { |
|
303 | + $this->fallbackDirsPsr4 = (array) $paths; |
|
304 | + } else { |
|
305 | + $length = strlen($prefix); |
|
306 | + if ('\\' !== $prefix[$length - 1]) { |
|
307 | + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
308 | + } |
|
309 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
310 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
311 | + } |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Turns on searching the include path for class files. |
|
316 | + * |
|
317 | + * @param bool $useIncludePath |
|
318 | + * |
|
319 | + * @return void |
|
320 | + */ |
|
321 | + public function setUseIncludePath($useIncludePath) |
|
322 | + { |
|
323 | + $this->useIncludePath = $useIncludePath; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Can be used to check if the autoloader uses the include path to check |
|
328 | + * for classes. |
|
329 | + * |
|
330 | + * @return bool |
|
331 | + */ |
|
332 | + public function getUseIncludePath() |
|
333 | + { |
|
334 | + return $this->useIncludePath; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Turns off searching the prefix and fallback directories for classes |
|
339 | + * that have not been registered with the class map. |
|
340 | + * |
|
341 | + * @param bool $classMapAuthoritative |
|
342 | + * |
|
343 | + * @return void |
|
344 | + */ |
|
345 | + public function setClassMapAuthoritative($classMapAuthoritative) |
|
346 | + { |
|
347 | + $this->classMapAuthoritative = $classMapAuthoritative; |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * Should class lookup fail if not found in the current class map? |
|
352 | + * |
|
353 | + * @return bool |
|
354 | + */ |
|
355 | + public function isClassMapAuthoritative() |
|
356 | + { |
|
357 | + return $this->classMapAuthoritative; |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
|
362 | + * |
|
363 | + * @param string|null $apcuPrefix |
|
364 | + * |
|
365 | + * @return void |
|
366 | + */ |
|
367 | + public function setApcuPrefix($apcuPrefix) |
|
368 | + { |
|
369 | + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * The APCu prefix in use, or null if APCu caching is not enabled. |
|
374 | + * |
|
375 | + * @return string|null |
|
376 | + */ |
|
377 | + public function getApcuPrefix() |
|
378 | + { |
|
379 | + return $this->apcuPrefix; |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Registers this instance as an autoloader. |
|
384 | + * |
|
385 | + * @param bool $prepend Whether to prepend the autoloader or not |
|
386 | + * |
|
387 | + * @return void |
|
388 | + */ |
|
389 | + public function register($prepend = false) |
|
390 | + { |
|
391 | + spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
392 | + |
|
393 | + if (null === $this->vendorDir) { |
|
394 | + return; |
|
395 | + } |
|
396 | + |
|
397 | + if ($prepend) { |
|
398 | + self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; |
|
399 | + } else { |
|
400 | + unset(self::$registeredLoaders[$this->vendorDir]); |
|
401 | + self::$registeredLoaders[$this->vendorDir] = $this; |
|
402 | + } |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Unregisters this instance as an autoloader. |
|
407 | + * |
|
408 | + * @return void |
|
409 | + */ |
|
410 | + public function unregister() |
|
411 | + { |
|
412 | + spl_autoload_unregister(array($this, 'loadClass')); |
|
413 | + |
|
414 | + if (null !== $this->vendorDir) { |
|
415 | + unset(self::$registeredLoaders[$this->vendorDir]); |
|
416 | + } |
|
417 | + } |
|
418 | + |
|
419 | + /** |
|
420 | + * Loads the given class or interface. |
|
421 | + * |
|
422 | + * @param string $class The name of the class |
|
423 | + * @return true|null True if loaded, null otherwise |
|
424 | + */ |
|
425 | + public function loadClass($class) |
|
426 | + { |
|
427 | + if ($file = $this->findFile($class)) { |
|
428 | + includeFile($file); |
|
429 | + |
|
430 | + return true; |
|
431 | + } |
|
432 | + |
|
433 | + return null; |
|
434 | + } |
|
435 | + |
|
436 | + /** |
|
437 | + * Finds the path to the file where the class is defined. |
|
438 | + * |
|
439 | + * @param string $class The name of the class |
|
440 | + * |
|
441 | + * @return string|false The path if found, false otherwise |
|
442 | + */ |
|
443 | + public function findFile($class) |
|
444 | + { |
|
445 | + // class map lookup |
|
446 | + if (isset($this->classMap[$class])) { |
|
447 | + return $this->classMap[$class]; |
|
448 | + } |
|
449 | + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
|
450 | + return false; |
|
451 | + } |
|
452 | + if (null !== $this->apcuPrefix) { |
|
453 | + $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
|
454 | + if ($hit) { |
|
455 | + return $file; |
|
456 | + } |
|
457 | + } |
|
458 | + |
|
459 | + $file = $this->findFileWithExtension($class, '.php'); |
|
460 | + |
|
461 | + // Search for Hack files if we are running on HHVM |
|
462 | + if (false === $file && defined('HHVM_VERSION')) { |
|
463 | + $file = $this->findFileWithExtension($class, '.hh'); |
|
464 | + } |
|
465 | + |
|
466 | + if (null !== $this->apcuPrefix) { |
|
467 | + apcu_add($this->apcuPrefix.$class, $file); |
|
468 | + } |
|
469 | + |
|
470 | + if (false === $file) { |
|
471 | + // Remember that this class does not exist. |
|
472 | + $this->missingClasses[$class] = true; |
|
473 | + } |
|
474 | + |
|
475 | + return $file; |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * Returns the currently registered loaders indexed by their corresponding vendor directories. |
|
480 | + * |
|
481 | + * @return self[] |
|
482 | + */ |
|
483 | + public static function getRegisteredLoaders() |
|
484 | + { |
|
485 | + return self::$registeredLoaders; |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * @param string $class |
|
490 | + * @param string $ext |
|
491 | + * @return string|false |
|
492 | + */ |
|
493 | + private function findFileWithExtension($class, $ext) |
|
494 | + { |
|
495 | + // PSR-4 lookup |
|
496 | + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
497 | + |
|
498 | + $first = $class[0]; |
|
499 | + if (isset($this->prefixLengthsPsr4[$first])) { |
|
500 | + $subPath = $class; |
|
501 | + while (false !== $lastPos = strrpos($subPath, '\\')) { |
|
502 | + $subPath = substr($subPath, 0, $lastPos); |
|
503 | + $search = $subPath . '\\'; |
|
504 | + if (isset($this->prefixDirsPsr4[$search])) { |
|
505 | + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
|
506 | + foreach ($this->prefixDirsPsr4[$search] as $dir) { |
|
507 | + if (file_exists($file = $dir . $pathEnd)) { |
|
508 | + return $file; |
|
509 | + } |
|
510 | + } |
|
511 | + } |
|
512 | + } |
|
513 | + } |
|
514 | + |
|
515 | + // PSR-4 fallback dirs |
|
516 | + foreach ($this->fallbackDirsPsr4 as $dir) { |
|
517 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
518 | + return $file; |
|
519 | + } |
|
520 | + } |
|
521 | + |
|
522 | + // PSR-0 lookup |
|
523 | + if (false !== $pos = strrpos($class, '\\')) { |
|
524 | + // namespaced class name |
|
525 | + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
526 | + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
527 | + } else { |
|
528 | + // PEAR-like class name |
|
529 | + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
530 | + } |
|
531 | + |
|
532 | + if (isset($this->prefixesPsr0[$first])) { |
|
533 | + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
534 | + if (0 === strpos($class, $prefix)) { |
|
535 | + foreach ($dirs as $dir) { |
|
536 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
537 | + return $file; |
|
538 | + } |
|
539 | + } |
|
540 | + } |
|
541 | + } |
|
542 | + } |
|
543 | + |
|
544 | + // PSR-0 fallback dirs |
|
545 | + foreach ($this->fallbackDirsPsr0 as $dir) { |
|
546 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
547 | + return $file; |
|
548 | + } |
|
549 | + } |
|
550 | + |
|
551 | + // PSR-0 include paths. |
|
552 | + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
553 | + return $file; |
|
554 | + } |
|
555 | + |
|
556 | + return false; |
|
557 | + } |
|
558 | 558 | } |
559 | 559 | |
560 | 560 | /** |
@@ -568,5 +568,5 @@ discard block |
||
568 | 568 | */ |
569 | 569 | function includeFile($file) |
570 | 570 | { |
571 | - include $file; |
|
571 | + include $file; |
|
572 | 572 | } |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | public static function getVersionRanges($packageName) |
136 | 136 | { |
137 | 137 | foreach (self::getInstalled() as $installed) { |
138 | - if (!isset($installed['versions'][$packageName])) { |
|
138 | + if ( ! isset($installed['versions'][$packageName])) { |
|
139 | 139 | continue; |
140 | 140 | } |
141 | 141 | |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | return implode(' || ', $ranges); |
157 | 157 | } |
158 | 158 | |
159 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
159 | + throw new \OutOfBoundsException('Package "'.$packageName.'" is not installed'); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | /** |
@@ -166,18 +166,18 @@ discard block |
||
166 | 166 | public static function getVersion($packageName) |
167 | 167 | { |
168 | 168 | foreach (self::getInstalled() as $installed) { |
169 | - if (!isset($installed['versions'][$packageName])) { |
|
169 | + if ( ! isset($installed['versions'][$packageName])) { |
|
170 | 170 | continue; |
171 | 171 | } |
172 | 172 | |
173 | - if (!isset($installed['versions'][$packageName]['version'])) { |
|
173 | + if ( ! isset($installed['versions'][$packageName]['version'])) { |
|
174 | 174 | return null; |
175 | 175 | } |
176 | 176 | |
177 | 177 | return $installed['versions'][$packageName]['version']; |
178 | 178 | } |
179 | 179 | |
180 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
180 | + throw new \OutOfBoundsException('Package "'.$packageName.'" is not installed'); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | /** |
@@ -187,18 +187,18 @@ discard block |
||
187 | 187 | public static function getPrettyVersion($packageName) |
188 | 188 | { |
189 | 189 | foreach (self::getInstalled() as $installed) { |
190 | - if (!isset($installed['versions'][$packageName])) { |
|
190 | + if ( ! isset($installed['versions'][$packageName])) { |
|
191 | 191 | continue; |
192 | 192 | } |
193 | 193 | |
194 | - if (!isset($installed['versions'][$packageName]['pretty_version'])) { |
|
194 | + if ( ! isset($installed['versions'][$packageName]['pretty_version'])) { |
|
195 | 195 | return null; |
196 | 196 | } |
197 | 197 | |
198 | 198 | return $installed['versions'][$packageName]['pretty_version']; |
199 | 199 | } |
200 | 200 | |
201 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
201 | + throw new \OutOfBoundsException('Package "'.$packageName.'" is not installed'); |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
@@ -208,18 +208,18 @@ discard block |
||
208 | 208 | public static function getReference($packageName) |
209 | 209 | { |
210 | 210 | foreach (self::getInstalled() as $installed) { |
211 | - if (!isset($installed['versions'][$packageName])) { |
|
211 | + if ( ! isset($installed['versions'][$packageName])) { |
|
212 | 212 | continue; |
213 | 213 | } |
214 | 214 | |
215 | - if (!isset($installed['versions'][$packageName]['reference'])) { |
|
215 | + if ( ! isset($installed['versions'][$packageName]['reference'])) { |
|
216 | 216 | return null; |
217 | 217 | } |
218 | 218 | |
219 | 219 | return $installed['versions'][$packageName]['reference']; |
220 | 220 | } |
221 | 221 | |
222 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
222 | + throw new \OutOfBoundsException('Package "'.$packageName.'" is not installed'); |
|
223 | 223 | } |
224 | 224 | |
225 | 225 | /** |
@@ -229,14 +229,14 @@ discard block |
||
229 | 229 | public static function getInstallPath($packageName) |
230 | 230 | { |
231 | 231 | foreach (self::getInstalled() as $installed) { |
232 | - if (!isset($installed['versions'][$packageName])) { |
|
232 | + if ( ! isset($installed['versions'][$packageName])) { |
|
233 | 233 | continue; |
234 | 234 | } |
235 | 235 | |
236 | 236 | return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null; |
237 | 237 | } |
238 | 238 | |
239 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
239 | + throw new \OutOfBoundsException('Package "'.$packageName.'" is not installed'); |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | /** |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | // only require the installed.php file if this file is loaded from its dumped location, |
266 | 266 | // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
267 | 267 | if (substr(__DIR__, -8, 1) !== 'C') { |
268 | - self::$installed = include __DIR__ . '/installed.php'; |
|
268 | + self::$installed = include __DIR__.'/installed.php'; |
|
269 | 269 | } else { |
270 | 270 | self::$installed = array(); |
271 | 271 | } |
@@ -338,7 +338,7 @@ discard block |
||
338 | 338 | // only require the installed.php file if this file is loaded from its dumped location, |
339 | 339 | // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
340 | 340 | if (substr(__DIR__, -8, 1) !== 'C') { |
341 | - self::$installed = require __DIR__ . '/installed.php'; |
|
341 | + self::$installed = require __DIR__.'/installed.php'; |
|
342 | 342 | } else { |
343 | 343 | self::$installed = array(); |
344 | 344 | } |
@@ -26,327 +26,327 @@ |
||
26 | 26 | */ |
27 | 27 | class InstalledVersions |
28 | 28 | { |
29 | - /** |
|
30 | - * @var mixed[]|null |
|
31 | - * @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null |
|
32 | - */ |
|
33 | - private static $installed; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var bool|null |
|
37 | - */ |
|
38 | - private static $canGetVendors; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var array[] |
|
42 | - * @psalm-var array<string, array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}> |
|
43 | - */ |
|
44 | - private static $installedByVendor = array(); |
|
45 | - |
|
46 | - /** |
|
47 | - * Returns a list of all package names which are present, either by being installed, replaced or provided |
|
48 | - * |
|
49 | - * @return string[] |
|
50 | - * @psalm-return list<string> |
|
51 | - */ |
|
52 | - public static function getInstalledPackages() |
|
53 | - { |
|
54 | - $packages = array(); |
|
55 | - foreach (self::getInstalled() as $installed) { |
|
56 | - $packages[] = array_keys($installed['versions']); |
|
57 | - } |
|
58 | - |
|
59 | - if (1 === \count($packages)) { |
|
60 | - return $packages[0]; |
|
61 | - } |
|
62 | - |
|
63 | - return array_keys(array_flip(\call_user_func_array('array_merge', $packages))); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Returns a list of all package names with a specific type e.g. 'library' |
|
68 | - * |
|
69 | - * @param string $type |
|
70 | - * @return string[] |
|
71 | - * @psalm-return list<string> |
|
72 | - */ |
|
73 | - public static function getInstalledPackagesByType($type) |
|
74 | - { |
|
75 | - $packagesByType = array(); |
|
76 | - |
|
77 | - foreach (self::getInstalled() as $installed) { |
|
78 | - foreach ($installed['versions'] as $name => $package) { |
|
79 | - if (isset($package['type']) && $package['type'] === $type) { |
|
80 | - $packagesByType[] = $name; |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - return $packagesByType; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Checks whether the given package is installed |
|
90 | - * |
|
91 | - * This also returns true if the package name is provided or replaced by another package |
|
92 | - * |
|
93 | - * @param string $packageName |
|
94 | - * @param bool $includeDevRequirements |
|
95 | - * @return bool |
|
96 | - */ |
|
97 | - public static function isInstalled($packageName, $includeDevRequirements = true) |
|
98 | - { |
|
99 | - foreach (self::getInstalled() as $installed) { |
|
100 | - if (isset($installed['versions'][$packageName])) { |
|
101 | - return $includeDevRequirements || empty($installed['versions'][$packageName]['dev_requirement']); |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - return false; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Checks whether the given package satisfies a version constraint |
|
110 | - * |
|
111 | - * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call: |
|
112 | - * |
|
113 | - * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3') |
|
114 | - * |
|
115 | - * @param VersionParser $parser Install composer/semver to have access to this class and functionality |
|
116 | - * @param string $packageName |
|
117 | - * @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package |
|
118 | - * @return bool |
|
119 | - */ |
|
120 | - public static function satisfies(VersionParser $parser, $packageName, $constraint) |
|
121 | - { |
|
122 | - $constraint = $parser->parseConstraints($constraint); |
|
123 | - $provided = $parser->parseConstraints(self::getVersionRanges($packageName)); |
|
124 | - |
|
125 | - return $provided->matches($constraint); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Returns a version constraint representing all the range(s) which are installed for a given package |
|
130 | - * |
|
131 | - * It is easier to use this via isInstalled() with the $constraint argument if you need to check |
|
132 | - * whether a given version of a package is installed, and not just whether it exists |
|
133 | - * |
|
134 | - * @param string $packageName |
|
135 | - * @return string Version constraint usable with composer/semver |
|
136 | - */ |
|
137 | - public static function getVersionRanges($packageName) |
|
138 | - { |
|
139 | - foreach (self::getInstalled() as $installed) { |
|
140 | - if (!isset($installed['versions'][$packageName])) { |
|
141 | - continue; |
|
142 | - } |
|
143 | - |
|
144 | - $ranges = array(); |
|
145 | - if (isset($installed['versions'][$packageName]['pretty_version'])) { |
|
146 | - $ranges[] = $installed['versions'][$packageName]['pretty_version']; |
|
147 | - } |
|
148 | - if (array_key_exists('aliases', $installed['versions'][$packageName])) { |
|
149 | - $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']); |
|
150 | - } |
|
151 | - if (array_key_exists('replaced', $installed['versions'][$packageName])) { |
|
152 | - $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']); |
|
153 | - } |
|
154 | - if (array_key_exists('provided', $installed['versions'][$packageName])) { |
|
155 | - $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']); |
|
156 | - } |
|
157 | - |
|
158 | - return implode(' || ', $ranges); |
|
159 | - } |
|
160 | - |
|
161 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param string $packageName |
|
166 | - * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present |
|
167 | - */ |
|
168 | - public static function getVersion($packageName) |
|
169 | - { |
|
170 | - foreach (self::getInstalled() as $installed) { |
|
171 | - if (!isset($installed['versions'][$packageName])) { |
|
172 | - continue; |
|
173 | - } |
|
174 | - |
|
175 | - if (!isset($installed['versions'][$packageName]['version'])) { |
|
176 | - return null; |
|
177 | - } |
|
178 | - |
|
179 | - return $installed['versions'][$packageName]['version']; |
|
180 | - } |
|
181 | - |
|
182 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @param string $packageName |
|
187 | - * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present |
|
188 | - */ |
|
189 | - public static function getPrettyVersion($packageName) |
|
190 | - { |
|
191 | - foreach (self::getInstalled() as $installed) { |
|
192 | - if (!isset($installed['versions'][$packageName])) { |
|
193 | - continue; |
|
194 | - } |
|
195 | - |
|
196 | - if (!isset($installed['versions'][$packageName]['pretty_version'])) { |
|
197 | - return null; |
|
198 | - } |
|
199 | - |
|
200 | - return $installed['versions'][$packageName]['pretty_version']; |
|
201 | - } |
|
202 | - |
|
203 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * @param string $packageName |
|
208 | - * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference |
|
209 | - */ |
|
210 | - public static function getReference($packageName) |
|
211 | - { |
|
212 | - foreach (self::getInstalled() as $installed) { |
|
213 | - if (!isset($installed['versions'][$packageName])) { |
|
214 | - continue; |
|
215 | - } |
|
216 | - |
|
217 | - if (!isset($installed['versions'][$packageName]['reference'])) { |
|
218 | - return null; |
|
219 | - } |
|
220 | - |
|
221 | - return $installed['versions'][$packageName]['reference']; |
|
222 | - } |
|
223 | - |
|
224 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * @param string $packageName |
|
229 | - * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path. |
|
230 | - */ |
|
231 | - public static function getInstallPath($packageName) |
|
232 | - { |
|
233 | - foreach (self::getInstalled() as $installed) { |
|
234 | - if (!isset($installed['versions'][$packageName])) { |
|
235 | - continue; |
|
236 | - } |
|
237 | - |
|
238 | - return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null; |
|
239 | - } |
|
240 | - |
|
241 | - throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @return array |
|
246 | - * @psalm-return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool} |
|
247 | - */ |
|
248 | - public static function getRootPackage() |
|
249 | - { |
|
250 | - $installed = self::getInstalled(); |
|
251 | - |
|
252 | - return $installed[0]['root']; |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Returns the raw installed.php data for custom implementations |
|
257 | - * |
|
258 | - * @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect. |
|
259 | - * @return array[] |
|
260 | - * @psalm-return array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} |
|
261 | - */ |
|
262 | - public static function getRawData() |
|
263 | - { |
|
264 | - @trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED); |
|
265 | - |
|
266 | - if (null === self::$installed) { |
|
267 | - // only require the installed.php file if this file is loaded from its dumped location, |
|
268 | - // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
|
269 | - if (substr(__DIR__, -8, 1) !== 'C') { |
|
270 | - self::$installed = include __DIR__ . '/installed.php'; |
|
271 | - } else { |
|
272 | - self::$installed = array(); |
|
273 | - } |
|
274 | - } |
|
275 | - |
|
276 | - return self::$installed; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Returns the raw data of all installed.php which are currently loaded for custom implementations |
|
281 | - * |
|
282 | - * @return array[] |
|
283 | - * @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}> |
|
284 | - */ |
|
285 | - public static function getAllRawData() |
|
286 | - { |
|
287 | - return self::getInstalled(); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Lets you reload the static array from another file |
|
292 | - * |
|
293 | - * This is only useful for complex integrations in which a project needs to use |
|
294 | - * this class but then also needs to execute another project's autoloader in process, |
|
295 | - * and wants to ensure both projects have access to their version of installed.php. |
|
296 | - * |
|
297 | - * A typical case would be PHPUnit, where it would need to make sure it reads all |
|
298 | - * the data it needs from this class, then call reload() with |
|
299 | - * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure |
|
300 | - * the project in which it runs can then also use this class safely, without |
|
301 | - * interference between PHPUnit's dependencies and the project's dependencies. |
|
302 | - * |
|
303 | - * @param array[] $data A vendor/composer/installed.php data set |
|
304 | - * @return void |
|
305 | - * |
|
306 | - * @psalm-param array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $data |
|
307 | - */ |
|
308 | - public static function reload($data) |
|
309 | - { |
|
310 | - self::$installed = $data; |
|
311 | - self::$installedByVendor = array(); |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * @return array[] |
|
316 | - * @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}> |
|
317 | - */ |
|
318 | - private static function getInstalled() |
|
319 | - { |
|
320 | - if (null === self::$canGetVendors) { |
|
321 | - self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders'); |
|
322 | - } |
|
323 | - |
|
324 | - $installed = array(); |
|
325 | - |
|
326 | - if (self::$canGetVendors) { |
|
327 | - foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) { |
|
328 | - if (isset(self::$installedByVendor[$vendorDir])) { |
|
329 | - $installed[] = self::$installedByVendor[$vendorDir]; |
|
330 | - } elseif (is_file($vendorDir.'/composer/installed.php')) { |
|
331 | - $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php'; |
|
332 | - if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) { |
|
333 | - self::$installed = $installed[count($installed) - 1]; |
|
334 | - } |
|
335 | - } |
|
336 | - } |
|
337 | - } |
|
338 | - |
|
339 | - if (null === self::$installed) { |
|
340 | - // only require the installed.php file if this file is loaded from its dumped location, |
|
341 | - // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
|
342 | - if (substr(__DIR__, -8, 1) !== 'C') { |
|
343 | - self::$installed = require __DIR__ . '/installed.php'; |
|
344 | - } else { |
|
345 | - self::$installed = array(); |
|
346 | - } |
|
347 | - } |
|
348 | - $installed[] = self::$installed; |
|
349 | - |
|
350 | - return $installed; |
|
351 | - } |
|
29 | + /** |
|
30 | + * @var mixed[]|null |
|
31 | + * @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null |
|
32 | + */ |
|
33 | + private static $installed; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var bool|null |
|
37 | + */ |
|
38 | + private static $canGetVendors; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var array[] |
|
42 | + * @psalm-var array<string, array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}> |
|
43 | + */ |
|
44 | + private static $installedByVendor = array(); |
|
45 | + |
|
46 | + /** |
|
47 | + * Returns a list of all package names which are present, either by being installed, replaced or provided |
|
48 | + * |
|
49 | + * @return string[] |
|
50 | + * @psalm-return list<string> |
|
51 | + */ |
|
52 | + public static function getInstalledPackages() |
|
53 | + { |
|
54 | + $packages = array(); |
|
55 | + foreach (self::getInstalled() as $installed) { |
|
56 | + $packages[] = array_keys($installed['versions']); |
|
57 | + } |
|
58 | + |
|
59 | + if (1 === \count($packages)) { |
|
60 | + return $packages[0]; |
|
61 | + } |
|
62 | + |
|
63 | + return array_keys(array_flip(\call_user_func_array('array_merge', $packages))); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Returns a list of all package names with a specific type e.g. 'library' |
|
68 | + * |
|
69 | + * @param string $type |
|
70 | + * @return string[] |
|
71 | + * @psalm-return list<string> |
|
72 | + */ |
|
73 | + public static function getInstalledPackagesByType($type) |
|
74 | + { |
|
75 | + $packagesByType = array(); |
|
76 | + |
|
77 | + foreach (self::getInstalled() as $installed) { |
|
78 | + foreach ($installed['versions'] as $name => $package) { |
|
79 | + if (isset($package['type']) && $package['type'] === $type) { |
|
80 | + $packagesByType[] = $name; |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + return $packagesByType; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Checks whether the given package is installed |
|
90 | + * |
|
91 | + * This also returns true if the package name is provided or replaced by another package |
|
92 | + * |
|
93 | + * @param string $packageName |
|
94 | + * @param bool $includeDevRequirements |
|
95 | + * @return bool |
|
96 | + */ |
|
97 | + public static function isInstalled($packageName, $includeDevRequirements = true) |
|
98 | + { |
|
99 | + foreach (self::getInstalled() as $installed) { |
|
100 | + if (isset($installed['versions'][$packageName])) { |
|
101 | + return $includeDevRequirements || empty($installed['versions'][$packageName]['dev_requirement']); |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + return false; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Checks whether the given package satisfies a version constraint |
|
110 | + * |
|
111 | + * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call: |
|
112 | + * |
|
113 | + * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3') |
|
114 | + * |
|
115 | + * @param VersionParser $parser Install composer/semver to have access to this class and functionality |
|
116 | + * @param string $packageName |
|
117 | + * @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package |
|
118 | + * @return bool |
|
119 | + */ |
|
120 | + public static function satisfies(VersionParser $parser, $packageName, $constraint) |
|
121 | + { |
|
122 | + $constraint = $parser->parseConstraints($constraint); |
|
123 | + $provided = $parser->parseConstraints(self::getVersionRanges($packageName)); |
|
124 | + |
|
125 | + return $provided->matches($constraint); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Returns a version constraint representing all the range(s) which are installed for a given package |
|
130 | + * |
|
131 | + * It is easier to use this via isInstalled() with the $constraint argument if you need to check |
|
132 | + * whether a given version of a package is installed, and not just whether it exists |
|
133 | + * |
|
134 | + * @param string $packageName |
|
135 | + * @return string Version constraint usable with composer/semver |
|
136 | + */ |
|
137 | + public static function getVersionRanges($packageName) |
|
138 | + { |
|
139 | + foreach (self::getInstalled() as $installed) { |
|
140 | + if (!isset($installed['versions'][$packageName])) { |
|
141 | + continue; |
|
142 | + } |
|
143 | + |
|
144 | + $ranges = array(); |
|
145 | + if (isset($installed['versions'][$packageName]['pretty_version'])) { |
|
146 | + $ranges[] = $installed['versions'][$packageName]['pretty_version']; |
|
147 | + } |
|
148 | + if (array_key_exists('aliases', $installed['versions'][$packageName])) { |
|
149 | + $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']); |
|
150 | + } |
|
151 | + if (array_key_exists('replaced', $installed['versions'][$packageName])) { |
|
152 | + $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']); |
|
153 | + } |
|
154 | + if (array_key_exists('provided', $installed['versions'][$packageName])) { |
|
155 | + $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']); |
|
156 | + } |
|
157 | + |
|
158 | + return implode(' || ', $ranges); |
|
159 | + } |
|
160 | + |
|
161 | + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param string $packageName |
|
166 | + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present |
|
167 | + */ |
|
168 | + public static function getVersion($packageName) |
|
169 | + { |
|
170 | + foreach (self::getInstalled() as $installed) { |
|
171 | + if (!isset($installed['versions'][$packageName])) { |
|
172 | + continue; |
|
173 | + } |
|
174 | + |
|
175 | + if (!isset($installed['versions'][$packageName]['version'])) { |
|
176 | + return null; |
|
177 | + } |
|
178 | + |
|
179 | + return $installed['versions'][$packageName]['version']; |
|
180 | + } |
|
181 | + |
|
182 | + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @param string $packageName |
|
187 | + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present |
|
188 | + */ |
|
189 | + public static function getPrettyVersion($packageName) |
|
190 | + { |
|
191 | + foreach (self::getInstalled() as $installed) { |
|
192 | + if (!isset($installed['versions'][$packageName])) { |
|
193 | + continue; |
|
194 | + } |
|
195 | + |
|
196 | + if (!isset($installed['versions'][$packageName]['pretty_version'])) { |
|
197 | + return null; |
|
198 | + } |
|
199 | + |
|
200 | + return $installed['versions'][$packageName]['pretty_version']; |
|
201 | + } |
|
202 | + |
|
203 | + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * @param string $packageName |
|
208 | + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference |
|
209 | + */ |
|
210 | + public static function getReference($packageName) |
|
211 | + { |
|
212 | + foreach (self::getInstalled() as $installed) { |
|
213 | + if (!isset($installed['versions'][$packageName])) { |
|
214 | + continue; |
|
215 | + } |
|
216 | + |
|
217 | + if (!isset($installed['versions'][$packageName]['reference'])) { |
|
218 | + return null; |
|
219 | + } |
|
220 | + |
|
221 | + return $installed['versions'][$packageName]['reference']; |
|
222 | + } |
|
223 | + |
|
224 | + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * @param string $packageName |
|
229 | + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path. |
|
230 | + */ |
|
231 | + public static function getInstallPath($packageName) |
|
232 | + { |
|
233 | + foreach (self::getInstalled() as $installed) { |
|
234 | + if (!isset($installed['versions'][$packageName])) { |
|
235 | + continue; |
|
236 | + } |
|
237 | + |
|
238 | + return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null; |
|
239 | + } |
|
240 | + |
|
241 | + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @return array |
|
246 | + * @psalm-return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool} |
|
247 | + */ |
|
248 | + public static function getRootPackage() |
|
249 | + { |
|
250 | + $installed = self::getInstalled(); |
|
251 | + |
|
252 | + return $installed[0]['root']; |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Returns the raw installed.php data for custom implementations |
|
257 | + * |
|
258 | + * @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect. |
|
259 | + * @return array[] |
|
260 | + * @psalm-return array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} |
|
261 | + */ |
|
262 | + public static function getRawData() |
|
263 | + { |
|
264 | + @trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED); |
|
265 | + |
|
266 | + if (null === self::$installed) { |
|
267 | + // only require the installed.php file if this file is loaded from its dumped location, |
|
268 | + // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
|
269 | + if (substr(__DIR__, -8, 1) !== 'C') { |
|
270 | + self::$installed = include __DIR__ . '/installed.php'; |
|
271 | + } else { |
|
272 | + self::$installed = array(); |
|
273 | + } |
|
274 | + } |
|
275 | + |
|
276 | + return self::$installed; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Returns the raw data of all installed.php which are currently loaded for custom implementations |
|
281 | + * |
|
282 | + * @return array[] |
|
283 | + * @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}> |
|
284 | + */ |
|
285 | + public static function getAllRawData() |
|
286 | + { |
|
287 | + return self::getInstalled(); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Lets you reload the static array from another file |
|
292 | + * |
|
293 | + * This is only useful for complex integrations in which a project needs to use |
|
294 | + * this class but then also needs to execute another project's autoloader in process, |
|
295 | + * and wants to ensure both projects have access to their version of installed.php. |
|
296 | + * |
|
297 | + * A typical case would be PHPUnit, where it would need to make sure it reads all |
|
298 | + * the data it needs from this class, then call reload() with |
|
299 | + * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure |
|
300 | + * the project in which it runs can then also use this class safely, without |
|
301 | + * interference between PHPUnit's dependencies and the project's dependencies. |
|
302 | + * |
|
303 | + * @param array[] $data A vendor/composer/installed.php data set |
|
304 | + * @return void |
|
305 | + * |
|
306 | + * @psalm-param array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $data |
|
307 | + */ |
|
308 | + public static function reload($data) |
|
309 | + { |
|
310 | + self::$installed = $data; |
|
311 | + self::$installedByVendor = array(); |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * @return array[] |
|
316 | + * @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}> |
|
317 | + */ |
|
318 | + private static function getInstalled() |
|
319 | + { |
|
320 | + if (null === self::$canGetVendors) { |
|
321 | + self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders'); |
|
322 | + } |
|
323 | + |
|
324 | + $installed = array(); |
|
325 | + |
|
326 | + if (self::$canGetVendors) { |
|
327 | + foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) { |
|
328 | + if (isset(self::$installedByVendor[$vendorDir])) { |
|
329 | + $installed[] = self::$installedByVendor[$vendorDir]; |
|
330 | + } elseif (is_file($vendorDir.'/composer/installed.php')) { |
|
331 | + $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php'; |
|
332 | + if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) { |
|
333 | + self::$installed = $installed[count($installed) - 1]; |
|
334 | + } |
|
335 | + } |
|
336 | + } |
|
337 | + } |
|
338 | + |
|
339 | + if (null === self::$installed) { |
|
340 | + // only require the installed.php file if this file is loaded from its dumped location, |
|
341 | + // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
|
342 | + if (substr(__DIR__, -8, 1) !== 'C') { |
|
343 | + self::$installed = require __DIR__ . '/installed.php'; |
|
344 | + } else { |
|
345 | + self::$installed = array(); |
|
346 | + } |
|
347 | + } |
|
348 | + $installed[] = self::$installed; |
|
349 | + |
|
350 | + return $installed; |
|
351 | + } |
|
352 | 352 | } |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | // used by display strategies |
95 | 95 | // assign incoming values to properties |
96 | 96 | foreach ($options_array as $key => $value) { |
97 | - $key = '_' . $key; |
|
97 | + $key = '_'.$key; |
|
98 | 98 | if (property_exists($this, $key) && empty($this->{$key})) { |
99 | 99 | $this->{$key} = $value; |
100 | 100 | if ($key === '_subsections' && ! is_array($value)) { |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | */ |
135 | 135 | public function ensure_construct_finalized_called() |
136 | 136 | { |
137 | - if (! $this->_construction_finalized) { |
|
137 | + if ( ! $this->_construction_finalized) { |
|
138 | 138 | $this->_construct_finalize($this->_parent_section, $this->_name); |
139 | 139 | } |
140 | 140 | } |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | */ |
198 | 198 | protected function _set_default_html_id_if_empty() |
199 | 199 | { |
200 | - if (! $this->_html_id) { |
|
200 | + if ( ! $this->_html_id) { |
|
201 | 201 | if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
202 | 202 | $this->_html_id = $this->_parent_section->html_id() |
203 | 203 | . '-' |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | public function html_id($add_pound_sign = false) |
257 | 257 | { |
258 | 258 | $this->_set_default_html_id_if_empty(); |
259 | - return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
259 | + return $add_pound_sign ? '#'.$this->_html_id : $this->_html_id; |
|
260 | 260 | } |
261 | 261 | |
262 | 262 | |
@@ -326,7 +326,7 @@ discard block |
||
326 | 326 | */ |
327 | 327 | public function other_html_attributes() |
328 | 328 | { |
329 | - return ! empty($this->_other_html_attributes) ? ' ' . $this->_other_html_attributes : ''; |
|
329 | + return ! empty($this->_other_html_attributes) ? ' '.$this->_other_html_attributes : ''; |
|
330 | 330 | } |
331 | 331 | |
332 | 332 | |
@@ -339,7 +339,7 @@ discard block |
||
339 | 339 | */ |
340 | 340 | public function name() |
341 | 341 | { |
342 | - if (! $this->_construction_finalized) { |
|
342 | + if ( ! $this->_construction_finalized) { |
|
343 | 343 | throw new EE_Error(sprintf(esc_html__( |
344 | 344 | 'You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
345 | 345 | 'event_espresso' |
@@ -372,18 +372,18 @@ discard block |
||
372 | 372 | */ |
373 | 373 | public function form_open($action = '', $method = '', $other_attributes = '') |
374 | 374 | { |
375 | - if (! empty($action)) { |
|
375 | + if ( ! empty($action)) { |
|
376 | 376 | $this->set_action($action); |
377 | 377 | } |
378 | - if (! empty($method)) { |
|
378 | + if ( ! empty($method)) { |
|
379 | 379 | $this->set_method($method); |
380 | 380 | } |
381 | - $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
382 | - $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
383 | - $html .= ' action="' . esc_url_raw($this->action()) . '"'; |
|
384 | - $html .= ' method="' . $this->method() . '"'; |
|
385 | - $html .= ' name="' . $this->name() . '"'; |
|
386 | - $html .= ' ' . $other_attributes . '>'; |
|
381 | + $html = EEH_HTML::nl(1, 'form').'<form'; |
|
382 | + $html .= $this->html_id() !== '' ? ' id="'.$this->get_html_id_for_form($this->html_id()).'"' : ''; |
|
383 | + $html .= ' action="'.esc_url_raw($this->action()).'"'; |
|
384 | + $html .= ' method="'.$this->method().'"'; |
|
385 | + $html .= ' name="'.$this->name().'"'; |
|
386 | + $html .= ' '.$other_attributes.'>'; |
|
387 | 387 | return $html; |
388 | 388 | } |
389 | 389 | |
@@ -401,7 +401,7 @@ discard block |
||
401 | 401 | $strlen = strlen($html_id); |
402 | 402 | $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4 |
403 | 403 | ? $html_id |
404 | - : $html_id . '-frm'; |
|
404 | + : $html_id.'-frm'; |
|
405 | 405 | return $html_id; |
406 | 406 | } |
407 | 407 |
@@ -14,484 +14,484 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class EE_Form_Section_Base |
16 | 16 | { |
17 | - /** |
|
18 | - * the URL the form is submitted to |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $_action = ''; |
|
23 | - |
|
24 | - /** |
|
25 | - * POST (default) or GET |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $_method = ''; |
|
30 | - |
|
31 | - /** |
|
32 | - * html_id and html_name are derived from this by default |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $_name = ''; |
|
37 | - |
|
38 | - /** |
|
39 | - * $_html_id |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - protected $_html_id = ''; |
|
43 | - |
|
44 | - /** |
|
45 | - * $_html_class |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $_html_class = ''; |
|
49 | - |
|
50 | - /** |
|
51 | - * $_html_style |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $_html_style = ''; |
|
55 | - |
|
56 | - /** |
|
57 | - * $_other_html_attributes |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $_other_html_attributes = ''; |
|
61 | - |
|
62 | - /** |
|
63 | - * The form section of which this form section is a part |
|
64 | - * |
|
65 | - * @var EE_Form_Section_Proper |
|
66 | - */ |
|
67 | - protected $_parent_section; |
|
68 | - |
|
69 | - /** |
|
70 | - * flag indicating that _construct_finalize has been called. |
|
71 | - * If it has not been called and we try to use functions which require it, we call it |
|
72 | - * with no parameters. But normally, _construct_finalize should be called by the instantiating class |
|
73 | - * |
|
74 | - * @var bool |
|
75 | - */ |
|
76 | - protected $_construction_finalized = false; |
|
77 | - |
|
78 | - /** |
|
79 | - * Strategy for parsing the form HTML upon display |
|
80 | - * |
|
81 | - * @var FormHtmlFilter |
|
82 | - */ |
|
83 | - protected $_form_html_filter; |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * @param array $options_array { |
|
88 | - * @type $name string the name for this form section, if you want to explicitly define it |
|
89 | - * } |
|
90 | - * @throws InvalidDataTypeException |
|
91 | - */ |
|
92 | - public function __construct($options_array = array()) |
|
93 | - { |
|
94 | - // used by display strategies |
|
95 | - // assign incoming values to properties |
|
96 | - foreach ($options_array as $key => $value) { |
|
97 | - $key = '_' . $key; |
|
98 | - if (property_exists($this, $key) && empty($this->{$key})) { |
|
99 | - $this->{$key} = $value; |
|
100 | - if ($key === '_subsections' && ! is_array($value)) { |
|
101 | - throw new InvalidDataTypeException($key, $value, 'array'); |
|
102 | - } |
|
103 | - } |
|
104 | - } |
|
105 | - // set parser which allows the form section's rendered HTML to be filtered |
|
106 | - if (isset($options_array['form_html_filter']) && $options_array['form_html_filter'] instanceof FormHtmlFilter) { |
|
107 | - $this->_form_html_filter = $options_array['form_html_filter']; |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * @param $parent_form_section |
|
115 | - * @param $name |
|
116 | - */ |
|
117 | - protected function _construct_finalize($parent_form_section, $name) |
|
118 | - { |
|
119 | - $this->_construction_finalized = true; |
|
120 | - $this->_parent_section = $parent_form_section; |
|
121 | - if ($name !== null) { |
|
122 | - $this->_name = $name; |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * make sure construction finalized was called, otherwise children might not be ready |
|
130 | - * |
|
131 | - * @return void |
|
132 | - */ |
|
133 | - public function ensure_construct_finalized_called() |
|
134 | - { |
|
135 | - if (! $this->_construction_finalized) { |
|
136 | - $this->_construct_finalize($this->_parent_section, $this->_name); |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function action() |
|
146 | - { |
|
147 | - return $this->_action; |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * @param string $action |
|
154 | - */ |
|
155 | - public function set_action($action) |
|
156 | - { |
|
157 | - $this->_action = $action; |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * @return string |
|
164 | - */ |
|
165 | - public function method() |
|
166 | - { |
|
167 | - return ! empty($this->_method) ? $this->_method : 'POST'; |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * @param string $method |
|
174 | - */ |
|
175 | - public function set_method($method) |
|
176 | - { |
|
177 | - switch ($method) { |
|
178 | - case 'get': |
|
179 | - case 'GET': |
|
180 | - $this->_method = 'GET'; |
|
181 | - break; |
|
182 | - default: |
|
183 | - $this->_method = 'POST'; |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - |
|
188 | - |
|
189 | - /** |
|
190 | - * Sets the html_id to its default value, if none was specified in the constructor. |
|
191 | - * Calculation involves using the name and the parent's html id |
|
192 | - * return void |
|
193 | - * |
|
194 | - * @throws \EE_Error |
|
195 | - */ |
|
196 | - protected function _set_default_html_id_if_empty() |
|
197 | - { |
|
198 | - if (! $this->_html_id) { |
|
199 | - if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
200 | - $this->_html_id = $this->_parent_section->html_id() |
|
201 | - . '-' |
|
202 | - . $this->_prep_name_for_html_id($this->name()); |
|
203 | - } else { |
|
204 | - $this->_html_id = $this->_prep_name_for_html_id($this->name()); |
|
205 | - } |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - |
|
211 | - /** |
|
212 | - * _prep_name_for_html_id |
|
213 | - * |
|
214 | - * @param $name |
|
215 | - * @return string |
|
216 | - */ |
|
217 | - private function _prep_name_for_html_id($name) |
|
218 | - { |
|
219 | - return sanitize_key(str_replace(array(' ', ' ', '_'), '-', $name)); |
|
220 | - } |
|
17 | + /** |
|
18 | + * the URL the form is submitted to |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $_action = ''; |
|
23 | + |
|
24 | + /** |
|
25 | + * POST (default) or GET |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $_method = ''; |
|
30 | + |
|
31 | + /** |
|
32 | + * html_id and html_name are derived from this by default |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $_name = ''; |
|
37 | + |
|
38 | + /** |
|
39 | + * $_html_id |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + protected $_html_id = ''; |
|
43 | + |
|
44 | + /** |
|
45 | + * $_html_class |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $_html_class = ''; |
|
49 | + |
|
50 | + /** |
|
51 | + * $_html_style |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $_html_style = ''; |
|
55 | + |
|
56 | + /** |
|
57 | + * $_other_html_attributes |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $_other_html_attributes = ''; |
|
61 | + |
|
62 | + /** |
|
63 | + * The form section of which this form section is a part |
|
64 | + * |
|
65 | + * @var EE_Form_Section_Proper |
|
66 | + */ |
|
67 | + protected $_parent_section; |
|
68 | + |
|
69 | + /** |
|
70 | + * flag indicating that _construct_finalize has been called. |
|
71 | + * If it has not been called and we try to use functions which require it, we call it |
|
72 | + * with no parameters. But normally, _construct_finalize should be called by the instantiating class |
|
73 | + * |
|
74 | + * @var bool |
|
75 | + */ |
|
76 | + protected $_construction_finalized = false; |
|
77 | + |
|
78 | + /** |
|
79 | + * Strategy for parsing the form HTML upon display |
|
80 | + * |
|
81 | + * @var FormHtmlFilter |
|
82 | + */ |
|
83 | + protected $_form_html_filter; |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * @param array $options_array { |
|
88 | + * @type $name string the name for this form section, if you want to explicitly define it |
|
89 | + * } |
|
90 | + * @throws InvalidDataTypeException |
|
91 | + */ |
|
92 | + public function __construct($options_array = array()) |
|
93 | + { |
|
94 | + // used by display strategies |
|
95 | + // assign incoming values to properties |
|
96 | + foreach ($options_array as $key => $value) { |
|
97 | + $key = '_' . $key; |
|
98 | + if (property_exists($this, $key) && empty($this->{$key})) { |
|
99 | + $this->{$key} = $value; |
|
100 | + if ($key === '_subsections' && ! is_array($value)) { |
|
101 | + throw new InvalidDataTypeException($key, $value, 'array'); |
|
102 | + } |
|
103 | + } |
|
104 | + } |
|
105 | + // set parser which allows the form section's rendered HTML to be filtered |
|
106 | + if (isset($options_array['form_html_filter']) && $options_array['form_html_filter'] instanceof FormHtmlFilter) { |
|
107 | + $this->_form_html_filter = $options_array['form_html_filter']; |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * @param $parent_form_section |
|
115 | + * @param $name |
|
116 | + */ |
|
117 | + protected function _construct_finalize($parent_form_section, $name) |
|
118 | + { |
|
119 | + $this->_construction_finalized = true; |
|
120 | + $this->_parent_section = $parent_form_section; |
|
121 | + if ($name !== null) { |
|
122 | + $this->_name = $name; |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * make sure construction finalized was called, otherwise children might not be ready |
|
130 | + * |
|
131 | + * @return void |
|
132 | + */ |
|
133 | + public function ensure_construct_finalized_called() |
|
134 | + { |
|
135 | + if (! $this->_construction_finalized) { |
|
136 | + $this->_construct_finalize($this->_parent_section, $this->_name); |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function action() |
|
146 | + { |
|
147 | + return $this->_action; |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * @param string $action |
|
154 | + */ |
|
155 | + public function set_action($action) |
|
156 | + { |
|
157 | + $this->_action = $action; |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * @return string |
|
164 | + */ |
|
165 | + public function method() |
|
166 | + { |
|
167 | + return ! empty($this->_method) ? $this->_method : 'POST'; |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * @param string $method |
|
174 | + */ |
|
175 | + public function set_method($method) |
|
176 | + { |
|
177 | + switch ($method) { |
|
178 | + case 'get': |
|
179 | + case 'GET': |
|
180 | + $this->_method = 'GET'; |
|
181 | + break; |
|
182 | + default: |
|
183 | + $this->_method = 'POST'; |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + |
|
188 | + |
|
189 | + /** |
|
190 | + * Sets the html_id to its default value, if none was specified in the constructor. |
|
191 | + * Calculation involves using the name and the parent's html id |
|
192 | + * return void |
|
193 | + * |
|
194 | + * @throws \EE_Error |
|
195 | + */ |
|
196 | + protected function _set_default_html_id_if_empty() |
|
197 | + { |
|
198 | + if (! $this->_html_id) { |
|
199 | + if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
200 | + $this->_html_id = $this->_parent_section->html_id() |
|
201 | + . '-' |
|
202 | + . $this->_prep_name_for_html_id($this->name()); |
|
203 | + } else { |
|
204 | + $this->_html_id = $this->_prep_name_for_html_id($this->name()); |
|
205 | + } |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + |
|
211 | + /** |
|
212 | + * _prep_name_for_html_id |
|
213 | + * |
|
214 | + * @param $name |
|
215 | + * @return string |
|
216 | + */ |
|
217 | + private function _prep_name_for_html_id($name) |
|
218 | + { |
|
219 | + return sanitize_key(str_replace(array(' ', ' ', '_'), '-', $name)); |
|
220 | + } |
|
221 | 221 | |
222 | 222 | |
223 | 223 | |
224 | - /** |
|
225 | - * Returns the HTML, JS, and CSS necessary to display this form section on a page. |
|
226 | - * Note however, it's recommended that you instead call enqueue_js on the "wp_enqueue_scripts" action, |
|
227 | - * and call get_html when you want to output the html. Calling get_html_and_js after |
|
228 | - * "wp_enqueue_scripts" has already fired seems to work for now, but is contrary |
|
229 | - * to the instructions on https://developer.wordpress.org/reference/functions/wp_enqueue_script/ |
|
230 | - * and so might stop working anytime. |
|
231 | - * |
|
232 | - * @return string |
|
233 | - */ |
|
234 | - public function get_html_and_js() |
|
235 | - { |
|
236 | - return $this->get_html(); |
|
237 | - } |
|
224 | + /** |
|
225 | + * Returns the HTML, JS, and CSS necessary to display this form section on a page. |
|
226 | + * Note however, it's recommended that you instead call enqueue_js on the "wp_enqueue_scripts" action, |
|
227 | + * and call get_html when you want to output the html. Calling get_html_and_js after |
|
228 | + * "wp_enqueue_scripts" has already fired seems to work for now, but is contrary |
|
229 | + * to the instructions on https://developer.wordpress.org/reference/functions/wp_enqueue_script/ |
|
230 | + * and so might stop working anytime. |
|
231 | + * |
|
232 | + * @return string |
|
233 | + */ |
|
234 | + public function get_html_and_js() |
|
235 | + { |
|
236 | + return $this->get_html(); |
|
237 | + } |
|
238 | 238 | |
239 | 239 | |
240 | - |
|
241 | - /** |
|
242 | - * Gets the HTML for displaying this form section |
|
243 | - * |
|
244 | - * @return string |
|
245 | - */ |
|
246 | - abstract public function get_html(); |
|
247 | - |
|
248 | - |
|
249 | - /** |
|
250 | - * @param bool $add_pound_sign |
|
251 | - * @return string |
|
252 | - * @throws EE_Error |
|
253 | - */ |
|
254 | - public function html_id($add_pound_sign = false) |
|
255 | - { |
|
256 | - $this->_set_default_html_id_if_empty(); |
|
257 | - return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * @return string |
|
264 | - */ |
|
265 | - public function html_class() |
|
266 | - { |
|
267 | - return $this->_html_class; |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - |
|
272 | - /** |
|
273 | - * @return string |
|
274 | - */ |
|
275 | - public function html_style() |
|
276 | - { |
|
277 | - return $this->_html_style; |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * @param mixed $html_class |
|
284 | - */ |
|
285 | - public function set_html_class($html_class) |
|
286 | - { |
|
287 | - $this->_html_class = $html_class; |
|
288 | - } |
|
289 | - |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * @param mixed $html_id |
|
294 | - */ |
|
295 | - public function set_html_id($html_id) |
|
296 | - { |
|
297 | - $this->_html_id = $html_id; |
|
298 | - } |
|
299 | - |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * @param mixed $html_style |
|
304 | - */ |
|
305 | - public function set_html_style($html_style) |
|
306 | - { |
|
307 | - $this->_html_style = $html_style; |
|
308 | - } |
|
309 | - |
|
310 | - |
|
311 | - |
|
312 | - /** |
|
313 | - * @param string $other_html_attributes |
|
314 | - */ |
|
315 | - public function set_other_html_attributes($other_html_attributes) |
|
316 | - { |
|
317 | - $this->_other_html_attributes = $other_html_attributes ?? ''; |
|
318 | - } |
|
319 | - |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * @return string |
|
324 | - */ |
|
325 | - public function other_html_attributes() |
|
326 | - { |
|
327 | - return ! empty($this->_other_html_attributes) ? ' ' . $this->_other_html_attributes : ''; |
|
328 | - } |
|
329 | - |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * Gets the name of the form section. This is not the same as the HTML name. |
|
334 | - * |
|
335 | - * @throws EE_Error |
|
336 | - * @return string |
|
337 | - */ |
|
338 | - public function name() |
|
339 | - { |
|
340 | - if (! $this->_construction_finalized) { |
|
341 | - throw new EE_Error(sprintf(esc_html__( |
|
342 | - 'You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
|
343 | - 'event_espresso' |
|
344 | - ), get_class($this))); |
|
345 | - } |
|
346 | - return $this->_name; |
|
347 | - } |
|
348 | - |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * Gets the parent section |
|
353 | - * |
|
354 | - * @return EE_Form_Section_Proper |
|
355 | - */ |
|
356 | - public function parent_section() |
|
357 | - { |
|
358 | - return $this->_parent_section; |
|
359 | - } |
|
360 | - |
|
361 | - |
|
362 | - /** |
|
363 | - * returns HTML for generating the opening form HTML tag (<form>) |
|
364 | - * |
|
365 | - * @param string $action the URL the form is submitted to |
|
366 | - * @param string $method POST (default) or GET |
|
367 | - * @param string $other_attributes anything else added to the form open tag, MUST BE VALID HTML |
|
368 | - * @return string |
|
369 | - * @throws EE_Error |
|
370 | - */ |
|
371 | - public function form_open($action = '', $method = '', $other_attributes = '') |
|
372 | - { |
|
373 | - if (! empty($action)) { |
|
374 | - $this->set_action($action); |
|
375 | - } |
|
376 | - if (! empty($method)) { |
|
377 | - $this->set_method($method); |
|
378 | - } |
|
379 | - $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
380 | - $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
381 | - $html .= ' action="' . esc_url_raw($this->action()) . '"'; |
|
382 | - $html .= ' method="' . $this->method() . '"'; |
|
383 | - $html .= ' name="' . $this->name() . '"'; |
|
384 | - $html .= ' ' . $other_attributes . '>'; |
|
385 | - return $html; |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - |
|
390 | - /** |
|
391 | - * ensures that html id for form either ends in "-form" or "-frm" |
|
392 | - * so that id doesn't conflict/collide with other elements |
|
393 | - * |
|
394 | - * @param string $html_id |
|
395 | - * @return string |
|
396 | - */ |
|
397 | - protected function get_html_id_for_form($html_id) |
|
398 | - { |
|
399 | - $strlen = strlen($html_id); |
|
400 | - $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4 |
|
401 | - ? $html_id |
|
402 | - : $html_id . '-frm'; |
|
403 | - return $html_id; |
|
404 | - } |
|
405 | - |
|
406 | - |
|
407 | - /** |
|
408 | - * returns HTML for generating the closing form HTML tag (</form>) |
|
409 | - * |
|
410 | - * @return string |
|
411 | - * @throws EE_Error |
|
412 | - */ |
|
413 | - public function form_close() |
|
414 | - { |
|
415 | - return EEH_HTML::nl(-1, 'form') |
|
416 | - . '</form>' |
|
417 | - . EEH_HTML::nl() |
|
418 | - . '<!-- end of ee-' |
|
419 | - . $this->html_id() |
|
420 | - . '-form -->' |
|
421 | - . EEH_HTML::nl(); |
|
422 | - } |
|
423 | - |
|
424 | - |
|
425 | - |
|
426 | - /** |
|
427 | - * enqueues JS (and CSS) for the form (ie immediately call wp_enqueue_script and |
|
428 | - * wp_enqueue_style; the scripts could have optionally been registered earlier) |
|
429 | - * Default does nothing, but child classes can override |
|
430 | - * |
|
431 | - * @return void |
|
432 | - */ |
|
433 | - public function enqueue_js() |
|
434 | - { |
|
435 | - // defaults to enqueue NO js or css |
|
436 | - } |
|
437 | - |
|
438 | - |
|
439 | - |
|
440 | - /** |
|
441 | - * Adds any extra data needed by js. Eventually we'll call wp_localize_script |
|
442 | - * with it, and it will be on each form section's 'other_data' property. |
|
443 | - * By default nothing is added, but child classes can extend this method to add something. |
|
444 | - * Eg, if you have an input that will cause a modal dialog to appear, |
|
445 | - * here you could add an entry like 'modal_dialog_inputs' to this array |
|
446 | - * to map between the input's html ID and the modal dialogue's ID, so that |
|
447 | - * your JS code will know where to find the modal dialog when the input is pressed. |
|
448 | - * Eg $form_other_js_data['modal_dialog_inputs']['some-input-id']='modal-dialog-id'; |
|
449 | - * |
|
450 | - * @param array $form_other_js_data |
|
451 | - * @return array |
|
452 | - */ |
|
453 | - public function get_other_js_data($form_other_js_data = array()) |
|
454 | - { |
|
455 | - return $form_other_js_data; |
|
456 | - } |
|
457 | - |
|
458 | - |
|
459 | - |
|
460 | - /** |
|
461 | - * This isn't just the name of an input, it's a path pointing to an input. The |
|
462 | - * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
463 | - * dot-dot-slash (../) means to ascend into the parent section. |
|
464 | - * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
465 | - * which will be returned. |
|
466 | - * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
467 | - * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
468 | - * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
469 | - * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
470 | - * Etc |
|
471 | - * |
|
472 | - * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
473 | - * @return EE_Form_Section_Base |
|
474 | - */ |
|
475 | - public function find_section_from_path($form_section_path) |
|
476 | - { |
|
477 | - if (strpos($form_section_path, '/') === 0) { |
|
478 | - $form_section_path = substr($form_section_path, strlen('/')); |
|
479 | - } |
|
480 | - if (empty($form_section_path)) { |
|
481 | - return $this; |
|
482 | - } |
|
483 | - if (strpos($form_section_path, '../') === 0) { |
|
484 | - $parent = $this->parent_section(); |
|
485 | - $form_section_path = substr($form_section_path, strlen('../')); |
|
486 | - if ($parent instanceof EE_Form_Section_Base) { |
|
487 | - return $parent->find_section_from_path($form_section_path); |
|
488 | - } |
|
489 | - if (empty($form_section_path)) { |
|
490 | - return $this; |
|
491 | - } |
|
492 | - } |
|
493 | - // couldn't find it using simple parent following |
|
494 | - return null; |
|
495 | - } |
|
240 | + |
|
241 | + /** |
|
242 | + * Gets the HTML for displaying this form section |
|
243 | + * |
|
244 | + * @return string |
|
245 | + */ |
|
246 | + abstract public function get_html(); |
|
247 | + |
|
248 | + |
|
249 | + /** |
|
250 | + * @param bool $add_pound_sign |
|
251 | + * @return string |
|
252 | + * @throws EE_Error |
|
253 | + */ |
|
254 | + public function html_id($add_pound_sign = false) |
|
255 | + { |
|
256 | + $this->_set_default_html_id_if_empty(); |
|
257 | + return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * @return string |
|
264 | + */ |
|
265 | + public function html_class() |
|
266 | + { |
|
267 | + return $this->_html_class; |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + |
|
272 | + /** |
|
273 | + * @return string |
|
274 | + */ |
|
275 | + public function html_style() |
|
276 | + { |
|
277 | + return $this->_html_style; |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * @param mixed $html_class |
|
284 | + */ |
|
285 | + public function set_html_class($html_class) |
|
286 | + { |
|
287 | + $this->_html_class = $html_class; |
|
288 | + } |
|
289 | + |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * @param mixed $html_id |
|
294 | + */ |
|
295 | + public function set_html_id($html_id) |
|
296 | + { |
|
297 | + $this->_html_id = $html_id; |
|
298 | + } |
|
299 | + |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * @param mixed $html_style |
|
304 | + */ |
|
305 | + public function set_html_style($html_style) |
|
306 | + { |
|
307 | + $this->_html_style = $html_style; |
|
308 | + } |
|
309 | + |
|
310 | + |
|
311 | + |
|
312 | + /** |
|
313 | + * @param string $other_html_attributes |
|
314 | + */ |
|
315 | + public function set_other_html_attributes($other_html_attributes) |
|
316 | + { |
|
317 | + $this->_other_html_attributes = $other_html_attributes ?? ''; |
|
318 | + } |
|
319 | + |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * @return string |
|
324 | + */ |
|
325 | + public function other_html_attributes() |
|
326 | + { |
|
327 | + return ! empty($this->_other_html_attributes) ? ' ' . $this->_other_html_attributes : ''; |
|
328 | + } |
|
329 | + |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * Gets the name of the form section. This is not the same as the HTML name. |
|
334 | + * |
|
335 | + * @throws EE_Error |
|
336 | + * @return string |
|
337 | + */ |
|
338 | + public function name() |
|
339 | + { |
|
340 | + if (! $this->_construction_finalized) { |
|
341 | + throw new EE_Error(sprintf(esc_html__( |
|
342 | + 'You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
|
343 | + 'event_espresso' |
|
344 | + ), get_class($this))); |
|
345 | + } |
|
346 | + return $this->_name; |
|
347 | + } |
|
348 | + |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * Gets the parent section |
|
353 | + * |
|
354 | + * @return EE_Form_Section_Proper |
|
355 | + */ |
|
356 | + public function parent_section() |
|
357 | + { |
|
358 | + return $this->_parent_section; |
|
359 | + } |
|
360 | + |
|
361 | + |
|
362 | + /** |
|
363 | + * returns HTML for generating the opening form HTML tag (<form>) |
|
364 | + * |
|
365 | + * @param string $action the URL the form is submitted to |
|
366 | + * @param string $method POST (default) or GET |
|
367 | + * @param string $other_attributes anything else added to the form open tag, MUST BE VALID HTML |
|
368 | + * @return string |
|
369 | + * @throws EE_Error |
|
370 | + */ |
|
371 | + public function form_open($action = '', $method = '', $other_attributes = '') |
|
372 | + { |
|
373 | + if (! empty($action)) { |
|
374 | + $this->set_action($action); |
|
375 | + } |
|
376 | + if (! empty($method)) { |
|
377 | + $this->set_method($method); |
|
378 | + } |
|
379 | + $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
380 | + $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
381 | + $html .= ' action="' . esc_url_raw($this->action()) . '"'; |
|
382 | + $html .= ' method="' . $this->method() . '"'; |
|
383 | + $html .= ' name="' . $this->name() . '"'; |
|
384 | + $html .= ' ' . $other_attributes . '>'; |
|
385 | + return $html; |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + |
|
390 | + /** |
|
391 | + * ensures that html id for form either ends in "-form" or "-frm" |
|
392 | + * so that id doesn't conflict/collide with other elements |
|
393 | + * |
|
394 | + * @param string $html_id |
|
395 | + * @return string |
|
396 | + */ |
|
397 | + protected function get_html_id_for_form($html_id) |
|
398 | + { |
|
399 | + $strlen = strlen($html_id); |
|
400 | + $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4 |
|
401 | + ? $html_id |
|
402 | + : $html_id . '-frm'; |
|
403 | + return $html_id; |
|
404 | + } |
|
405 | + |
|
406 | + |
|
407 | + /** |
|
408 | + * returns HTML for generating the closing form HTML tag (</form>) |
|
409 | + * |
|
410 | + * @return string |
|
411 | + * @throws EE_Error |
|
412 | + */ |
|
413 | + public function form_close() |
|
414 | + { |
|
415 | + return EEH_HTML::nl(-1, 'form') |
|
416 | + . '</form>' |
|
417 | + . EEH_HTML::nl() |
|
418 | + . '<!-- end of ee-' |
|
419 | + . $this->html_id() |
|
420 | + . '-form -->' |
|
421 | + . EEH_HTML::nl(); |
|
422 | + } |
|
423 | + |
|
424 | + |
|
425 | + |
|
426 | + /** |
|
427 | + * enqueues JS (and CSS) for the form (ie immediately call wp_enqueue_script and |
|
428 | + * wp_enqueue_style; the scripts could have optionally been registered earlier) |
|
429 | + * Default does nothing, but child classes can override |
|
430 | + * |
|
431 | + * @return void |
|
432 | + */ |
|
433 | + public function enqueue_js() |
|
434 | + { |
|
435 | + // defaults to enqueue NO js or css |
|
436 | + } |
|
437 | + |
|
438 | + |
|
439 | + |
|
440 | + /** |
|
441 | + * Adds any extra data needed by js. Eventually we'll call wp_localize_script |
|
442 | + * with it, and it will be on each form section's 'other_data' property. |
|
443 | + * By default nothing is added, but child classes can extend this method to add something. |
|
444 | + * Eg, if you have an input that will cause a modal dialog to appear, |
|
445 | + * here you could add an entry like 'modal_dialog_inputs' to this array |
|
446 | + * to map between the input's html ID and the modal dialogue's ID, so that |
|
447 | + * your JS code will know where to find the modal dialog when the input is pressed. |
|
448 | + * Eg $form_other_js_data['modal_dialog_inputs']['some-input-id']='modal-dialog-id'; |
|
449 | + * |
|
450 | + * @param array $form_other_js_data |
|
451 | + * @return array |
|
452 | + */ |
|
453 | + public function get_other_js_data($form_other_js_data = array()) |
|
454 | + { |
|
455 | + return $form_other_js_data; |
|
456 | + } |
|
457 | + |
|
458 | + |
|
459 | + |
|
460 | + /** |
|
461 | + * This isn't just the name of an input, it's a path pointing to an input. The |
|
462 | + * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
463 | + * dot-dot-slash (../) means to ascend into the parent section. |
|
464 | + * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
465 | + * which will be returned. |
|
466 | + * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
467 | + * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
468 | + * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
469 | + * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
470 | + * Etc |
|
471 | + * |
|
472 | + * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
473 | + * @return EE_Form_Section_Base |
|
474 | + */ |
|
475 | + public function find_section_from_path($form_section_path) |
|
476 | + { |
|
477 | + if (strpos($form_section_path, '/') === 0) { |
|
478 | + $form_section_path = substr($form_section_path, strlen('/')); |
|
479 | + } |
|
480 | + if (empty($form_section_path)) { |
|
481 | + return $this; |
|
482 | + } |
|
483 | + if (strpos($form_section_path, '../') === 0) { |
|
484 | + $parent = $this->parent_section(); |
|
485 | + $form_section_path = substr($form_section_path, strlen('../')); |
|
486 | + if ($parent instanceof EE_Form_Section_Base) { |
|
487 | + return $parent->find_section_from_path($form_section_path); |
|
488 | + } |
|
489 | + if (empty($form_section_path)) { |
|
490 | + return $this; |
|
491 | + } |
|
492 | + } |
|
493 | + // couldn't find it using simple parent following |
|
494 | + return null; |
|
495 | + } |
|
496 | 496 | } |
497 | 497 | // End of file EE_Form_Section_Base.form.php |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | public function ensureTableNameHasPrefix(string $table_name): string |
38 | 38 | { |
39 | 39 | global $wpdb; |
40 | - return strpos($table_name, $wpdb->base_prefix) === 0 ? $table_name : $wpdb->prefix . $table_name; |
|
40 | + return strpos($table_name, $wpdb->base_prefix) === 0 ? $table_name : $wpdb->prefix.$table_name; |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | $wpdb->last_error = $old_error; |
86 | 86 | $EZSQL_ERROR = $ezsql_error_cache; |
87 | 87 | // if there was a table doesn't exist error |
88 | - if (! empty($new_error)) { |
|
88 | + if ( ! empty($new_error)) { |
|
89 | 89 | if ( |
90 | 90 | in_array( |
91 | 91 | EEH_Activation::last_wpdb_error_code(), |
@@ -17,118 +17,118 @@ |
||
17 | 17 | */ |
18 | 18 | class TableAnalysis extends EE_Base |
19 | 19 | { |
20 | - /** |
|
21 | - * The maximum number of characters that can be indexed on a column using utf8mb4 collation, |
|
22 | - * see https://events.codebasehq.com/redirect?https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/ |
|
23 | - */ |
|
24 | - public const INDEX_COLUMN_SIZE = 191; |
|
20 | + /** |
|
21 | + * The maximum number of characters that can be indexed on a column using utf8mb4 collation, |
|
22 | + * see https://events.codebasehq.com/redirect?https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/ |
|
23 | + */ |
|
24 | + public const INDEX_COLUMN_SIZE = 191; |
|
25 | 25 | |
26 | 26 | |
27 | - /** |
|
28 | - * Returns the table name which will definitely have the wpdb prefix on the front, |
|
29 | - * except if it currently has the wpdb->base_prefix on the front, in which case |
|
30 | - * it will have the wpdb->base_prefix on it |
|
31 | - * |
|
32 | - * @param string $table_name |
|
33 | - * @return string $tableName, having ensured it has the wpdb prefix on the front |
|
34 | - * @global wpdb $wpdb |
|
35 | - */ |
|
36 | - public function ensureTableNameHasPrefix(string $table_name): string |
|
37 | - { |
|
38 | - global $wpdb; |
|
39 | - return strpos($table_name, $wpdb->base_prefix) === 0 ? $table_name : $wpdb->prefix . $table_name; |
|
40 | - } |
|
27 | + /** |
|
28 | + * Returns the table name which will definitely have the wpdb prefix on the front, |
|
29 | + * except if it currently has the wpdb->base_prefix on the front, in which case |
|
30 | + * it will have the wpdb->base_prefix on it |
|
31 | + * |
|
32 | + * @param string $table_name |
|
33 | + * @return string $tableName, having ensured it has the wpdb prefix on the front |
|
34 | + * @global wpdb $wpdb |
|
35 | + */ |
|
36 | + public function ensureTableNameHasPrefix(string $table_name): string |
|
37 | + { |
|
38 | + global $wpdb; |
|
39 | + return strpos($table_name, $wpdb->base_prefix) === 0 ? $table_name : $wpdb->prefix . $table_name; |
|
40 | + } |
|
41 | 41 | |
42 | 42 | |
43 | - /** |
|
44 | - * Indicates whether or not the table has any entries. $table_name can |
|
45 | - * optionally start with $wpdb->prefix or not |
|
46 | - * |
|
47 | - * @param string $table_name |
|
48 | - * @return bool |
|
49 | - * @global wpdb $wpdb |
|
50 | - */ |
|
51 | - public function tableIsEmpty(string $table_name): bool |
|
52 | - { |
|
53 | - global $wpdb; |
|
54 | - $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
55 | - if ($this->tableExists($table_name)) { |
|
56 | - $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name"); |
|
57 | - return absint($count) === 0; |
|
58 | - } |
|
59 | - return false; |
|
60 | - } |
|
43 | + /** |
|
44 | + * Indicates whether or not the table has any entries. $table_name can |
|
45 | + * optionally start with $wpdb->prefix or not |
|
46 | + * |
|
47 | + * @param string $table_name |
|
48 | + * @return bool |
|
49 | + * @global wpdb $wpdb |
|
50 | + */ |
|
51 | + public function tableIsEmpty(string $table_name): bool |
|
52 | + { |
|
53 | + global $wpdb; |
|
54 | + $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
55 | + if ($this->tableExists($table_name)) { |
|
56 | + $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name"); |
|
57 | + return absint($count) === 0; |
|
58 | + } |
|
59 | + return false; |
|
60 | + } |
|
61 | 61 | |
62 | 62 | |
63 | - /** |
|
64 | - * Indicates whether or not the table exists. $table_name can optionally |
|
65 | - * have the $wpdb->prefix on the beginning, or not. |
|
66 | - * |
|
67 | - * @param string $table_name |
|
68 | - * @return bool |
|
69 | - * @global wpdb $wpdb |
|
70 | - * @global array $EZSQL_ERROR |
|
71 | - */ |
|
72 | - public function tableExists(string $table_name): bool |
|
73 | - { |
|
74 | - global $wpdb, $EZSQL_ERROR; |
|
75 | - $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
76 | - // ignore if this causes an sql error |
|
77 | - $old_error = $wpdb->last_error; |
|
78 | - $old_suppress_errors = $wpdb->suppress_errors(); |
|
79 | - $old_show_errors_value = $wpdb->show_errors(false); |
|
80 | - $ezsql_error_cache = $EZSQL_ERROR; |
|
81 | - $wpdb->get_results("SELECT * from $table_name LIMIT 1"); |
|
82 | - $wpdb->show_errors($old_show_errors_value); |
|
83 | - $wpdb->suppress_errors($old_suppress_errors); |
|
84 | - $new_error = $wpdb->last_error; |
|
85 | - $wpdb->last_error = $old_error; |
|
86 | - $EZSQL_ERROR = $ezsql_error_cache; |
|
87 | - // if there was a table doesn't exist error |
|
88 | - if (! empty($new_error)) { |
|
89 | - if ( |
|
90 | - in_array( |
|
91 | - EEH_Activation::last_wpdb_error_code(), |
|
92 | - [ |
|
93 | - 1051, // bad table |
|
94 | - 1109, // unknown table |
|
95 | - 117, // no such table |
|
96 | - ] |
|
97 | - ) |
|
98 | - || preg_match( |
|
99 | - '~^Table .* doesn\'t exist~', |
|
100 | - $new_error |
|
101 | - ) // in case not using mysql and error codes aren't reliable, just check for this error string |
|
102 | - ) { |
|
103 | - return false; |
|
104 | - } else { |
|
105 | - // log this because that's weird. Just use the normal PHP error log |
|
106 | - error_log( |
|
107 | - sprintf( |
|
108 | - esc_html__( |
|
109 | - 'Event Espresso error detected when checking if table existed: %1$s (it wasn\'t just that the table didn\'t exist either)', |
|
110 | - 'event_espresso' |
|
111 | - ), |
|
112 | - $new_error |
|
113 | - ) |
|
114 | - ); |
|
115 | - } |
|
116 | - } |
|
117 | - return true; |
|
118 | - } |
|
63 | + /** |
|
64 | + * Indicates whether or not the table exists. $table_name can optionally |
|
65 | + * have the $wpdb->prefix on the beginning, or not. |
|
66 | + * |
|
67 | + * @param string $table_name |
|
68 | + * @return bool |
|
69 | + * @global wpdb $wpdb |
|
70 | + * @global array $EZSQL_ERROR |
|
71 | + */ |
|
72 | + public function tableExists(string $table_name): bool |
|
73 | + { |
|
74 | + global $wpdb, $EZSQL_ERROR; |
|
75 | + $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
76 | + // ignore if this causes an sql error |
|
77 | + $old_error = $wpdb->last_error; |
|
78 | + $old_suppress_errors = $wpdb->suppress_errors(); |
|
79 | + $old_show_errors_value = $wpdb->show_errors(false); |
|
80 | + $ezsql_error_cache = $EZSQL_ERROR; |
|
81 | + $wpdb->get_results("SELECT * from $table_name LIMIT 1"); |
|
82 | + $wpdb->show_errors($old_show_errors_value); |
|
83 | + $wpdb->suppress_errors($old_suppress_errors); |
|
84 | + $new_error = $wpdb->last_error; |
|
85 | + $wpdb->last_error = $old_error; |
|
86 | + $EZSQL_ERROR = $ezsql_error_cache; |
|
87 | + // if there was a table doesn't exist error |
|
88 | + if (! empty($new_error)) { |
|
89 | + if ( |
|
90 | + in_array( |
|
91 | + EEH_Activation::last_wpdb_error_code(), |
|
92 | + [ |
|
93 | + 1051, // bad table |
|
94 | + 1109, // unknown table |
|
95 | + 117, // no such table |
|
96 | + ] |
|
97 | + ) |
|
98 | + || preg_match( |
|
99 | + '~^Table .* doesn\'t exist~', |
|
100 | + $new_error |
|
101 | + ) // in case not using mysql and error codes aren't reliable, just check for this error string |
|
102 | + ) { |
|
103 | + return false; |
|
104 | + } else { |
|
105 | + // log this because that's weird. Just use the normal PHP error log |
|
106 | + error_log( |
|
107 | + sprintf( |
|
108 | + esc_html__( |
|
109 | + 'Event Espresso error detected when checking if table existed: %1$s (it wasn\'t just that the table didn\'t exist either)', |
|
110 | + 'event_espresso' |
|
111 | + ), |
|
112 | + $new_error |
|
113 | + ) |
|
114 | + ); |
|
115 | + } |
|
116 | + } |
|
117 | + return true; |
|
118 | + } |
|
119 | 119 | |
120 | 120 | |
121 | - /** |
|
122 | - * @param string $table_name |
|
123 | - * @param string $index_name |
|
124 | - * @return array of columns used on that index, Each entry is an object with the following properties { |
|
125 | - * } |
|
126 | - */ |
|
127 | - public function showIndexes(string $table_name, string $index_name): array |
|
128 | - { |
|
129 | - global $wpdb; |
|
130 | - $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
131 | - $index_exists_query = "SHOW INDEX FROM $table_name WHERE Key_name = '$index_name'"; |
|
132 | - return $wpdb->get_results($index_exists_query); |
|
133 | - } |
|
121 | + /** |
|
122 | + * @param string $table_name |
|
123 | + * @param string $index_name |
|
124 | + * @return array of columns used on that index, Each entry is an object with the following properties { |
|
125 | + * } |
|
126 | + */ |
|
127 | + public function showIndexes(string $table_name, string $index_name): array |
|
128 | + { |
|
129 | + global $wpdb; |
|
130 | + $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
131 | + $index_exists_query = "SHOW INDEX FROM $table_name WHERE Key_name = '$index_name'"; |
|
132 | + return $wpdb->get_results($index_exists_query); |
|
133 | + } |
|
134 | 134 | } |
@@ -44,7 +44,7 @@ |
||
44 | 44 | ) |
45 | 45 | ); |
46 | 46 | // if the incoming country has a set format, use that, else use the default |
47 | - $formatted_address = $address_formats[ $CNT_ISO ] ?? $address_formats['ZZZ']; |
|
47 | + $formatted_address = $address_formats[$CNT_ISO] ?? $address_formats['ZZZ']; |
|
48 | 48 | return $this->parse_formatted_address( |
49 | 49 | $address, |
50 | 50 | $address2, |
@@ -13,45 +13,45 @@ |
||
13 | 13 | */ |
14 | 14 | class InlineAddressFormatter extends AddressFormatter implements AddressFormatterInterface |
15 | 15 | { |
16 | - /** |
|
17 | - * @param string $address |
|
18 | - * @param string $address2 |
|
19 | - * @param string $city |
|
20 | - * @param string $state |
|
21 | - * @param string $zip |
|
22 | - * @param string $country |
|
23 | - * @param string $CNT_ISO |
|
24 | - * @return string|null |
|
25 | - */ |
|
26 | - public function format( |
|
27 | - string $address, |
|
28 | - string $address2, |
|
29 | - string $city, |
|
30 | - string $state, |
|
31 | - string $zip, |
|
32 | - string $country, |
|
33 | - string $CNT_ISO |
|
34 | - ): ?string { |
|
35 | - $address_formats = apply_filters( |
|
36 | - 'FHEE__EE_Inline_Address_Formatter__address_formats', |
|
37 | - array( |
|
38 | - 'CA' => "{address}%{address2}%{city}%{state}%{country}%{zip}", |
|
39 | - 'GB' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
40 | - 'US' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
41 | - 'ZZZ' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
42 | - ) |
|
43 | - ); |
|
44 | - // if the incoming country has a set format, use that, else use the default |
|
45 | - $formatted_address = $address_formats[ $CNT_ISO ] ?? $address_formats['ZZZ']; |
|
46 | - return $this->parse_formatted_address( |
|
47 | - $address, |
|
48 | - $address2, |
|
49 | - $city, |
|
50 | - $state, |
|
51 | - $zip, |
|
52 | - $country, |
|
53 | - $formatted_address, |
|
54 | - ', ' |
|
55 | - ); |
|
56 | - } |
|
16 | + /** |
|
17 | + * @param string $address |
|
18 | + * @param string $address2 |
|
19 | + * @param string $city |
|
20 | + * @param string $state |
|
21 | + * @param string $zip |
|
22 | + * @param string $country |
|
23 | + * @param string $CNT_ISO |
|
24 | + * @return string|null |
|
25 | + */ |
|
26 | + public function format( |
|
27 | + string $address, |
|
28 | + string $address2, |
|
29 | + string $city, |
|
30 | + string $state, |
|
31 | + string $zip, |
|
32 | + string $country, |
|
33 | + string $CNT_ISO |
|
34 | + ): ?string { |
|
35 | + $address_formats = apply_filters( |
|
36 | + 'FHEE__EE_Inline_Address_Formatter__address_formats', |
|
37 | + array( |
|
38 | + 'CA' => "{address}%{address2}%{city}%{state}%{country}%{zip}", |
|
39 | + 'GB' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
40 | + 'US' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
41 | + 'ZZZ' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
42 | + ) |
|
43 | + ); |
|
44 | + // if the incoming country has a set format, use that, else use the default |
|
45 | + $formatted_address = $address_formats[ $CNT_ISO ] ?? $address_formats['ZZZ']; |
|
46 | + return $this->parse_formatted_address( |
|
47 | + $address, |
|
48 | + $address2, |
|
49 | + $city, |
|
50 | + $state, |
|
51 | + $zip, |
|
52 | + $country, |
|
53 | + $formatted_address, |
|
54 | + ', ' |
|
55 | + ); |
|
56 | + } |
|
57 | 57 | } |
@@ -44,7 +44,7 @@ |
||
44 | 44 | ) |
45 | 45 | ); |
46 | 46 | // if the incoming country has a set format, use that, else use the default |
47 | - $formatted_address = $address_formats[ $CNT_ISO ] ?? $address_formats['ZZ']; |
|
47 | + $formatted_address = $address_formats[$CNT_ISO] ?? $address_formats['ZZ']; |
|
48 | 48 | return $this->parse_formatted_address( |
49 | 49 | $address, |
50 | 50 | $address2, |
@@ -13,45 +13,45 @@ |
||
13 | 13 | */ |
14 | 14 | class MultiLineAddressFormatter extends AddressFormatter implements AddressFormatterInterface |
15 | 15 | { |
16 | - /** |
|
17 | - * @param string $address |
|
18 | - * @param string $address2 |
|
19 | - * @param string $city |
|
20 | - * @param string $state |
|
21 | - * @param string $zip |
|
22 | - * @param string $country |
|
23 | - * @param string $CNT_ISO |
|
24 | - * @return string |
|
25 | - */ |
|
26 | - public function format( |
|
27 | - string $address, |
|
28 | - string $address2, |
|
29 | - string $city, |
|
30 | - string $state, |
|
31 | - string $zip, |
|
32 | - string $country, |
|
33 | - string $CNT_ISO |
|
34 | - ): ?string { |
|
35 | - $address_formats = apply_filters( |
|
36 | - 'FHEE__EE_MultiLine_Address_Formatter__address_formats', |
|
37 | - array( |
|
38 | - 'CA' => "{address}%{address2}%{city}%{state}%{country}%{zip}", |
|
39 | - 'GB' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
40 | - 'US' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
41 | - 'ZZ' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
42 | - ) |
|
43 | - ); |
|
44 | - // if the incoming country has a set format, use that, else use the default |
|
45 | - $formatted_address = $address_formats[ $CNT_ISO ] ?? $address_formats['ZZ']; |
|
46 | - return $this->parse_formatted_address( |
|
47 | - $address, |
|
48 | - $address2, |
|
49 | - $city, |
|
50 | - $state, |
|
51 | - $zip, |
|
52 | - $country, |
|
53 | - $formatted_address, |
|
54 | - '<br />' |
|
55 | - ); |
|
56 | - } |
|
16 | + /** |
|
17 | + * @param string $address |
|
18 | + * @param string $address2 |
|
19 | + * @param string $city |
|
20 | + * @param string $state |
|
21 | + * @param string $zip |
|
22 | + * @param string $country |
|
23 | + * @param string $CNT_ISO |
|
24 | + * @return string |
|
25 | + */ |
|
26 | + public function format( |
|
27 | + string $address, |
|
28 | + string $address2, |
|
29 | + string $city, |
|
30 | + string $state, |
|
31 | + string $zip, |
|
32 | + string $country, |
|
33 | + string $CNT_ISO |
|
34 | + ): ?string { |
|
35 | + $address_formats = apply_filters( |
|
36 | + 'FHEE__EE_MultiLine_Address_Formatter__address_formats', |
|
37 | + array( |
|
38 | + 'CA' => "{address}%{address2}%{city}%{state}%{country}%{zip}", |
|
39 | + 'GB' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
40 | + 'US' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
41 | + 'ZZ' => "{address}%{address2}%{city}%{state}%{zip}%{country}", |
|
42 | + ) |
|
43 | + ); |
|
44 | + // if the incoming country has a set format, use that, else use the default |
|
45 | + $formatted_address = $address_formats[ $CNT_ISO ] ?? $address_formats['ZZ']; |
|
46 | + return $this->parse_formatted_address( |
|
47 | + $address, |
|
48 | + $address2, |
|
49 | + $city, |
|
50 | + $state, |
|
51 | + $zip, |
|
52 | + $country, |
|
53 | + $formatted_address, |
|
54 | + '<br />' |
|
55 | + ); |
|
56 | + } |
|
57 | 57 | } |
@@ -12,38 +12,38 @@ |
||
12 | 12 | */ |
13 | 13 | class AddressFormatter |
14 | 14 | { |
15 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
16 | - /** |
|
17 | - * @param string $address |
|
18 | - * @param string $address2 |
|
19 | - * @param string $city |
|
20 | - * @param string $state |
|
21 | - * @param string $zip |
|
22 | - * @param string $country |
|
23 | - * @param string $formatted_address |
|
24 | - * @param string $sub |
|
25 | - * @return string |
|
26 | - */ |
|
27 | - protected function parse_formatted_address( |
|
28 | - string $address, |
|
29 | - string $address2, |
|
30 | - string $city, |
|
31 | - string $state, |
|
32 | - string $zip, |
|
33 | - string $country, |
|
34 | - string $formatted_address, |
|
35 | - string $sub |
|
36 | - ): string { |
|
37 | - // swap address part placeholders for the real text |
|
38 | - $formatted_address = str_replace( |
|
39 | - // find |
|
40 | - array('{address}', '{address2}', '{city}', '{state}', '{zip}', '{country}'), |
|
41 | - // replace |
|
42 | - array($address, $address2, $city, $state, $zip, $country), |
|
43 | - // string |
|
44 | - $formatted_address |
|
45 | - ); |
|
46 | - // remove placeholder from start and end, reduce repeating placeholders to singles, then replace with HTML line breaks |
|
47 | - return preg_replace('/%+/', $sub, trim($formatted_address, '%')); |
|
48 | - } |
|
15 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
16 | + /** |
|
17 | + * @param string $address |
|
18 | + * @param string $address2 |
|
19 | + * @param string $city |
|
20 | + * @param string $state |
|
21 | + * @param string $zip |
|
22 | + * @param string $country |
|
23 | + * @param string $formatted_address |
|
24 | + * @param string $sub |
|
25 | + * @return string |
|
26 | + */ |
|
27 | + protected function parse_formatted_address( |
|
28 | + string $address, |
|
29 | + string $address2, |
|
30 | + string $city, |
|
31 | + string $state, |
|
32 | + string $zip, |
|
33 | + string $country, |
|
34 | + string $formatted_address, |
|
35 | + string $sub |
|
36 | + ): string { |
|
37 | + // swap address part placeholders for the real text |
|
38 | + $formatted_address = str_replace( |
|
39 | + // find |
|
40 | + array('{address}', '{address2}', '{city}', '{state}', '{zip}', '{country}'), |
|
41 | + // replace |
|
42 | + array($address, $address2, $city, $state, $zip, $country), |
|
43 | + // string |
|
44 | + $formatted_address |
|
45 | + ); |
|
46 | + // remove placeholder from start and end, reduce repeating placeholders to singles, then replace with HTML line breaks |
|
47 | + return preg_replace('/%+/', $sub, trim($formatted_address, '%')); |
|
48 | + } |
|
49 | 49 | } |
@@ -6,8 +6,8 @@ |
||
6 | 6 | $baseDir = dirname($vendorDir); |
7 | 7 | |
8 | 8 | return array( |
9 | - '6124b4c8570aa390c21fafd04a26c69f' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/deep_copy.php', |
|
10 | - 'a4a119a56e50fbb293281d9a48007e0e' => $vendorDir . '/symfony/polyfill-php80/bootstrap.php', |
|
11 | - 'ec07570ca5a812141189b1fa81503674' => $vendorDir . '/phpunit/phpunit/src/Framework/Assert/Functions.php', |
|
12 | - '7d3b315c4f303f2fc14aca642a738e50' => $vendorDir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php', |
|
9 | + '6124b4c8570aa390c21fafd04a26c69f' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/deep_copy.php', |
|
10 | + 'a4a119a56e50fbb293281d9a48007e0e' => $vendorDir . '/symfony/polyfill-php80/bootstrap.php', |
|
11 | + 'ec07570ca5a812141189b1fa81503674' => $vendorDir . '/phpunit/phpunit/src/Framework/Assert/Functions.php', |
|
12 | + '7d3b315c4f303f2fc14aca642a738e50' => $vendorDir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php', |
|
13 | 13 | ); |
@@ -6,8 +6,8 @@ |
||
6 | 6 | $baseDir = dirname($vendorDir); |
7 | 7 | |
8 | 8 | return array( |
9 | - '6124b4c8570aa390c21fafd04a26c69f' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/deep_copy.php', |
|
10 | - 'a4a119a56e50fbb293281d9a48007e0e' => $vendorDir . '/symfony/polyfill-php80/bootstrap.php', |
|
11 | - 'ec07570ca5a812141189b1fa81503674' => $vendorDir . '/phpunit/phpunit/src/Framework/Assert/Functions.php', |
|
12 | - '7d3b315c4f303f2fc14aca642a738e50' => $vendorDir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php', |
|
9 | + '6124b4c8570aa390c21fafd04a26c69f' => $vendorDir.'/myclabs/deep-copy/src/DeepCopy/deep_copy.php', |
|
10 | + 'a4a119a56e50fbb293281d9a48007e0e' => $vendorDir.'/symfony/polyfill-php80/bootstrap.php', |
|
11 | + 'ec07570ca5a812141189b1fa81503674' => $vendorDir.'/phpunit/phpunit/src/Framework/Assert/Functions.php', |
|
12 | + '7d3b315c4f303f2fc14aca642a738e50' => $vendorDir.'/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php', |
|
13 | 13 | ); |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | */ |
118 | 118 | public function getShowHideLinks(): string |
119 | 119 | { |
120 | - if (! $this->showTicketDetails()) { |
|
120 | + if ( ! $this->showTicketDetails()) { |
|
121 | 121 | return ''; |
122 | 122 | } |
123 | 123 | return EEH_HTML::link( |
@@ -132,8 +132,8 @@ discard block |
||
132 | 132 | "display-{$this->cssId()}", |
133 | 133 | 'display-tckt-slctr-tkt-details display-the-hidden lt-grey-text smaller-text hide-if-no-js', |
134 | 134 | '', |
135 | - 'rel="' . $this->cssId() . '"' |
|
136 | - ) . EEH_HTML::link( |
|
135 | + 'rel="'.$this->cssId().'"' |
|
136 | + ).EEH_HTML::link( |
|
137 | 137 | '', |
138 | 138 | sprintf(esc_html__('hide%1$sdetails%1$s-', 'event_espresso'), ' '), |
139 | 139 | esc_attr( |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | "hide-{$this->cssId()}", |
146 | 146 | 'hide-tckt-slctr-tkt-details hide-the-displayed lt-grey-text smaller-text hide-if-no-js', |
147 | 147 | 'display:none;', |
148 | - 'rel="' . $this->cssId() . '"' |
|
148 | + 'rel="'.$this->cssId().'"' |
|
149 | 149 | ); |
150 | 150 | } |
151 | 151 | |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | 'FHEE__ticket_selector_chart_template__display_ticket_price_details', |
192 | 192 | true |
193 | 193 | ); |
194 | - $template_args['price_breakdown_heading'] = apply_filters( |
|
194 | + $template_args['price_breakdown_heading'] = apply_filters( |
|
195 | 195 | 'FHEE__ticket_selector_chart_template__ticket_details_price_breakdown_heading', |
196 | 196 | esc_html__('Price', 'event_espresso') |
197 | 197 | ); |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | return EEH_Template::locate_template( |
203 | 203 | apply_filters( |
204 | 204 | 'FHEE__EventEspresso_modules_ticket_selector_TicketDetails__display__template_path', |
205 | - TICKET_SELECTOR_TEMPLATES_PATH . 'ticket_details.template.php', |
|
205 | + TICKET_SELECTOR_TEMPLATES_PATH.'ticket_details.template.php', |
|
206 | 206 | $this->ticket |
207 | 207 | ), |
208 | 208 | $template_args |
@@ -19,193 +19,193 @@ |
||
19 | 19 | */ |
20 | 20 | class TicketDetails |
21 | 21 | { |
22 | - /** |
|
23 | - * @var EE_Ticket |
|
24 | - */ |
|
25 | - protected $ticket; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var EE_Ticket_Selector_Config |
|
29 | - */ |
|
30 | - protected $template_settings; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var string $date_format |
|
34 | - */ |
|
35 | - protected $date_format; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var string $time_format |
|
39 | - */ |
|
40 | - protected $time_format; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var bool $event_is_expired |
|
44 | - */ |
|
45 | - protected $event_is_expired; |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * TicketDetails constructor. |
|
50 | - * |
|
51 | - * @param EE_Ticket $ticket |
|
52 | - * @param EE_Ticket_Selector_Config $template_settings |
|
53 | - * @param array $template_args |
|
54 | - */ |
|
55 | - public function __construct( |
|
56 | - EE_Ticket $ticket, |
|
57 | - EE_Ticket_Selector_Config $template_settings, |
|
58 | - array $template_args |
|
59 | - ) { |
|
60 | - $this->ticket = $ticket; |
|
61 | - $this->template_settings = $template_settings; |
|
62 | - $this->date_format = $template_args['date_format']; |
|
63 | - $this->time_format = $template_args['time_format']; |
|
64 | - $this->event_is_expired = $template_args['event_is_expired']; |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * @return EE_Ticket |
|
70 | - */ |
|
71 | - public function getTicket(): EE_Ticket |
|
72 | - { |
|
73 | - return $this->ticket; |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * @return bool |
|
79 | - */ |
|
80 | - public function showTicketDetails(): bool |
|
81 | - { |
|
82 | - return $this->template_settings->show_ticket_details; |
|
83 | - } |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * @return EE_Ticket_Selector_Config |
|
88 | - */ |
|
89 | - public function getTemplateSettings(): EE_Ticket_Selector_Config |
|
90 | - { |
|
91 | - return $this->template_settings; |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * @return string |
|
97 | - */ |
|
98 | - public function getDateFormat(): string |
|
99 | - { |
|
100 | - return $this->date_format; |
|
101 | - } |
|
102 | - |
|
103 | - |
|
104 | - /** |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public function getTimeFormat(): string |
|
108 | - { |
|
109 | - return $this->time_format; |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * @return string |
|
115 | - * @throws EE_Error |
|
116 | - * @throws ReflectionException |
|
117 | - */ |
|
118 | - public function getShowHideLinks(): string |
|
119 | - { |
|
120 | - if (! $this->showTicketDetails()) { |
|
121 | - return ''; |
|
122 | - } |
|
123 | - return EEH_HTML::link( |
|
124 | - '', |
|
125 | - sprintf(esc_html__('show%1$sdetails%1$s+', 'event_espresso'), ' '), |
|
126 | - esc_attr( |
|
127 | - apply_filters( |
|
128 | - 'FHEE__ticket_selector_chart_template__show_ticket_details_link_title', |
|
129 | - esc_html__('click to show additional ticket details', 'event_espresso') |
|
130 | - ) |
|
131 | - ), |
|
132 | - "display-{$this->cssId()}", |
|
133 | - 'display-tckt-slctr-tkt-details display-the-hidden lt-grey-text smaller-text hide-if-no-js', |
|
134 | - '', |
|
135 | - 'rel="' . $this->cssId() . '"' |
|
136 | - ) . EEH_HTML::link( |
|
137 | - '', |
|
138 | - sprintf(esc_html__('hide%1$sdetails%1$s-', 'event_espresso'), ' '), |
|
139 | - esc_attr( |
|
140 | - apply_filters( |
|
141 | - 'FHEE__ticket_selector_chart_template__hide_ticket_details_link_title', |
|
142 | - esc_html__('click to hide additional ticket details', 'event_espresso') |
|
143 | - ) |
|
144 | - ), |
|
145 | - "hide-{$this->cssId()}", |
|
146 | - 'hide-tckt-slctr-tkt-details hide-the-displayed lt-grey-text smaller-text hide-if-no-js', |
|
147 | - 'display:none;', |
|
148 | - 'rel="' . $this->cssId() . '"' |
|
149 | - ); |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * @return string |
|
155 | - * @throws EE_Error |
|
156 | - * @throws ReflectionException |
|
157 | - */ |
|
158 | - public function cssId(): string |
|
159 | - { |
|
160 | - return apply_filters( |
|
161 | - 'FHEE__ticket_selector_chart_template__ticket_details_css_id', |
|
162 | - "tckt-slctr-tkt-details-{$this->ticket->get_event_ID()}-{$this->ticket->ID()}" |
|
163 | - ); |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * @param float|string $ticket_price |
|
169 | - * @param int|string $remaining |
|
170 | - * @param int|string $cols |
|
171 | - * @return string |
|
172 | - * @throws EE_Error |
|
173 | - * @throws ReflectionException |
|
174 | - */ |
|
175 | - public function display( |
|
176 | - $ticket_price = 0.00, |
|
177 | - $remaining = EE_INF, |
|
178 | - $cols = 2 |
|
179 | - ): string { |
|
180 | - $template_args = []; |
|
181 | - $template_args['ticket'] = $this->ticket; |
|
182 | - $template_args['ticket_price'] = $ticket_price; |
|
183 | - $template_args['remaining'] = $remaining; |
|
184 | - $template_args['cols'] = $cols; |
|
185 | - $template_args['show_ticket_details'] = $this->template_settings->show_ticket_details; |
|
186 | - $template_args['show_ticket_sale_columns'] = $this->template_settings->show_ticket_sale_columns; |
|
187 | - $template_args['ticket_details_row_class'] = espresso_get_object_css_class($this->ticket, '', 'details'); |
|
188 | - $template_args['ticket_details_css_id'] = $this->cssId(); |
|
189 | - $template_args['display_ticket_price'] = $ticket_price !== 0 |
|
190 | - && apply_filters( |
|
191 | - 'FHEE__ticket_selector_chart_template__display_ticket_price_details', |
|
192 | - true |
|
193 | - ); |
|
194 | - $template_args['price_breakdown_heading'] = apply_filters( |
|
195 | - 'FHEE__ticket_selector_chart_template__ticket_details_price_breakdown_heading', |
|
196 | - esc_html__('Price', 'event_espresso') |
|
197 | - ); |
|
198 | - $template_args['date_format'] = $this->date_format; |
|
199 | - $template_args['time_format'] = $this->time_format; |
|
200 | - $template_args['event_is_expired'] = $this->event_is_expired; |
|
201 | - |
|
202 | - return EEH_Template::locate_template( |
|
203 | - apply_filters( |
|
204 | - 'FHEE__EventEspresso_modules_ticket_selector_TicketDetails__display__template_path', |
|
205 | - TICKET_SELECTOR_TEMPLATES_PATH . 'ticket_details.template.php', |
|
206 | - $this->ticket |
|
207 | - ), |
|
208 | - $template_args |
|
209 | - ); |
|
210 | - } |
|
22 | + /** |
|
23 | + * @var EE_Ticket |
|
24 | + */ |
|
25 | + protected $ticket; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var EE_Ticket_Selector_Config |
|
29 | + */ |
|
30 | + protected $template_settings; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var string $date_format |
|
34 | + */ |
|
35 | + protected $date_format; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var string $time_format |
|
39 | + */ |
|
40 | + protected $time_format; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var bool $event_is_expired |
|
44 | + */ |
|
45 | + protected $event_is_expired; |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * TicketDetails constructor. |
|
50 | + * |
|
51 | + * @param EE_Ticket $ticket |
|
52 | + * @param EE_Ticket_Selector_Config $template_settings |
|
53 | + * @param array $template_args |
|
54 | + */ |
|
55 | + public function __construct( |
|
56 | + EE_Ticket $ticket, |
|
57 | + EE_Ticket_Selector_Config $template_settings, |
|
58 | + array $template_args |
|
59 | + ) { |
|
60 | + $this->ticket = $ticket; |
|
61 | + $this->template_settings = $template_settings; |
|
62 | + $this->date_format = $template_args['date_format']; |
|
63 | + $this->time_format = $template_args['time_format']; |
|
64 | + $this->event_is_expired = $template_args['event_is_expired']; |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * @return EE_Ticket |
|
70 | + */ |
|
71 | + public function getTicket(): EE_Ticket |
|
72 | + { |
|
73 | + return $this->ticket; |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * @return bool |
|
79 | + */ |
|
80 | + public function showTicketDetails(): bool |
|
81 | + { |
|
82 | + return $this->template_settings->show_ticket_details; |
|
83 | + } |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * @return EE_Ticket_Selector_Config |
|
88 | + */ |
|
89 | + public function getTemplateSettings(): EE_Ticket_Selector_Config |
|
90 | + { |
|
91 | + return $this->template_settings; |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * @return string |
|
97 | + */ |
|
98 | + public function getDateFormat(): string |
|
99 | + { |
|
100 | + return $this->date_format; |
|
101 | + } |
|
102 | + |
|
103 | + |
|
104 | + /** |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public function getTimeFormat(): string |
|
108 | + { |
|
109 | + return $this->time_format; |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * @return string |
|
115 | + * @throws EE_Error |
|
116 | + * @throws ReflectionException |
|
117 | + */ |
|
118 | + public function getShowHideLinks(): string |
|
119 | + { |
|
120 | + if (! $this->showTicketDetails()) { |
|
121 | + return ''; |
|
122 | + } |
|
123 | + return EEH_HTML::link( |
|
124 | + '', |
|
125 | + sprintf(esc_html__('show%1$sdetails%1$s+', 'event_espresso'), ' '), |
|
126 | + esc_attr( |
|
127 | + apply_filters( |
|
128 | + 'FHEE__ticket_selector_chart_template__show_ticket_details_link_title', |
|
129 | + esc_html__('click to show additional ticket details', 'event_espresso') |
|
130 | + ) |
|
131 | + ), |
|
132 | + "display-{$this->cssId()}", |
|
133 | + 'display-tckt-slctr-tkt-details display-the-hidden lt-grey-text smaller-text hide-if-no-js', |
|
134 | + '', |
|
135 | + 'rel="' . $this->cssId() . '"' |
|
136 | + ) . EEH_HTML::link( |
|
137 | + '', |
|
138 | + sprintf(esc_html__('hide%1$sdetails%1$s-', 'event_espresso'), ' '), |
|
139 | + esc_attr( |
|
140 | + apply_filters( |
|
141 | + 'FHEE__ticket_selector_chart_template__hide_ticket_details_link_title', |
|
142 | + esc_html__('click to hide additional ticket details', 'event_espresso') |
|
143 | + ) |
|
144 | + ), |
|
145 | + "hide-{$this->cssId()}", |
|
146 | + 'hide-tckt-slctr-tkt-details hide-the-displayed lt-grey-text smaller-text hide-if-no-js', |
|
147 | + 'display:none;', |
|
148 | + 'rel="' . $this->cssId() . '"' |
|
149 | + ); |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * @return string |
|
155 | + * @throws EE_Error |
|
156 | + * @throws ReflectionException |
|
157 | + */ |
|
158 | + public function cssId(): string |
|
159 | + { |
|
160 | + return apply_filters( |
|
161 | + 'FHEE__ticket_selector_chart_template__ticket_details_css_id', |
|
162 | + "tckt-slctr-tkt-details-{$this->ticket->get_event_ID()}-{$this->ticket->ID()}" |
|
163 | + ); |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * @param float|string $ticket_price |
|
169 | + * @param int|string $remaining |
|
170 | + * @param int|string $cols |
|
171 | + * @return string |
|
172 | + * @throws EE_Error |
|
173 | + * @throws ReflectionException |
|
174 | + */ |
|
175 | + public function display( |
|
176 | + $ticket_price = 0.00, |
|
177 | + $remaining = EE_INF, |
|
178 | + $cols = 2 |
|
179 | + ): string { |
|
180 | + $template_args = []; |
|
181 | + $template_args['ticket'] = $this->ticket; |
|
182 | + $template_args['ticket_price'] = $ticket_price; |
|
183 | + $template_args['remaining'] = $remaining; |
|
184 | + $template_args['cols'] = $cols; |
|
185 | + $template_args['show_ticket_details'] = $this->template_settings->show_ticket_details; |
|
186 | + $template_args['show_ticket_sale_columns'] = $this->template_settings->show_ticket_sale_columns; |
|
187 | + $template_args['ticket_details_row_class'] = espresso_get_object_css_class($this->ticket, '', 'details'); |
|
188 | + $template_args['ticket_details_css_id'] = $this->cssId(); |
|
189 | + $template_args['display_ticket_price'] = $ticket_price !== 0 |
|
190 | + && apply_filters( |
|
191 | + 'FHEE__ticket_selector_chart_template__display_ticket_price_details', |
|
192 | + true |
|
193 | + ); |
|
194 | + $template_args['price_breakdown_heading'] = apply_filters( |
|
195 | + 'FHEE__ticket_selector_chart_template__ticket_details_price_breakdown_heading', |
|
196 | + esc_html__('Price', 'event_espresso') |
|
197 | + ); |
|
198 | + $template_args['date_format'] = $this->date_format; |
|
199 | + $template_args['time_format'] = $this->time_format; |
|
200 | + $template_args['event_is_expired'] = $this->event_is_expired; |
|
201 | + |
|
202 | + return EEH_Template::locate_template( |
|
203 | + apply_filters( |
|
204 | + 'FHEE__EventEspresso_modules_ticket_selector_TicketDetails__display__template_path', |
|
205 | + TICKET_SELECTOR_TEMPLATES_PATH . 'ticket_details.template.php', |
|
206 | + $this->ticket |
|
207 | + ), |
|
208 | + $template_args |
|
209 | + ); |
|
210 | + } |
|
211 | 211 | } |