File.php
3 weeks ago
FileSystemServiceProvider.php
3 weeks ago
Fileable.php
3 weeks ago
Filesystem.php
3 weeks ago
Path.php
3 weeks ago
UploadedFile.php
3 weeks ago
Path.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Static utilities for joining path segments and normalizing filesystem paths. |
| 5 | * Handles trailing slashes, parent directory traversal, and cross-platform separators. |
| 6 | * Used by Application and generators for consistent path construction. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Filesystem |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Filesystem; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | class Path |
| 16 | { |
| 17 | /** |
| 18 | * Join path segments onto a base path. |
| 19 | * |
| 20 | * @param mixed $base The base. |
| 21 | * @param mixed $paths The paths. |
| 22 | * |
| 23 | * @return string |
| 24 | * |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | public static function join($base, ...$paths) |
| 28 | { |
| 29 | foreach ($paths as $index => $path) { |
| 30 | if (empty($path) && $path !== '0') { |
| 31 | unset($paths[$index]); |
| 32 | } else { |
| 33 | $paths[$index] = \DIRECTORY_SEPARATOR . \ltrim($path, \DIRECTORY_SEPARATOR); |
| 34 | } |
| 35 | } |
| 36 | return $base . \implode('', $paths); |
| 37 | } |
| 38 | /** |
| 39 | * Normalize a filesystem path without requiring it to exist. |
| 40 | * |
| 41 | * @param mixed $path The path. |
| 42 | * |
| 43 | * @return string |
| 44 | * |
| 45 | * @since 1.0.0 |
| 46 | */ |
| 47 | public static function normalize($path) |
| 48 | { |
| 49 | $path = \str_replace('\\', '/', $path); |
| 50 | $is_absolute = $path !== '' && $path[0] === '/'; |
| 51 | $parts = []; |
| 52 | foreach (\explode('/', $path) as $part) { |
| 53 | if ($part === '' || $part === '.') { |
| 54 | continue; |
| 55 | } |
| 56 | if ($part === '..') { |
| 57 | if (!empty($parts)) { |
| 58 | \array_pop($parts); |
| 59 | } |
| 60 | continue; |
| 61 | } |
| 62 | $parts[] = $part; |
| 63 | } |
| 64 | $normalized = \implode('/', $parts); |
| 65 | if ($is_absolute) { |
| 66 | return '/' . $normalized; |
| 67 | } |
| 68 | return $normalized; |
| 69 | } |
| 70 | } |
| 71 |