| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\Framework\Support; |
| 4 |
|
| 5 |
use FluentBoards\Framework\Foundation\App; |
| 6 |
|
| 7 |
class Path |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Get WordPress dir path. |
| 11 |
* |
| 12 |
* @return string Abs WP dir path |
| 13 |
*/ |
| 14 |
public static function wp() |
| 15 |
{ |
| 16 |
return static::resolve(App::make('path') . '../../..'); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Get WordPress dir path. |
| 21 |
* |
| 22 |
* @return string Abs WP dir path |
| 23 |
*/ |
| 24 |
public static function plugin() |
| 25 |
{ |
| 26 |
return App::make()['path']; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Get plugin dir path. |
| 31 |
* |
| 32 |
* @param string $segment folder name (i.e: app.Models) |
| 33 |
* @param bool $isFile whether the segment is for filename. |
| 34 |
* @return string Abs plugin dir path |
| 35 |
*/ |
| 36 |
public static function of($segment, $isFile = false) |
| 37 |
{ |
| 38 |
$app = App::make(); |
| 39 |
|
| 40 |
if (isset($app['path.' . $segment])) { |
| 41 |
$path = $app['path.' . $segment]; |
| 42 |
} else { |
| 43 |
$ds = DIRECTORY_SEPARATOR; |
| 44 |
|
| 45 |
if (str_contains($segment, '.')) { |
| 46 |
if (!str_contains($segment, $ds)) { |
| 47 |
$segment = str_replace('.', $ds, $segment); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$path = static::implode( |
| 52 |
$app['path'], ...explode($ds, $segment) |
| 53 |
); |
| 54 |
|
| 55 |
// If not directory exists then check if the path was |
| 56 |
// given for a filename so let's replace the last |
| 57 |
// seperator with a dot to make the last part |
| 58 |
// a file name and check if file exists. |
| 59 |
if ($isFile || !file_exists($path)) { |
| 60 |
if ( |
| 61 |
file_exists($filePath = substr_replace( |
| 62 |
$path, '.', strrpos($path, $ds), 1 |
| 63 |
)) |
| 64 |
) return $filePath; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return $path; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Determines whether the given path is an absolute path. |
| 73 |
* |
| 74 |
* @param string $path The path to check. |
| 75 |
* @return bool True if the path is absolute, false otherwise. |
| 76 |
*/ |
| 77 |
public static function isAbsolute($path) |
| 78 |
{ |
| 79 |
return path_is_absolute($path); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Determines whether the given path is an relative path. |
| 84 |
* |
| 85 |
* @param string $path The path to check. |
| 86 |
* @return bool True if the path is relative, false otherwise. |
| 87 |
*/ |
| 88 |
public static function isRelative($path) |
| 89 |
{ |
| 90 |
return !path_is_absolute($path); |
| 91 |
} |
| 92 |
/** |
| 93 |
* Joins two filesystem paths together. |
| 94 |
* |
| 95 |
* For example, 'give me $path relative to $base'. |
| 96 |
* If the $path is absolute, then the |
| 97 |
* full path will be returned. |
| 98 |
* |
| 99 |
* @param string $base Base path. |
| 100 |
* @param string $path Path relative to $base. |
| 101 |
* @return string The path with the base or absolute path. |
| 102 |
*/ |
| 103 |
public static function join($base, $path) |
| 104 |
{ |
| 105 |
return path_join($base, $path); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Normalizes a filesystem path. |
| 110 |
* |
| 111 |
* On windows systems, replaces backslashes with forward slashes |
| 112 |
* and forces upper-case drive letters. |
| 113 |
* Allows for two leading slashes for Windows network shares, but |
| 114 |
* ensures that all other duplicate slashes are reduced to a single. |
| 115 |
* |
| 116 |
* @param string $path Path to normalize. |
| 117 |
* @return string Normalized path. |
| 118 |
*/ |
| 119 |
public static function normalize($path) |
| 120 |
{ |
| 121 |
return wp_normalize_path($path); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Implodes path segments using the appropriate directory separator. |
| 126 |
* |
| 127 |
* @param string[] $paths |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
public static function implode(...$paths) |
| 131 |
{ |
| 132 |
$firstSegment = array_shift($paths); |
| 133 |
|
| 134 |
$allSegments = implode(DIRECTORY_SEPARATOR, array_map(function($path) { |
| 135 |
return trim($path, DIRECTORY_SEPARATOR); |
| 136 |
}, $paths)); |
| 137 |
|
| 138 |
return implode(DIRECTORY_SEPARATOR, [ |
| 139 |
rtrim($firstSegment, DIRECTORY_SEPARATOR), $allSegments |
| 140 |
]); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Resolves an absolute path from one or more path segments. |
| 145 |
* |
| 146 |
* @param string[] $paths Path segments to resolve. |
| 147 |
* @return string The resolved absolute path. |
| 148 |
*/ |
| 149 |
public static function resolve(...$paths) |
| 150 |
{ |
| 151 |
return realpath(static::implode(...$paths)); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns the directory name of a path. |
| 156 |
* |
| 157 |
* @param string $path The path. |
| 158 |
* @return string The directory name. |
| 159 |
*/ |
| 160 |
public static function dirname($path) |
| 161 |
{ |
| 162 |
return dirname($path); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Returns the last portion of a path, optionally removing a provided extension. |
| 167 |
* |
| 168 |
* @param string $path The path. |
| 169 |
* @param string|null $suffix Optional extension to remove. |
| 170 |
* @return string The last portion of the path. |
| 171 |
*/ |
| 172 |
public static function basename($path, $suffix = null) |
| 173 |
{ |
| 174 |
$baseName = basename($path); |
| 175 |
|
| 176 |
return $suffix ? rtrim($baseName, $suffix) : $baseName; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Returns the extension of the path. |
| 181 |
* |
| 182 |
* @param string $path The path. |
| 183 |
* @return string The extension. |
| 184 |
*/ |
| 185 |
public static function extname($path) |
| 186 |
{ |
| 187 |
return pathinfo($path, PATHINFO_EXTENSION); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Finds the closest dir/file from upward. |
| 192 |
* |
| 193 |
* @param string $path |
| 194 |
* @return string|null |
| 195 |
*/ |
| 196 |
public static function closest($path) |
| 197 |
{ |
| 198 |
$count = 0; |
| 199 |
|
| 200 |
$cwd = dirname(debug_backtrace(false, 1)[0]['file']) . '/../'; |
| 201 |
|
| 202 |
while (!file_exists($target = $cwd . $path)) { |
| 203 |
|
| 204 |
$cwd .= '../'; |
| 205 |
|
| 206 |
if (($count++) > 25) break; |
| 207 |
} |
| 208 |
|
| 209 |
return (string) realpath($target); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Breaks down a path into an an array like: |
| 214 |
* root, dir, base, ext, and name. |
| 215 |
* |
| 216 |
* @param string $path The path to parse. |
| 217 |
* @return array An associative array with path information. |
| 218 |
*/ |
| 219 |
public static function parse($path) |
| 220 |
{ |
| 221 |
$info = pathinfo($path); |
| 222 |
|
| 223 |
return [ |
| 224 |
'dir' => isset($info['dirname']) ? $info['dirname'] : null, |
| 225 |
'root' => isset($info['dirname']) ? $info['dirname'] : null, |
| 226 |
'base' => isset($info['basename']) ? $info['basename'] : null, |
| 227 |
'name' => isset($info['filename']) ? $info['filename'] : null, |
| 228 |
'ext' => isset($info['extension']) ? '.' . $info['extension'] : null, |
| 229 |
]; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|