PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.94
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.94
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Support / Path.php

Path.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.94, at vendor/wpfluent/framework/src/WPFluent/Support/Path.php

226 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Support;
4
5 use FluentCommunity\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($suffix = '')
25 {
26 return App::make()['path'] . $suffix;
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 no 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 * @see https://developer.wordpress.org/reference/functions/path_is_absolute
77 */
78 public static function isAbsolute($path)
79 {
80 return path_is_absolute($path);
81 }
82
83 /**
84 * Determines whether the given path is an relative path.
85 *
86 * @param string $path The path to check.
87 * @return bool True if the path is relative, false otherwise.
88 */
89 public static function isRelative($path)
90 {
91 return !path_is_absolute($path);
92 }
93 /**
94 * Joins two filesystem paths together.
95 *
96 * For example, 'give me $path relative to $base'.
97 * If the $path is absolute, then the
98 * full path will be returned.
99 *
100 * @param string $base Base path.
101 * @param string $path Path relative to $base.
102 * @return string The path with the base or absolute path.
103 * @see https://developer.wordpress.org/reference/functions/path_join
104 */
105 public static function join($base, $path)
106 {
107 return path_join($base, $path);
108 }
109
110 /**
111 * Normalizes a filesystem path.
112 *
113 * On windows systems, replaces backslashes with forward slashes
114 * and forces upper-case drive letters.
115 * Allows for two leading slashes for Windows network shares, but
116 * ensures that all other duplicate slashes are reduced to a single.
117 *
118 * @param string $path Path to normalize.
119 * @return string Normalized path.
120 * @see https://developer.wordpress.org/reference/functions/wp_normalize_path
121 */
122 public static function normalize($path)
123 {
124 return wp_normalize_path($path);
125 }
126
127 /**
128 * Implodes path segments using the appropriate directory separator.
129 *
130 * @param string[] $paths
131 * @return string
132 */
133 public static function implode(...$paths)
134 {
135 $firstSegment = array_shift($paths);
136
137 $allSegments = implode(DIRECTORY_SEPARATOR, array_map(function($path) {
138 return trim($path, DIRECTORY_SEPARATOR);
139 }, $paths));
140
141 return implode(DIRECTORY_SEPARATOR, [
142 rtrim($firstSegment, DIRECTORY_SEPARATOR), $allSegments
143 ]);
144 }
145
146 /**
147 * Resolves an absolute path from one or more path segments.
148 *
149 * @param string[] $paths Path segments to resolve.
150 * @return string The resolved absolute path.
151 */
152 public static function resolve(...$paths)
153 {
154 return realpath(static::implode(...$paths));
155 }
156
157 /**
158 * Returns the directory name of a path.
159 *
160 * @param string $path The path.
161 * @return string The directory name.
162 */
163 public static function dirname($path)
164 {
165 return dirname($path);
166 }
167
168 /**
169 * Returns the last portion of a path, optionally removing a provided extension.
170 *
171 * @param string $path The path.
172 * @param string|null $suffix Optional extension to remove.
173 * @return string The last portion of the path.
174 */
175 public static function basename($path, $suffix = null)
176 {
177 $baseName = basename($path);
178
179 return $suffix ? rtrim($baseName, $suffix) : $baseName;
180 }
181
182 /**
183 * Returns the extension of the path.
184 *
185 * @param string $path The path.
186 * @return string The extension.
187 */
188 public static function extname($path)
189 {
190 return pathinfo($path, PATHINFO_EXTENSION);
191 }
192
193 /**
194 * Finds the closest dir/file from upward.
195 *
196 * @param string $path
197 * @return string|null
198 */
199 public static function closest($path, $limit = 25)
200 {
201 $count = 0;
202
203 $cwd = dirname(debug_backtrace(false, 1)[0]['file']) . '/../';
204
205 while (!file_exists($target = $cwd . $path)) {
206
207 $cwd .= '../';
208
209 if (($count++) > $limit) break;
210 }
211
212 return (string) realpath($target);
213 }
214
215 /**
216 * Breaks down a path into an an array.
217 *
218 * @param string $path The path to parse.
219 * @return array An associative array with path information.
220 */
221 public static function parse($path)
222 {
223 return pathinfo($path);
224 }
225 }
226