| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Filesystem; |
| 13 |
|
| 14 |
use Symfony\Component\Filesystem\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\Filesystem\Exception\RuntimeException; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
> |
| 28 |
|
| 29 |
final class Path |
| 30 |
{ |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private const CLEANUP_THRESHOLD = 1250; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private const CLEANUP_SIZE = 1000; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private static array $buffer = []; |
| 47 |
|
| 48 |
private static int $bufferSize = 0; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static function canonicalize(string $path): string |
| 65 |
{ |
| 66 |
if ('' === $path) { |
| 67 |
return ''; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
if (isset(self::$buffer[$path])) { |
| 74 |
return self::$buffer[$path]; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
if ('~' === $path[0]) { |
| 79 |
$path = self::getHomeDirectory().substr($path, 1); |
| 80 |
} |
| 81 |
|
| 82 |
$path = self::normalize($path); |
| 83 |
|
| 84 |
[$root, $pathWithoutRoot] = self::split($path); |
| 85 |
|
| 86 |
$canonicalParts = self::findCanonicalParts($root, $pathWithoutRoot); |
| 87 |
|
| 88 |
|
| 89 |
self::$buffer[$path] = $canonicalPath = $root.implode('/', $canonicalParts); |
| 90 |
++self::$bufferSize; |
| 91 |
|
| 92 |
|
| 93 |
if (self::$bufferSize > self::CLEANUP_THRESHOLD) { |
| 94 |
self::$buffer = \array_slice(self::$buffer, -self::CLEANUP_SIZE, null, true); |
| 95 |
self::$bufferSize = self::CLEANUP_SIZE; |
| 96 |
} |
| 97 |
|
| 98 |
return $canonicalPath; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static function normalize(string $path): string |
| 112 |
{ |
| 113 |
return '\\' === \DIRECTORY_SEPARATOR ? str_replace('\\', '/', $path) : $path; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public static function getDirectory(string $path): string |
| 140 |
{ |
| 141 |
if ('' === $path) { |
| 142 |
return ''; |
| 143 |
} |
| 144 |
|
| 145 |
$path = self::canonicalize($path); |
| 146 |
|
| 147 |
|
| 148 |
if (false !== $schemeSeparatorPosition = strpos($path, '://')) { |
| 149 |
$scheme = substr($path, 0, $schemeSeparatorPosition + 3); |
| 150 |
$path = substr($path, $schemeSeparatorPosition + 3); |
| 151 |
} else { |
| 152 |
$scheme = ''; |
| 153 |
} |
| 154 |
|
| 155 |
if (false === $dirSeparatorPosition = strrpos($path, '/')) { |
| 156 |
return ''; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
if (0 === $dirSeparatorPosition) { |
| 161 |
return $scheme.'/'; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
if (2 === $dirSeparatorPosition && ctype_alpha($path[0]) && ':' === $path[1]) { |
| 166 |
return $scheme.substr($path, 0, 3); |
| 167 |
} |
| 168 |
|
| 169 |
return $scheme.substr($path, 0, $dirSeparatorPosition); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public static function getHomeDirectory(): string |
| 187 |
{ |
| 188 |
|
| 189 |
if (getenv('HOME')) { |
| 190 |
return self::canonicalize(getenv('HOME')); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
if (getenv('HOMEDRIVE') && getenv('HOMEPATH')) { |
| 195 |
return self::canonicalize(getenv('HOMEDRIVE').getenv('HOMEPATH')); |
| 196 |
} |
| 197 |
|
| 198 |
throw new RuntimeException("Cannot find the home directory path: Your environment or operating system isn't supported."); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public static function getRoot(string $path): string |
| 210 |
{ |
| 211 |
if ('' === $path) { |
| 212 |
return ''; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
if (false !== $schemeSeparatorPosition = strpos($path, '://')) { |
| 217 |
$scheme = substr($path, 0, $schemeSeparatorPosition + 3); |
| 218 |
$path = substr($path, $schemeSeparatorPosition + 3); |
| 219 |
} else { |
| 220 |
$scheme = ''; |
| 221 |
} |
| 222 |
|
| 223 |
$firstCharacter = $path[0]; |
| 224 |
|
| 225 |
if ('/' === $firstCharacter) { |
| 226 |
return $scheme.'/'; |
| 227 |
} |
| 228 |
|
| 229 |
if ('\\' !== \DIRECTORY_SEPARATOR) { |
| 230 |
return ''; |
| 231 |
} |
| 232 |
|
| 233 |
if ('\\' === $firstCharacter) { |
| 234 |
return $scheme.'/'; |
| 235 |
} |
| 236 |
|
| 237 |
$length = \strlen($path); |
| 238 |
|
| 239 |
|
| 240 |
if ($length > 1 && ':' === $path[1] && ctype_alpha($firstCharacter)) { |
| 241 |
|
| 242 |
if (2 === $length) { |
| 243 |
return $scheme.$path.'/'; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
if ('/' === $path[2] || '\\' === $path[2]) { |
| 248 |
return $scheme.$firstCharacter.$path[1].'/'; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return ''; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public static function getFilenameWithoutExtension(string $path, ?string $extension = null): string |
| 262 |
{ |
| 263 |
if ('' === $path) { |
| 264 |
return ''; |
| 265 |
} |
| 266 |
|
| 267 |
if (null !== $extension) { |
| 268 |
|
| 269 |
return rtrim(basename($path, $extension), '.'); |
| 270 |
} |
| 271 |
|
| 272 |
return pathinfo($path, \PATHINFO_FILENAME); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public static function getExtension(string $path, bool $forceLowerCase = false): string |
| 281 |
{ |
| 282 |
if ('' === $path) { |
| 283 |
return ''; |
| 284 |
} |
| 285 |
|
| 286 |
$extension = pathinfo($path, \PATHINFO_EXTENSION); |
| 287 |
|
| 288 |
if ($forceLowerCase) { |
| 289 |
$extension = self::toLower($extension); |
| 290 |
} |
| 291 |
|
| 292 |
return $extension; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public static function hasExtension(string $path, $extensions = null, bool $ignoreCase = false): bool |
| 307 |
{ |
| 308 |
if ('' === $path) { |
| 309 |
return false; |
| 310 |
} |
| 311 |
|
| 312 |
$actualExtension = self::getExtension($path, $ignoreCase); |
| 313 |
|
| 314 |
|
| 315 |
if ([] === $extensions || null === $extensions) { |
| 316 |
return '' !== $actualExtension; |
| 317 |
} |
| 318 |
|
| 319 |
if (\is_string($extensions)) { |
| 320 |
$extensions = [$extensions]; |
| 321 |
} |
| 322 |
|
| 323 |
foreach ($extensions as $key => $extension) { |
| 324 |
if ($ignoreCase) { |
| 325 |
$extension = self::toLower($extension); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
$extensions[$key] = ltrim($extension, '.'); |
| 330 |
} |
| 331 |
|
| 332 |
return \in_array($actualExtension, $extensions, true); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public static function changeExtension(string $path, string $extension): string |
| 344 |
{ |
| 345 |
if ('' === $path) { |
| 346 |
return ''; |
| 347 |
} |
| 348 |
|
| 349 |
$actualExtension = self::getExtension($path); |
| 350 |
$extension = ltrim($extension, '.'); |
| 351 |
|
| 352 |
|
| 353 |
if (str_ends_with($path, '/')) { |
| 354 |
return $path; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
if (!$actualExtension) { |
| 359 |
return $path.(str_ends_with($path, '.') ? '' : '.').$extension; |
| 360 |
} |
| 361 |
|
| 362 |
return substr($path, 0, -\strlen($actualExtension)).$extension; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public static function isAbsolute(string $path): bool |
| 369 |
{ |
| 370 |
if ('' === $path) { |
| 371 |
return false; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
if (str_contains($path, '://') && null !== parse_url($path, \PHP_URL_SCHEME)) { |
| 376 |
return true; |
| 377 |
} |
| 378 |
|
| 379 |
if ('/' === $path[0]) { |
| 380 |
return true; |
| 381 |
} |
| 382 |
|
| 383 |
if ('\\' !== \DIRECTORY_SEPARATOR) { |
| 384 |
return false; |
| 385 |
} |
| 386 |
|
| 387 |
if ('\\' === $path[0]) { |
| 388 |
return true; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
if (\strlen($path) > 1 && ctype_alpha($path[0]) && ':' === $path[1]) { |
| 393 |
|
| 394 |
if (2 === \strlen($path)) { |
| 395 |
return true; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
if ('/' === $path[2] || '\\' === $path[2]) { |
| 400 |
return true; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
public static function isRelative(string $path): bool |
| 408 |
{ |
| 409 |
return !self::isAbsolute($path); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
public static function makeAbsolute(string $path, string $basePath): string |
| 450 |
{ |
| 451 |
if ('' === $basePath) { |
| 452 |
throw new InvalidArgumentException(\sprintf('The base path must be a non-empty string. Got: "%s".', $basePath)); |
| 453 |
} |
| 454 |
|
| 455 |
if (!self::isAbsolute($basePath)) { |
| 456 |
throw new InvalidArgumentException(\sprintf('The base path "%s" is not an absolute path.', $basePath)); |
| 457 |
} |
| 458 |
|
| 459 |
if (self::isAbsolute($path)) { |
| 460 |
return self::canonicalize($path); |
| 461 |
} |
| 462 |
|
| 463 |
if (false !== $schemeSeparatorPosition = strpos($basePath, '://')) { |
| 464 |
$scheme = substr($basePath, 0, $schemeSeparatorPosition + 3); |
| 465 |
$basePath = substr($basePath, $schemeSeparatorPosition + 3); |
| 466 |
} else { |
| 467 |
$scheme = ''; |
| 468 |
} |
| 469 |
|
| 470 |
return $scheme.self::canonicalize(rtrim($basePath, '/'.\DIRECTORY_SEPARATOR).'/'.$path); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public static function makeRelative(string $path, string $basePath): string |
| 524 |
{ |
| 525 |
$path = self::canonicalize($path); |
| 526 |
$basePath = self::canonicalize($basePath); |
| 527 |
|
| 528 |
[$root, $relativePath] = self::split($path); |
| 529 |
[$baseRoot, $relativeBasePath] = self::split($basePath); |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
if ('' === $root && '' !== $baseRoot) { |
| 535 |
|
| 536 |
if ('' === $relativeBasePath) { |
| 537 |
$relativePath = ltrim($relativePath, './'.\DIRECTORY_SEPARATOR); |
| 538 |
} |
| 539 |
|
| 540 |
return $relativePath; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
if ('' !== $root && '' === $baseRoot) { |
| 546 |
throw new InvalidArgumentException(\sprintf('The absolute path "%s" cannot be made relative to the relative path "%s". You should provide an absolute base path instead.', $path, $basePath)); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
if ($baseRoot && $root !== $baseRoot) { |
| 551 |
throw new InvalidArgumentException(\sprintf('The path "%s" cannot be made relative to "%s", because they have different roots ("%s" and "%s").', $path, $basePath, $root, $baseRoot)); |
| 552 |
} |
| 553 |
|
| 554 |
if ('' === $relativeBasePath) { |
| 555 |
return $relativePath; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
$parts = explode('/', $relativePath); |
| 560 |
$baseParts = explode('/', $relativeBasePath); |
| 561 |
$dotDotPrefix = ''; |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
$match = true; |
| 566 |
|
| 567 |
foreach ($baseParts as $index => $basePart) { |
| 568 |
if ($match && isset($parts[$index]) && $basePart === $parts[$index]) { |
| 569 |
unset($parts[$index]); |
| 570 |
|
| 571 |
continue; |
| 572 |
} |
| 573 |
|
| 574 |
$match = false; |
| 575 |
$dotDotPrefix .= '../'; |
| 576 |
} |
| 577 |
|
| 578 |
return rtrim($dotDotPrefix.implode('/', $parts), '/'); |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
public static function isLocal(string $path): bool |
| 585 |
{ |
| 586 |
return '' !== $path && !str_contains($path, '://'); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
public static function getLongestCommonBasePath(string ...$paths): ?string |
| 626 |
{ |
| 627 |
[$bpRoot, $basePath] = self::split(self::canonicalize(reset($paths))); |
| 628 |
|
| 629 |
for (next($paths); null !== key($paths) && '' !== $basePath; next($paths)) { |
| 630 |
[$root, $path] = self::split(self::canonicalize(current($paths))); |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
if ($root !== $bpRoot) { |
| 635 |
return null; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
while (true) { |
| 640 |
if ('.' === $basePath) { |
| 641 |
|
| 642 |
$basePath = ''; |
| 643 |
|
| 644 |
|
| 645 |
continue 2; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
if (str_starts_with($path.'/', $basePath.'/')) { |
| 651 |
|
| 652 |
continue 2; |
| 653 |
} |
| 654 |
|
| 655 |
$basePath = \dirname($basePath); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
return $bpRoot.$basePath; |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
public static function join(string ...$paths): string |
| 666 |
{ |
| 667 |
$finalPath = null; |
| 668 |
$wasScheme = false; |
| 669 |
|
| 670 |
foreach ($paths as $path) { |
| 671 |
if ('' === $path) { |
| 672 |
continue; |
| 673 |
} |
| 674 |
|
| 675 |
if (null === $finalPath) { |
| 676 |
|
| 677 |
$finalPath = $path; |
| 678 |
$wasScheme = str_contains($path, '://'); |
| 679 |
continue; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
if ('/' !== substr($finalPath, -1) && \DIRECTORY_SEPARATOR !== substr($finalPath, -1)) { |
| 684 |
$finalPath .= '/'; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
$finalPath .= $wasScheme ? $path : ltrim($path, '/'); |
| 689 |
$wasScheme = false; |
| 690 |
} |
| 691 |
|
| 692 |
if (null === $finalPath) { |
| 693 |
return ''; |
| 694 |
} |
| 695 |
|
| 696 |
return self::canonicalize($finalPath); |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
public static function isBasePath(string $basePath, string $ofPath): bool |
| 720 |
{ |
| 721 |
$basePath = self::canonicalize($basePath); |
| 722 |
$ofPath = self::canonicalize($ofPath); |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
return str_starts_with($ofPath.'/', rtrim($basePath, '/').'/'); |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
private static function findCanonicalParts(string $root, string $pathWithoutRoot): array |
| 736 |
{ |
| 737 |
$parts = explode('/', $pathWithoutRoot); |
| 738 |
|
| 739 |
$canonicalParts = []; |
| 740 |
|
| 741 |
|
| 742 |
foreach ($parts as $part) { |
| 743 |
if ('.' === $part || '' === $part) { |
| 744 |
continue; |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
if ('..' === $part && \count($canonicalParts) > 0 && '..' !== $canonicalParts[\count($canonicalParts) - 1]) { |
| 750 |
array_pop($canonicalParts); |
| 751 |
|
| 752 |
continue; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
if ('..' !== $part || '' === $root) { |
| 757 |
$canonicalParts[] = $part; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
return $canonicalParts; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
private static function split(string $path): array |
| 782 |
{ |
| 783 |
if ('' === $path) { |
| 784 |
return ['', '']; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
if (false !== $schemeSeparatorPosition = strpos($path, '://')) { |
| 789 |
$root = substr($path, 0, $schemeSeparatorPosition + 3); |
| 790 |
$path = substr($path, $schemeSeparatorPosition + 3); |
| 791 |
} else { |
| 792 |
$root = ''; |
| 793 |
} |
| 794 |
|
| 795 |
$length = \strlen($path); |
| 796 |
|
| 797 |
|
| 798 |
if (str_starts_with($path, '/')) { |
| 799 |
$root .= '/'; |
| 800 |
$path = $length > 1 ? substr($path, 1) : ''; |
| 801 |
} elseif ('\\' === \DIRECTORY_SEPARATOR && $length > 1 && ctype_alpha($path[0]) && ':' === $path[1]) { |
| 802 |
if (2 === $length) { |
| 803 |
|
| 804 |
$root .= $path.'/'; |
| 805 |
$path = ''; |
| 806 |
} elseif ('/' === $path[2]) { |
| 807 |
|
| 808 |
$root .= substr($path, 0, 3); |
| 809 |
$path = $length > 3 ? substr($path, 3) : ''; |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
return [$root, $path]; |
| 814 |
} |
| 815 |
|
| 816 |
private static function toLower(string $string): string |
| 817 |
{ |
| 818 |
if (false !== $encoding = mb_detect_encoding($string, null, true)) { |
| 819 |
return mb_strtolower($string, $encoding); |
| 820 |
} |
| 821 |
|
| 822 |
return strtolower($string); |
| 823 |
} |
| 824 |
|
| 825 |
private function __construct() |
| 826 |
{ |
| 827 |
} |
| 828 |
} |