@@ -45,7 +45,7 @@ |
||
45 | 45 | { |
46 | 46 | try { |
47 | 47 | return $object->{$method}(...$parameters); |
48 | - } catch (Error|BadMethodCallException $e) { |
|
48 | + } catch (Error | BadMethodCallException $e) { |
|
49 | 49 | static::BadMethodCallException($method); |
50 | 50 | } |
51 | 51 | } |
@@ -248,7 +248,9 @@ |
||
248 | 248 | */ |
249 | 249 | public static function limit($value, $limit, $end = '...'): string |
250 | 250 | { |
251 | - if (static::length($value) <= $limit) return $value; |
|
251 | + if (static::length($value) <= $limit) { |
|
252 | + return $value; |
|
253 | + } |
|
252 | 254 | |
253 | 255 | return rtrim(mb_substr($value, 0, $limit, 'UTF-8')).$end; |
254 | 256 | } |
@@ -42,8 +42,8 @@ discard block |
||
42 | 42 | { |
43 | 43 | $testTime = $this->getConvertedUTC($time, $timezone); |
44 | 44 | $ourTime = $this->toDateTime() |
45 | - ->setTimezone(new DateTimeZone('UTC')) |
|
46 | - ->format('Y-m-d H:i:s'); |
|
45 | + ->setTimezone(new DateTimeZone('UTC')) |
|
46 | + ->format('Y-m-d H:i:s'); |
|
47 | 47 | |
48 | 48 | return $testTime->format('Y-m-d H:i:s') === $ourTime; |
49 | 49 | } |
@@ -65,15 +65,15 @@ discard block |
||
65 | 65 | return $testTime < $ourTime; |
66 | 66 | } |
67 | 67 | |
68 | - /** |
|
69 | - * Determines if the current instance's time is after test time, |
|
70 | - * after converting to UTC. |
|
71 | - * |
|
72 | - * @param \DateTime|string $time |
|
73 | - * @param \DatetimeZone|string|null $timezone |
|
74 | - * |
|
75 | - * @return bool |
|
76 | - */ |
|
68 | + /** |
|
69 | + * Determines if the current instance's time is after test time, |
|
70 | + * after converting to UTC. |
|
71 | + * |
|
72 | + * @param \DateTime|string $time |
|
73 | + * @param \DatetimeZone|string|null $timezone |
|
74 | + * |
|
75 | + * @return bool |
|
76 | + */ |
|
77 | 77 | public function isAfter($time, string $timezone = null): bool |
78 | 78 | { |
79 | 79 | $testTime = $this->getConvertedUTC($time, $timezone)->getTimestamp(); |
@@ -63,12 +63,12 @@ discard block |
||
63 | 63 | $this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone); |
64 | 64 | |
65 | 65 | if ( ! empty($time)) { |
66 | - if (is_string($time) && static::hasRelativeKeywords($time)) { |
|
67 | - $dateTime = new DateTime('now', $this->timezone); |
|
68 | - $dateTime->modify($time); |
|
66 | + if (is_string($time) && static::hasRelativeKeywords($time)) { |
|
67 | + $dateTime = new DateTime('now', $this->timezone); |
|
68 | + $dateTime->modify($time); |
|
69 | 69 | |
70 | - $time = $dateTime->format('Y-m-d H:i:s'); |
|
71 | - } |
|
70 | + $time = $dateTime->format('Y-m-d H:i:s'); |
|
71 | + } |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | return parent::__construct($time, $this->timezone); |
@@ -123,8 +123,8 @@ discard block |
||
123 | 123 | * @return \Syscodes\Components\Support\Chronos\Time |
124 | 124 | */ |
125 | 125 | public static function yesterday($timezone = null, string $locale = null) |
126 | - { |
|
127 | - return static::parse(date('Y-m-d 00:00:00', strtotime('-1 day')), $timezone, $locale); |
|
126 | + { |
|
127 | + return static::parse(date('Y-m-d 00:00:00', strtotime('-1 day')), $timezone, $locale); |
|
128 | 128 | } |
129 | 129 | |
130 | 130 | /** |
@@ -136,8 +136,8 @@ discard block |
||
136 | 136 | * @return \Syscodes\Components\Support\Chronos\Time |
137 | 137 | */ |
138 | 138 | public static function tomorrow($timezone = null, string $locale = null) |
139 | - { |
|
140 | - return static::parse(date('Y-m-d 00:00:00', strtotime('+1 day')), $timezone, $locale); |
|
139 | + { |
|
140 | + return static::parse(date('Y-m-d 00:00:00', strtotime('+1 day')), $timezone, $locale); |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | /** |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | |
270 | 270 | /** |
271 | 271 | * Creates an instance of Time that will be returned during testing |
272 | - * when calling 'Time::now' instead of the current time. |
|
272 | + * when calling 'Time::now' instead of the current time. |
|
273 | 273 | * |
274 | 274 | * @param \Syscodes\Components\Support\Chronos\Time|string $datetime |
275 | 275 | * @param string|null $timezone |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | |
103 | 103 | /** |
104 | 104 | * Returns boolean whether the passed timezone is the same as |
105 | - * the local timezone. |
|
105 | + * the local timezone. |
|
106 | 106 | * |
107 | 107 | * @return bool |
108 | 108 | */ |
@@ -203,11 +203,11 @@ discard block |
||
203 | 203 | return $this->toLocalizedFormatter('yyyy-MM-dd'); |
204 | 204 | } |
205 | 205 | |
206 | - /** |
|
207 | - * Returns a localized version of the time in nicer date format. |
|
208 | - * |
|
209 | - * @return string |
|
210 | - */ |
|
206 | + /** |
|
207 | + * Returns a localized version of the time in nicer date format. |
|
208 | + * |
|
209 | + * @return string |
|
210 | + */ |
|
211 | 211 | public function toTimeString() |
212 | 212 | { |
213 | 213 | return $this->toLocalizedFormatter('HH:mm:ss'); |
@@ -32,10 +32,10 @@ |
||
32 | 32 | trait Utilities |
33 | 33 | { |
34 | 34 | /** |
35 | - * Used to check time string to determine if it is relative time or not. |
|
36 | - * |
|
37 | - * @var string $relativePattern |
|
38 | - */ |
|
35 | + * Used to check time string to determine if it is relative time or not. |
|
36 | + * |
|
37 | + * @var string $relativePattern |
|
38 | + */ |
|
39 | 39 | protected static $relativePattern = '/this|next|last|tomorrow|yesterday|midnight|today|[+-]|first|last|ago/i'; |
40 | 40 | |
41 | 41 | /** |
@@ -33,828 +33,828 @@ |
||
33 | 33 | */ |
34 | 34 | class Filesystem |
35 | 35 | { |
36 | - /** |
|
37 | - * Enable locking for file reading and writing. |
|
38 | - * |
|
39 | - * @var null|bool $lock |
|
40 | - */ |
|
41 | - public $lock = null; |
|
42 | - |
|
43 | - /** |
|
44 | - * Holds the file handler resource if the file is opened. |
|
45 | - * |
|
46 | - * @var resource $handler |
|
47 | - */ |
|
48 | - protected $handler; |
|
49 | - |
|
50 | - /** |
|
51 | - * The files size in bytes. |
|
52 | - * |
|
53 | - * @var float $size |
|
54 | - */ |
|
55 | - protected $size; |
|
56 | - |
|
57 | - /** |
|
58 | - * Append given data string to this file. |
|
59 | - * |
|
60 | - * @param string $path |
|
61 | - * @param string $data |
|
62 | - * |
|
63 | - * @return bool |
|
64 | - */ |
|
65 | - public function append($path, $data) |
|
66 | - { |
|
67 | - return file_put_contents($path, $data, FILE_APPEND); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Copy a file to a new location. |
|
72 | - * |
|
73 | - * @param string $path |
|
74 | - * @param string $target |
|
75 | - * |
|
76 | - * @return bool |
|
77 | - */ |
|
78 | - public function copy($path, $target): bool |
|
79 | - { |
|
80 | - return copy($path, $target); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Get the contents of a file. |
|
85 | - * |
|
86 | - * @param string $path |
|
87 | - * @param bool $lock |
|
88 | - * @param bool $force |
|
89 | - * |
|
90 | - * @return string |
|
91 | - * |
|
92 | - * @throws FileNotFoundException |
|
93 | - */ |
|
94 | - public function get($path, $lock = false, $force = false): string |
|
95 | - { |
|
96 | - if ($this->isFile($path)) { |
|
97 | - return $lock ? $this->read($path, $force) : file_get_contents($path); |
|
98 | - } |
|
99 | - |
|
100 | - throw new FileNotFoundException($path); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Get contents of a file with shared access. |
|
105 | - * |
|
106 | - * @param string $path |
|
107 | - * @param bool $force |
|
108 | - * |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - public function read($path, $force = false): string |
|
112 | - { |
|
113 | - $contents = ''; |
|
114 | - |
|
115 | - $this->open($path, 'rb', $force); |
|
36 | + /** |
|
37 | + * Enable locking for file reading and writing. |
|
38 | + * |
|
39 | + * @var null|bool $lock |
|
40 | + */ |
|
41 | + public $lock = null; |
|
42 | + |
|
43 | + /** |
|
44 | + * Holds the file handler resource if the file is opened. |
|
45 | + * |
|
46 | + * @var resource $handler |
|
47 | + */ |
|
48 | + protected $handler; |
|
49 | + |
|
50 | + /** |
|
51 | + * The files size in bytes. |
|
52 | + * |
|
53 | + * @var float $size |
|
54 | + */ |
|
55 | + protected $size; |
|
56 | + |
|
57 | + /** |
|
58 | + * Append given data string to this file. |
|
59 | + * |
|
60 | + * @param string $path |
|
61 | + * @param string $data |
|
62 | + * |
|
63 | + * @return bool |
|
64 | + */ |
|
65 | + public function append($path, $data) |
|
66 | + { |
|
67 | + return file_put_contents($path, $data, FILE_APPEND); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Copy a file to a new location. |
|
72 | + * |
|
73 | + * @param string $path |
|
74 | + * @param string $target |
|
75 | + * |
|
76 | + * @return bool |
|
77 | + */ |
|
78 | + public function copy($path, $target): bool |
|
79 | + { |
|
80 | + return copy($path, $target); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Get the contents of a file. |
|
85 | + * |
|
86 | + * @param string $path |
|
87 | + * @param bool $lock |
|
88 | + * @param bool $force |
|
89 | + * |
|
90 | + * @return string |
|
91 | + * |
|
92 | + * @throws FileNotFoundException |
|
93 | + */ |
|
94 | + public function get($path, $lock = false, $force = false): string |
|
95 | + { |
|
96 | + if ($this->isFile($path)) { |
|
97 | + return $lock ? $this->read($path, $force) : file_get_contents($path); |
|
98 | + } |
|
99 | + |
|
100 | + throw new FileNotFoundException($path); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Get contents of a file with shared access. |
|
105 | + * |
|
106 | + * @param string $path |
|
107 | + * @param bool $force |
|
108 | + * |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + public function read($path, $force = false): string |
|
112 | + { |
|
113 | + $contents = ''; |
|
114 | + |
|
115 | + $this->open($path, 'rb', $force); |
|
116 | 116 | |
117 | - if ($this->handler) { |
|
118 | - try { |
|
119 | - if (flock($this->handler, LOCK_SH)) { |
|
120 | - $this->clearStatCache($path); |
|
117 | + if ($this->handler) { |
|
118 | + try { |
|
119 | + if (flock($this->handler, LOCK_SH)) { |
|
120 | + $this->clearStatCache($path); |
|
121 | 121 | |
122 | - $contents = fread($this->handler, $this->getSize($path) ?: 1); |
|
122 | + $contents = fread($this->handler, $this->getSize($path) ?: 1); |
|
123 | 123 | |
124 | - while ( ! feof($this->handler)) { |
|
125 | - $contents .= fgets($this->handler, 4096); |
|
126 | - } |
|
127 | - |
|
128 | - flock($this->handler, LOCK_UN); |
|
129 | - } |
|
130 | - } finally { |
|
131 | - $this->close(); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - return trim($contents); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Opens the current file with a given $mode. |
|
140 | - * |
|
141 | - * @param string $path |
|
142 | - * @param string $mode A valid 'fopen' mode string (r|w|a ...) |
|
143 | - * @param bool $force |
|
144 | - * |
|
145 | - * @return bool |
|
146 | - */ |
|
147 | - public function open($path, $mode, $force = false): bool |
|
148 | - { |
|
149 | - if ( ! $force && is_resource($this->handler)) { |
|
150 | - return true; |
|
151 | - } |
|
152 | - |
|
153 | - if ($this->exists($path) === false) { |
|
154 | - if ($this->create($path) === false) { |
|
155 | - return false; |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - $this->handler = fopen($path, $mode); |
|
160 | - |
|
161 | - return is_resource($this->handler); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Creates the file. |
|
166 | - * |
|
167 | - * @param string $path |
|
168 | - * |
|
169 | - * @return bool |
|
170 | - */ |
|
171 | - public function create($path): bool |
|
172 | - { |
|
173 | - if (($this->isDirectory($path)) && ($this->isWritable($path)) || ( ! $this->exists($path))) { |
|
174 | - if (touch($path)) { |
|
175 | - return true; |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - return false; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Determine if a file exists. |
|
184 | - * |
|
185 | - * @param string $path |
|
186 | - * |
|
187 | - * @return bool |
|
188 | - */ |
|
189 | - public function exists($path): bool |
|
190 | - { |
|
191 | - $this->clearStatCache($path); |
|
192 | - |
|
193 | - return file_exists($path); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Clear PHP's internal stat cache. |
|
198 | - * |
|
199 | - * @param string $path |
|
200 | - * @param bool $all Clear all cache or not |
|
201 | - * |
|
202 | - * @return void |
|
203 | - */ |
|
204 | - public function clearStatCache($path, $all = false): void |
|
205 | - { |
|
206 | - if ($all === false) { |
|
207 | - clearstatcache(false, $path); |
|
208 | - } |
|
209 | - |
|
210 | - clearstatcache(); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Get the returned value of a file. |
|
215 | - * |
|
216 | - * @param string $path |
|
217 | - * @param array $data |
|
218 | - * |
|
219 | - * @return mixed |
|
220 | - * |
|
221 | - * @throws \Syscodes\Filesystem\Exceptions\FileNotFoundException |
|
222 | - */ |
|
223 | - public function getRequire($path, array $data = []) |
|
224 | - { |
|
225 | - if ($this->isFile($path)) { |
|
226 | - $__path = $path; |
|
227 | - $__data = $data; |
|
228 | - |
|
229 | - return (static function () use ($__path, $__data) { |
|
230 | - extract($__data, EXTR_SKIP); |
|
231 | - |
|
232 | - return require $__path; |
|
233 | - })(); |
|
234 | - } |
|
235 | - |
|
236 | - throw new FileNotFoundException($path); |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * Require the given file once. |
|
241 | - * |
|
242 | - * @param string $path |
|
243 | - * @param array $data |
|
244 | - * |
|
245 | - * @return mixed |
|
246 | - * |
|
247 | - * @throws \Syscodes\Filesystem\Exceptions\FileNotFoundException |
|
248 | - */ |
|
249 | - public function getRequireOnce($path, array $data = []) |
|
250 | - { |
|
251 | - if ($this->isFile($path)) { |
|
252 | - $__path = $path; |
|
253 | - $__data = $data; |
|
254 | - |
|
255 | - return (static function () use ($__path, $__data) { |
|
256 | - extract($__data, EXTR_SKIP); |
|
257 | - |
|
258 | - return require_once $__path; |
|
259 | - })(); |
|
260 | - } |
|
261 | - |
|
262 | - throw new FileNotFoundException($path); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Retrieve the file size. |
|
267 | - * |
|
268 | - * Implementations SHOULD return the value stored in the "size" key of |
|
269 | - * the file in the $_FILES array if available, as PHP calculates this |
|
270 | - * based on the actual size transmitted. |
|
271 | - * |
|
272 | - * @param string $path |
|
273 | - * @param string $unit |
|
274 | - * |
|
275 | - * @return int|null The file size in bytes or null if unknown |
|
276 | - */ |
|
277 | - public function getSize($path, $unit = 'b') |
|
278 | - { |
|
279 | - if ( ! $this->exists($path)) { |
|
280 | - if (is_null($this->size)) { |
|
281 | - $this->size = filesize($path); |
|
282 | - } |
|
283 | - |
|
284 | - return match (strtolower($unit)) { |
|
285 | - 'kb' => number_format($this->size / 1024, 3), |
|
286 | - 'mb' => number_format(($this->size / 1024) / 1024, 3), |
|
287 | - default => $this->size, |
|
288 | - }; |
|
289 | - } |
|
290 | - |
|
291 | - return null; |
|
292 | - } |
|
124 | + while ( ! feof($this->handler)) { |
|
125 | + $contents .= fgets($this->handler, 4096); |
|
126 | + } |
|
127 | + |
|
128 | + flock($this->handler, LOCK_UN); |
|
129 | + } |
|
130 | + } finally { |
|
131 | + $this->close(); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + return trim($contents); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Opens the current file with a given $mode. |
|
140 | + * |
|
141 | + * @param string $path |
|
142 | + * @param string $mode A valid 'fopen' mode string (r|w|a ...) |
|
143 | + * @param bool $force |
|
144 | + * |
|
145 | + * @return bool |
|
146 | + */ |
|
147 | + public function open($path, $mode, $force = false): bool |
|
148 | + { |
|
149 | + if ( ! $force && is_resource($this->handler)) { |
|
150 | + return true; |
|
151 | + } |
|
152 | + |
|
153 | + if ($this->exists($path) === false) { |
|
154 | + if ($this->create($path) === false) { |
|
155 | + return false; |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + $this->handler = fopen($path, $mode); |
|
160 | + |
|
161 | + return is_resource($this->handler); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Creates the file. |
|
166 | + * |
|
167 | + * @param string $path |
|
168 | + * |
|
169 | + * @return bool |
|
170 | + */ |
|
171 | + public function create($path): bool |
|
172 | + { |
|
173 | + if (($this->isDirectory($path)) && ($this->isWritable($path)) || ( ! $this->exists($path))) { |
|
174 | + if (touch($path)) { |
|
175 | + return true; |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + return false; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Determine if a file exists. |
|
184 | + * |
|
185 | + * @param string $path |
|
186 | + * |
|
187 | + * @return bool |
|
188 | + */ |
|
189 | + public function exists($path): bool |
|
190 | + { |
|
191 | + $this->clearStatCache($path); |
|
192 | + |
|
193 | + return file_exists($path); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Clear PHP's internal stat cache. |
|
198 | + * |
|
199 | + * @param string $path |
|
200 | + * @param bool $all Clear all cache or not |
|
201 | + * |
|
202 | + * @return void |
|
203 | + */ |
|
204 | + public function clearStatCache($path, $all = false): void |
|
205 | + { |
|
206 | + if ($all === false) { |
|
207 | + clearstatcache(false, $path); |
|
208 | + } |
|
209 | + |
|
210 | + clearstatcache(); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Get the returned value of a file. |
|
215 | + * |
|
216 | + * @param string $path |
|
217 | + * @param array $data |
|
218 | + * |
|
219 | + * @return mixed |
|
220 | + * |
|
221 | + * @throws \Syscodes\Filesystem\Exceptions\FileNotFoundException |
|
222 | + */ |
|
223 | + public function getRequire($path, array $data = []) |
|
224 | + { |
|
225 | + if ($this->isFile($path)) { |
|
226 | + $__path = $path; |
|
227 | + $__data = $data; |
|
228 | + |
|
229 | + return (static function () use ($__path, $__data) { |
|
230 | + extract($__data, EXTR_SKIP); |
|
231 | + |
|
232 | + return require $__path; |
|
233 | + })(); |
|
234 | + } |
|
235 | + |
|
236 | + throw new FileNotFoundException($path); |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * Require the given file once. |
|
241 | + * |
|
242 | + * @param string $path |
|
243 | + * @param array $data |
|
244 | + * |
|
245 | + * @return mixed |
|
246 | + * |
|
247 | + * @throws \Syscodes\Filesystem\Exceptions\FileNotFoundException |
|
248 | + */ |
|
249 | + public function getRequireOnce($path, array $data = []) |
|
250 | + { |
|
251 | + if ($this->isFile($path)) { |
|
252 | + $__path = $path; |
|
253 | + $__data = $data; |
|
254 | + |
|
255 | + return (static function () use ($__path, $__data) { |
|
256 | + extract($__data, EXTR_SKIP); |
|
257 | + |
|
258 | + return require_once $__path; |
|
259 | + })(); |
|
260 | + } |
|
261 | + |
|
262 | + throw new FileNotFoundException($path); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Retrieve the file size. |
|
267 | + * |
|
268 | + * Implementations SHOULD return the value stored in the "size" key of |
|
269 | + * the file in the $_FILES array if available, as PHP calculates this |
|
270 | + * based on the actual size transmitted. |
|
271 | + * |
|
272 | + * @param string $path |
|
273 | + * @param string $unit |
|
274 | + * |
|
275 | + * @return int|null The file size in bytes or null if unknown |
|
276 | + */ |
|
277 | + public function getSize($path, $unit = 'b') |
|
278 | + { |
|
279 | + if ( ! $this->exists($path)) { |
|
280 | + if (is_null($this->size)) { |
|
281 | + $this->size = filesize($path); |
|
282 | + } |
|
283 | + |
|
284 | + return match (strtolower($unit)) { |
|
285 | + 'kb' => number_format($this->size / 1024, 3), |
|
286 | + 'mb' => number_format(($this->size / 1024) / 1024, 3), |
|
287 | + default => $this->size, |
|
288 | + }; |
|
289 | + } |
|
290 | + |
|
291 | + return null; |
|
292 | + } |
|
293 | 293 | |
294 | - /** |
|
295 | - * Returns the file's group. |
|
296 | - * |
|
297 | - * @param string $path |
|
298 | - * |
|
299 | - * @return int|bool The file group, or false in case of an error |
|
300 | - */ |
|
301 | - public function group($path) |
|
302 | - { |
|
303 | - if ( ! $this->exists($path)) { |
|
304 | - return filegroup($path); |
|
305 | - } |
|
306 | - |
|
307 | - return false; |
|
308 | - } |
|
294 | + /** |
|
295 | + * Returns the file's group. |
|
296 | + * |
|
297 | + * @param string $path |
|
298 | + * |
|
299 | + * @return int|bool The file group, or false in case of an error |
|
300 | + */ |
|
301 | + public function group($path) |
|
302 | + { |
|
303 | + if ( ! $this->exists($path)) { |
|
304 | + return filegroup($path); |
|
305 | + } |
|
306 | + |
|
307 | + return false; |
|
308 | + } |
|
309 | 309 | |
310 | - /** |
|
311 | - * Returns true if the file is executable. |
|
312 | - * |
|
313 | - * @param string $path |
|
314 | - * |
|
315 | - * @return bool True if file is executable, false otherwise |
|
316 | - */ |
|
317 | - public function exec($path): bool |
|
318 | - { |
|
319 | - return is_executable($path); |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Determine if the given path is a directory. |
|
324 | - * |
|
325 | - * @param string $directory |
|
326 | - * |
|
327 | - * @return bool |
|
328 | - */ |
|
329 | - public function isDirectory($directory): bool |
|
330 | - { |
|
331 | - return is_dir($directory); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Determine if the given path is a file. |
|
336 | - * |
|
337 | - * @param string $file |
|
338 | - * |
|
339 | - * @return bool |
|
340 | - */ |
|
341 | - public function isFile($file): bool |
|
342 | - { |
|
343 | - return is_file($file); |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Determine if the given path is writable. |
|
348 | - * |
|
349 | - * @param string $path |
|
350 | - * |
|
351 | - * @return bool |
|
352 | - */ |
|
353 | - public function isWritable($path): bool |
|
354 | - { |
|
355 | - return is_writable($path); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Returns if true the file is readable. |
|
360 | - * |
|
361 | - * @param string $path |
|
362 | - * |
|
363 | - * @return bool True if file is readable, false otherwise |
|
364 | - */ |
|
365 | - public function isReadable($path): bool |
|
366 | - { |
|
367 | - return is_readable($path); |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * Returns last access time. |
|
372 | - * |
|
373 | - * @param string $path |
|
374 | - * |
|
375 | - * @return int|bool Timestamp of last access time, or false in case of an error |
|
376 | - */ |
|
377 | - public function lastAccess($path) |
|
378 | - { |
|
379 | - if ( ! $this->exists($path)) { |
|
380 | - return fileatime($path); |
|
381 | - } |
|
382 | - |
|
383 | - return false; |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * Returns last modified time. |
|
388 | - * |
|
389 | - * @param string $path |
|
390 | - * |
|
391 | - * @return int|bool Timestamp of last modified time, or false in case of an error |
|
392 | - */ |
|
393 | - public function lastModified($path) |
|
394 | - { |
|
395 | - if ( ! $this->exists($path)) { |
|
396 | - return filemtime($path); |
|
397 | - } |
|
398 | - |
|
399 | - return false; |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Get all of the directories within a given directory. |
|
404 | - * |
|
405 | - * @param string $directory |
|
406 | - * |
|
407 | - * @return array |
|
408 | - */ |
|
409 | - public function directories($directory): array |
|
410 | - { |
|
411 | - $directories = []; |
|
412 | - |
|
413 | - $iterators = new FilesystemIterator($directory); |
|
414 | - |
|
415 | - foreach ($iterators as $iterator) { |
|
416 | - $directories[] = trim($iterator->getPathname(), '/').'/'; |
|
417 | - } |
|
418 | - |
|
419 | - return $directories; |
|
420 | - } |
|
421 | - |
|
422 | - /** |
|
423 | - * Delete the file at a given path. |
|
424 | - * |
|
425 | - * @param string $paths |
|
426 | - * |
|
427 | - * @return bool |
|
428 | - */ |
|
429 | - public function delete($paths): bool |
|
430 | - { |
|
431 | - $paths = is_array($paths) ? $paths : func_get_args(); |
|
432 | - |
|
433 | - $success = true; |
|
434 | - |
|
435 | - foreach ($paths as $path) { |
|
436 | - if ( ! @unlink($path)) $success = false; |
|
437 | - } |
|
438 | - |
|
439 | - return $success; |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * Create a directory. |
|
444 | - * |
|
445 | - * @param string $path |
|
446 | - * @param int $mode |
|
447 | - * @param bool $recursive |
|
448 | - * @param bool $force |
|
449 | - * |
|
450 | - * @return bool |
|
451 | - * |
|
452 | - * @throws FileException |
|
453 | - */ |
|
454 | - public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false): bool |
|
455 | - { |
|
456 | - if ($force) { |
|
457 | - return @mkdir($path, $mode, $recursive); |
|
458 | - } |
|
459 | - |
|
460 | - mkdir($path, $mode, $recursive); |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * Copy a directory from one location to another. |
|
465 | - * |
|
466 | - * @param string $directory |
|
467 | - * @param string $destination |
|
468 | - * @param int $options |
|
469 | - * |
|
470 | - * @return bool |
|
471 | - */ |
|
472 | - public function copyDirectory($directory, $destination, $options = null): bool |
|
473 | - { |
|
474 | - if ( ! $this->isDirectory($directory)) return false; |
|
475 | - |
|
476 | - $options = $options ?: FilesystemIterator::SKIP_DOTS; |
|
310 | + /** |
|
311 | + * Returns true if the file is executable. |
|
312 | + * |
|
313 | + * @param string $path |
|
314 | + * |
|
315 | + * @return bool True if file is executable, false otherwise |
|
316 | + */ |
|
317 | + public function exec($path): bool |
|
318 | + { |
|
319 | + return is_executable($path); |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Determine if the given path is a directory. |
|
324 | + * |
|
325 | + * @param string $directory |
|
326 | + * |
|
327 | + * @return bool |
|
328 | + */ |
|
329 | + public function isDirectory($directory): bool |
|
330 | + { |
|
331 | + return is_dir($directory); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Determine if the given path is a file. |
|
336 | + * |
|
337 | + * @param string $file |
|
338 | + * |
|
339 | + * @return bool |
|
340 | + */ |
|
341 | + public function isFile($file): bool |
|
342 | + { |
|
343 | + return is_file($file); |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Determine if the given path is writable. |
|
348 | + * |
|
349 | + * @param string $path |
|
350 | + * |
|
351 | + * @return bool |
|
352 | + */ |
|
353 | + public function isWritable($path): bool |
|
354 | + { |
|
355 | + return is_writable($path); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Returns if true the file is readable. |
|
360 | + * |
|
361 | + * @param string $path |
|
362 | + * |
|
363 | + * @return bool True if file is readable, false otherwise |
|
364 | + */ |
|
365 | + public function isReadable($path): bool |
|
366 | + { |
|
367 | + return is_readable($path); |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * Returns last access time. |
|
372 | + * |
|
373 | + * @param string $path |
|
374 | + * |
|
375 | + * @return int|bool Timestamp of last access time, or false in case of an error |
|
376 | + */ |
|
377 | + public function lastAccess($path) |
|
378 | + { |
|
379 | + if ( ! $this->exists($path)) { |
|
380 | + return fileatime($path); |
|
381 | + } |
|
382 | + |
|
383 | + return false; |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * Returns last modified time. |
|
388 | + * |
|
389 | + * @param string $path |
|
390 | + * |
|
391 | + * @return int|bool Timestamp of last modified time, or false in case of an error |
|
392 | + */ |
|
393 | + public function lastModified($path) |
|
394 | + { |
|
395 | + if ( ! $this->exists($path)) { |
|
396 | + return filemtime($path); |
|
397 | + } |
|
398 | + |
|
399 | + return false; |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Get all of the directories within a given directory. |
|
404 | + * |
|
405 | + * @param string $directory |
|
406 | + * |
|
407 | + * @return array |
|
408 | + */ |
|
409 | + public function directories($directory): array |
|
410 | + { |
|
411 | + $directories = []; |
|
412 | + |
|
413 | + $iterators = new FilesystemIterator($directory); |
|
414 | + |
|
415 | + foreach ($iterators as $iterator) { |
|
416 | + $directories[] = trim($iterator->getPathname(), '/').'/'; |
|
417 | + } |
|
418 | + |
|
419 | + return $directories; |
|
420 | + } |
|
421 | + |
|
422 | + /** |
|
423 | + * Delete the file at a given path. |
|
424 | + * |
|
425 | + * @param string $paths |
|
426 | + * |
|
427 | + * @return bool |
|
428 | + */ |
|
429 | + public function delete($paths): bool |
|
430 | + { |
|
431 | + $paths = is_array($paths) ? $paths : func_get_args(); |
|
432 | + |
|
433 | + $success = true; |
|
434 | + |
|
435 | + foreach ($paths as $path) { |
|
436 | + if ( ! @unlink($path)) $success = false; |
|
437 | + } |
|
438 | + |
|
439 | + return $success; |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * Create a directory. |
|
444 | + * |
|
445 | + * @param string $path |
|
446 | + * @param int $mode |
|
447 | + * @param bool $recursive |
|
448 | + * @param bool $force |
|
449 | + * |
|
450 | + * @return bool |
|
451 | + * |
|
452 | + * @throws FileException |
|
453 | + */ |
|
454 | + public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false): bool |
|
455 | + { |
|
456 | + if ($force) { |
|
457 | + return @mkdir($path, $mode, $recursive); |
|
458 | + } |
|
459 | + |
|
460 | + mkdir($path, $mode, $recursive); |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * Copy a directory from one location to another. |
|
465 | + * |
|
466 | + * @param string $directory |
|
467 | + * @param string $destination |
|
468 | + * @param int $options |
|
469 | + * |
|
470 | + * @return bool |
|
471 | + */ |
|
472 | + public function copyDirectory($directory, $destination, $options = null): bool |
|
473 | + { |
|
474 | + if ( ! $this->isDirectory($directory)) return false; |
|
475 | + |
|
476 | + $options = $options ?: FilesystemIterator::SKIP_DOTS; |
|
477 | 477 | |
478 | - // If the destination directory does not actually exist, we will go ahead and |
|
479 | - // create it recursively, which just gets the destination prepared to copy |
|
480 | - // the files over. Once we make the directory we'll proceed the copying. |
|
481 | - if ( ! $this->isdirectory($destination)) { |
|
482 | - $this->makeDirectory($destination, 0777, true); |
|
483 | - } |
|
478 | + // If the destination directory does not actually exist, we will go ahead and |
|
479 | + // create it recursively, which just gets the destination prepared to copy |
|
480 | + // the files over. Once we make the directory we'll proceed the copying. |
|
481 | + if ( ! $this->isdirectory($destination)) { |
|
482 | + $this->makeDirectory($destination, 0777, true); |
|
483 | + } |
|
484 | 484 | |
485 | - $iterators = new FilesystemIterator($directory, $options); |
|
485 | + $iterators = new FilesystemIterator($directory, $options); |
|
486 | 486 | |
487 | - foreach ($iterators as $iterator) { |
|
488 | - $target = $destination.DIRECTORY_SEPARATOR.$iterator->getBasename(); |
|
487 | + foreach ($iterators as $iterator) { |
|
488 | + $target = $destination.DIRECTORY_SEPARATOR.$iterator->getBasename(); |
|
489 | 489 | |
490 | - // As we spin through items, we will check to see if the current file is actually |
|
491 | - // a directory or a file. When it is actually a directory we will need to call |
|
492 | - // back into this function recursively to keep copying these nested folders. |
|
493 | - if ($iterator->isDir()) { |
|
494 | - if ( ! $this->copyDirectory($iterator->getPathname(), $target, $options)) return false; |
|
495 | - } |
|
496 | - // If the current items is just a regular file, we will just copy this to the new |
|
497 | - // location and keep looping. If for some reason the copy fails we'll bail out |
|
498 | - // and return false, so the developer is aware that the copy process failed. |
|
499 | - else { |
|
500 | - if ( ! $this->copy($iterator->getPathname(), $target)) return false; |
|
501 | - } |
|
502 | - } |
|
503 | - |
|
504 | - return true; |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * Recursively delete a directory and optionally you can keep |
|
509 | - * the directory if you wish. |
|
510 | - * |
|
511 | - * @param string $directory |
|
512 | - * @param bool $keep |
|
513 | - * |
|
514 | - * @return bool |
|
515 | - */ |
|
516 | - public function deleteDirectory($directory, $keep = false): bool |
|
517 | - { |
|
518 | - if ( ! $this->isDirectory($directory)) return false; |
|
519 | - |
|
520 | - $iterators = new filesystemIterator($directory); |
|
521 | - |
|
522 | - foreach ($iterators as $iterator) { |
|
523 | - // If the item is a directory, we can just recurse into the function and delete |
|
524 | - // that sub-directory otherwise we'll just delete the file and keep iterating |
|
525 | - // through each file until the directory is cleaned. |
|
526 | - if ($iterator->isDir() && ! $iterator->isLink()) { |
|
527 | - $this->deleteDirectory($iterator->getPathname()); |
|
528 | - } |
|
529 | - // If the item is just a file, we can go ahead and delete it since we're |
|
530 | - // just looping through and waxing all of the files in this directory |
|
531 | - // and calling directories recursively, so we delete the real path. |
|
532 | - else { |
|
533 | - $this->delete($iterator->getPathname()); |
|
534 | - } |
|
535 | - } |
|
536 | - |
|
537 | - if ( ! $keep) @rmdir($directory); |
|
538 | - |
|
539 | - return true; |
|
540 | - } |
|
541 | - |
|
542 | - /** |
|
543 | - * Empty the specified directory of all files and folders. |
|
544 | - * |
|
545 | - * |
|
546 | - * @param string $directory |
|
547 | - * |
|
548 | - * @return bool |
|
549 | - */ |
|
550 | - public function cleanDirectory($directory): bool |
|
551 | - { |
|
552 | - return $this->deleteDirectory($directory, true); |
|
553 | - } |
|
554 | - |
|
555 | - /** |
|
556 | - * Moves a file to a new location. |
|
557 | - * |
|
558 | - * @param string $from |
|
559 | - * @param string $to |
|
560 | - * @param bool $overwrite |
|
561 | - * |
|
562 | - * @return bool |
|
563 | - */ |
|
564 | - public function moveDirectory($from, $to, $overwrite = false): bool |
|
565 | - { |
|
566 | - if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) return false; |
|
567 | - |
|
568 | - if (false === @rename($from, $to)) { |
|
569 | - $error = error_get_last(); |
|
570 | - |
|
571 | - throw new FileUnableToMoveException($from, $to, strip_tags($error['message'])); |
|
572 | - } |
|
573 | - |
|
574 | - $this->perms($to, 0777 & ~umask()); |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * Attempts to determine the file extension based on the trusted |
|
579 | - * getType() method. If the mime type is unknown, will return null. |
|
580 | - * |
|
581 | - * @param string $path |
|
582 | - * |
|
583 | - * @return string|null |
|
584 | - */ |
|
585 | - public function guessExtension($path) |
|
586 | - { |
|
587 | - return FileMimeType::guessExtensionFromType($this->getMimeType($path)); |
|
588 | - } |
|
589 | - |
|
590 | - /** |
|
591 | - * Retrieve the media type of the file. |
|
592 | - * |
|
593 | - * @param string $path |
|
594 | - * |
|
595 | - * @return string|null |
|
596 | - */ |
|
597 | - public function getMimeType($path) |
|
598 | - { |
|
599 | - $finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
600 | - $mimeType = finfo_file($finfo, $path); |
|
601 | - |
|
602 | - finfo_close($finfo); |
|
603 | - |
|
604 | - return $mimeType; |
|
605 | - } |
|
606 | - |
|
607 | - /** |
|
608 | - * Move a file to a new location. |
|
609 | - * |
|
610 | - * @param string $path |
|
611 | - * @param string $target |
|
612 | - * |
|
613 | - * @return bool |
|
614 | - */ |
|
615 | - public function move($path, $target): bool |
|
616 | - { |
|
617 | - if ($this->exists($path)) { |
|
618 | - return rename($path, $target); |
|
619 | - } |
|
620 | - } |
|
621 | - |
|
622 | - /** |
|
623 | - * Extract the file name from a file path. |
|
624 | - * |
|
625 | - * @param string $path |
|
626 | - * |
|
627 | - * @return string |
|
628 | - */ |
|
629 | - public function name($path) |
|
630 | - { |
|
631 | - return pathinfo($path, PATHINFO_FILENAME); |
|
632 | - } |
|
633 | - |
|
634 | - /** |
|
635 | - * Extract the trailing name component from a file path. |
|
636 | - * |
|
637 | - * @param string $path |
|
638 | - * |
|
639 | - * @return string |
|
640 | - */ |
|
641 | - public function basename($path) |
|
642 | - { |
|
643 | - return pathinfo($path, PATHINFO_BASENAME); |
|
644 | - } |
|
645 | - |
|
646 | - /** |
|
647 | - * Extract the parent directory from a file path. |
|
648 | - * |
|
649 | - * @param string $path |
|
650 | - * |
|
651 | - * @return string |
|
652 | - */ |
|
653 | - public function dirname($path) |
|
654 | - { |
|
655 | - return pathinfo($path, PATHINFO_DIRNAME); |
|
656 | - } |
|
657 | - |
|
658 | - /** |
|
659 | - * Extract the file extension from a file path. |
|
660 | - * |
|
661 | - * @param string $path |
|
662 | - * |
|
663 | - * @return string |
|
664 | - */ |
|
665 | - public function extension($path) |
|
666 | - { |
|
667 | - return pathinfo($path, PATHINFO_EXTENSION); |
|
668 | - } |
|
669 | - |
|
670 | - /** |
|
671 | - * Find path names matching a given pattern. |
|
672 | - * |
|
673 | - * @param string $pattern |
|
674 | - * @param int $flags (0 by default) |
|
675 | - * |
|
676 | - * @return array |
|
677 | - */ |
|
678 | - public function glob($pattern, $flags = 0): bool |
|
679 | - { |
|
680 | - return glob($pattern, $flags); |
|
681 | - } |
|
682 | - |
|
683 | - /** |
|
684 | - * Returns the file's owner. |
|
685 | - * |
|
686 | - * @param string $path |
|
687 | - * |
|
688 | - * @return int|bool The file owner, or false in case of an error |
|
689 | - */ |
|
690 | - public function owner($path) |
|
691 | - { |
|
692 | - if ($this->exists($path)) { |
|
693 | - return fileowner($path); |
|
694 | - } |
|
695 | - |
|
696 | - return false; |
|
697 | - } |
|
698 | - |
|
699 | - /** |
|
700 | - * Returns the "chmod" (permissions) of the file. |
|
701 | - * |
|
702 | - * @param string $path |
|
703 | - * @param int|null $mode |
|
704 | - * |
|
705 | - * @return mixed Permissions for the file, or false in case of an error |
|
706 | - */ |
|
707 | - public function perms($path, $mode = null) |
|
708 | - { |
|
709 | - if ($mode) { |
|
710 | - chmod($path, $mode); |
|
711 | - } |
|
712 | - |
|
713 | - return substr(sprintf('%o', fileperms($path)), -4); |
|
714 | - } |
|
715 | - |
|
716 | - /** |
|
717 | - * Prepend to a file. |
|
718 | - * |
|
719 | - * @param string $path |
|
720 | - * @param string $data |
|
721 | - * |
|
722 | - * @return int |
|
723 | - */ |
|
724 | - public function prepend($path, $data): int |
|
725 | - { |
|
726 | - if ($this->exists($path)) { |
|
727 | - $this->put($path, $data.$this->get($path)); |
|
728 | - } |
|
729 | - |
|
730 | - return $this->put($path, $data); |
|
731 | - } |
|
732 | - |
|
733 | - /** |
|
734 | - * Write the content of a file. |
|
735 | - * |
|
736 | - * @param string $path |
|
737 | - * @param string $contents |
|
738 | - * @param bool $lock |
|
739 | - * |
|
740 | - * @return int|bool |
|
741 | - */ |
|
742 | - public function put($path, $contents, $lock = false): int|bool |
|
743 | - { |
|
744 | - return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
|
745 | - } |
|
746 | - |
|
747 | - /** |
|
748 | - * Get the file type of a given file. |
|
749 | - * |
|
750 | - * @param string $path |
|
751 | - * |
|
752 | - * @return string |
|
753 | - */ |
|
754 | - public function type($path): string |
|
755 | - { |
|
756 | - return filetype($path); |
|
757 | - } |
|
490 | + // As we spin through items, we will check to see if the current file is actually |
|
491 | + // a directory or a file. When it is actually a directory we will need to call |
|
492 | + // back into this function recursively to keep copying these nested folders. |
|
493 | + if ($iterator->isDir()) { |
|
494 | + if ( ! $this->copyDirectory($iterator->getPathname(), $target, $options)) return false; |
|
495 | + } |
|
496 | + // If the current items is just a regular file, we will just copy this to the new |
|
497 | + // location and keep looping. If for some reason the copy fails we'll bail out |
|
498 | + // and return false, so the developer is aware that the copy process failed. |
|
499 | + else { |
|
500 | + if ( ! $this->copy($iterator->getPathname(), $target)) return false; |
|
501 | + } |
|
502 | + } |
|
503 | + |
|
504 | + return true; |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * Recursively delete a directory and optionally you can keep |
|
509 | + * the directory if you wish. |
|
510 | + * |
|
511 | + * @param string $directory |
|
512 | + * @param bool $keep |
|
513 | + * |
|
514 | + * @return bool |
|
515 | + */ |
|
516 | + public function deleteDirectory($directory, $keep = false): bool |
|
517 | + { |
|
518 | + if ( ! $this->isDirectory($directory)) return false; |
|
519 | + |
|
520 | + $iterators = new filesystemIterator($directory); |
|
521 | + |
|
522 | + foreach ($iterators as $iterator) { |
|
523 | + // If the item is a directory, we can just recurse into the function and delete |
|
524 | + // that sub-directory otherwise we'll just delete the file and keep iterating |
|
525 | + // through each file until the directory is cleaned. |
|
526 | + if ($iterator->isDir() && ! $iterator->isLink()) { |
|
527 | + $this->deleteDirectory($iterator->getPathname()); |
|
528 | + } |
|
529 | + // If the item is just a file, we can go ahead and delete it since we're |
|
530 | + // just looping through and waxing all of the files in this directory |
|
531 | + // and calling directories recursively, so we delete the real path. |
|
532 | + else { |
|
533 | + $this->delete($iterator->getPathname()); |
|
534 | + } |
|
535 | + } |
|
536 | + |
|
537 | + if ( ! $keep) @rmdir($directory); |
|
538 | + |
|
539 | + return true; |
|
540 | + } |
|
541 | + |
|
542 | + /** |
|
543 | + * Empty the specified directory of all files and folders. |
|
544 | + * |
|
545 | + * |
|
546 | + * @param string $directory |
|
547 | + * |
|
548 | + * @return bool |
|
549 | + */ |
|
550 | + public function cleanDirectory($directory): bool |
|
551 | + { |
|
552 | + return $this->deleteDirectory($directory, true); |
|
553 | + } |
|
554 | + |
|
555 | + /** |
|
556 | + * Moves a file to a new location. |
|
557 | + * |
|
558 | + * @param string $from |
|
559 | + * @param string $to |
|
560 | + * @param bool $overwrite |
|
561 | + * |
|
562 | + * @return bool |
|
563 | + */ |
|
564 | + public function moveDirectory($from, $to, $overwrite = false): bool |
|
565 | + { |
|
566 | + if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) return false; |
|
567 | + |
|
568 | + if (false === @rename($from, $to)) { |
|
569 | + $error = error_get_last(); |
|
570 | + |
|
571 | + throw new FileUnableToMoveException($from, $to, strip_tags($error['message'])); |
|
572 | + } |
|
573 | + |
|
574 | + $this->perms($to, 0777 & ~umask()); |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * Attempts to determine the file extension based on the trusted |
|
579 | + * getType() method. If the mime type is unknown, will return null. |
|
580 | + * |
|
581 | + * @param string $path |
|
582 | + * |
|
583 | + * @return string|null |
|
584 | + */ |
|
585 | + public function guessExtension($path) |
|
586 | + { |
|
587 | + return FileMimeType::guessExtensionFromType($this->getMimeType($path)); |
|
588 | + } |
|
589 | + |
|
590 | + /** |
|
591 | + * Retrieve the media type of the file. |
|
592 | + * |
|
593 | + * @param string $path |
|
594 | + * |
|
595 | + * @return string|null |
|
596 | + */ |
|
597 | + public function getMimeType($path) |
|
598 | + { |
|
599 | + $finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
600 | + $mimeType = finfo_file($finfo, $path); |
|
601 | + |
|
602 | + finfo_close($finfo); |
|
603 | + |
|
604 | + return $mimeType; |
|
605 | + } |
|
606 | + |
|
607 | + /** |
|
608 | + * Move a file to a new location. |
|
609 | + * |
|
610 | + * @param string $path |
|
611 | + * @param string $target |
|
612 | + * |
|
613 | + * @return bool |
|
614 | + */ |
|
615 | + public function move($path, $target): bool |
|
616 | + { |
|
617 | + if ($this->exists($path)) { |
|
618 | + return rename($path, $target); |
|
619 | + } |
|
620 | + } |
|
621 | + |
|
622 | + /** |
|
623 | + * Extract the file name from a file path. |
|
624 | + * |
|
625 | + * @param string $path |
|
626 | + * |
|
627 | + * @return string |
|
628 | + */ |
|
629 | + public function name($path) |
|
630 | + { |
|
631 | + return pathinfo($path, PATHINFO_FILENAME); |
|
632 | + } |
|
633 | + |
|
634 | + /** |
|
635 | + * Extract the trailing name component from a file path. |
|
636 | + * |
|
637 | + * @param string $path |
|
638 | + * |
|
639 | + * @return string |
|
640 | + */ |
|
641 | + public function basename($path) |
|
642 | + { |
|
643 | + return pathinfo($path, PATHINFO_BASENAME); |
|
644 | + } |
|
645 | + |
|
646 | + /** |
|
647 | + * Extract the parent directory from a file path. |
|
648 | + * |
|
649 | + * @param string $path |
|
650 | + * |
|
651 | + * @return string |
|
652 | + */ |
|
653 | + public function dirname($path) |
|
654 | + { |
|
655 | + return pathinfo($path, PATHINFO_DIRNAME); |
|
656 | + } |
|
657 | + |
|
658 | + /** |
|
659 | + * Extract the file extension from a file path. |
|
660 | + * |
|
661 | + * @param string $path |
|
662 | + * |
|
663 | + * @return string |
|
664 | + */ |
|
665 | + public function extension($path) |
|
666 | + { |
|
667 | + return pathinfo($path, PATHINFO_EXTENSION); |
|
668 | + } |
|
669 | + |
|
670 | + /** |
|
671 | + * Find path names matching a given pattern. |
|
672 | + * |
|
673 | + * @param string $pattern |
|
674 | + * @param int $flags (0 by default) |
|
675 | + * |
|
676 | + * @return array |
|
677 | + */ |
|
678 | + public function glob($pattern, $flags = 0): bool |
|
679 | + { |
|
680 | + return glob($pattern, $flags); |
|
681 | + } |
|
682 | + |
|
683 | + /** |
|
684 | + * Returns the file's owner. |
|
685 | + * |
|
686 | + * @param string $path |
|
687 | + * |
|
688 | + * @return int|bool The file owner, or false in case of an error |
|
689 | + */ |
|
690 | + public function owner($path) |
|
691 | + { |
|
692 | + if ($this->exists($path)) { |
|
693 | + return fileowner($path); |
|
694 | + } |
|
695 | + |
|
696 | + return false; |
|
697 | + } |
|
698 | + |
|
699 | + /** |
|
700 | + * Returns the "chmod" (permissions) of the file. |
|
701 | + * |
|
702 | + * @param string $path |
|
703 | + * @param int|null $mode |
|
704 | + * |
|
705 | + * @return mixed Permissions for the file, or false in case of an error |
|
706 | + */ |
|
707 | + public function perms($path, $mode = null) |
|
708 | + { |
|
709 | + if ($mode) { |
|
710 | + chmod($path, $mode); |
|
711 | + } |
|
712 | + |
|
713 | + return substr(sprintf('%o', fileperms($path)), -4); |
|
714 | + } |
|
715 | + |
|
716 | + /** |
|
717 | + * Prepend to a file. |
|
718 | + * |
|
719 | + * @param string $path |
|
720 | + * @param string $data |
|
721 | + * |
|
722 | + * @return int |
|
723 | + */ |
|
724 | + public function prepend($path, $data): int |
|
725 | + { |
|
726 | + if ($this->exists($path)) { |
|
727 | + $this->put($path, $data.$this->get($path)); |
|
728 | + } |
|
729 | + |
|
730 | + return $this->put($path, $data); |
|
731 | + } |
|
732 | + |
|
733 | + /** |
|
734 | + * Write the content of a file. |
|
735 | + * |
|
736 | + * @param string $path |
|
737 | + * @param string $contents |
|
738 | + * @param bool $lock |
|
739 | + * |
|
740 | + * @return int|bool |
|
741 | + */ |
|
742 | + public function put($path, $contents, $lock = false): int|bool |
|
743 | + { |
|
744 | + return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
|
745 | + } |
|
746 | + |
|
747 | + /** |
|
748 | + * Get the file type of a given file. |
|
749 | + * |
|
750 | + * @param string $path |
|
751 | + * |
|
752 | + * @return string |
|
753 | + */ |
|
754 | + public function type($path): string |
|
755 | + { |
|
756 | + return filetype($path); |
|
757 | + } |
|
758 | 758 | |
759 | - /** |
|
760 | - * Write the contents of a file, replacing it atomically if it already exists. |
|
761 | - * |
|
762 | - * @param string $path |
|
763 | - * @param string $content |
|
764 | - * |
|
765 | - * @return void |
|
766 | - */ |
|
767 | - public function replace($path, $content): void |
|
768 | - { |
|
769 | - $this->clearstatcache($path); |
|
759 | + /** |
|
760 | + * Write the contents of a file, replacing it atomically if it already exists. |
|
761 | + * |
|
762 | + * @param string $path |
|
763 | + * @param string $content |
|
764 | + * |
|
765 | + * @return void |
|
766 | + */ |
|
767 | + public function replace($path, $content): void |
|
768 | + { |
|
769 | + $this->clearstatcache($path); |
|
770 | 770 | |
771 | - $path = realpath($path) ?: $path; |
|
771 | + $path = realpath($path) ?: $path; |
|
772 | 772 | |
773 | - $tempPath = tempnam(dirname($path), basename($path)); |
|
773 | + $tempPath = tempnam(dirname($path), basename($path)); |
|
774 | 774 | |
775 | - $this->perms($tempPath, 0777 - umask()); |
|
775 | + $this->perms($tempPath, 0777 - umask()); |
|
776 | 776 | |
777 | - $this->put($tempPath, $content); |
|
777 | + $this->put($tempPath, $content); |
|
778 | 778 | |
779 | - $this->move($tempPath, $path); |
|
780 | - } |
|
781 | - |
|
782 | - /** |
|
783 | - * Searches for a given text and replaces the text if found. |
|
784 | - * |
|
785 | - * @param string $path |
|
786 | - * @param string $search |
|
787 | - * @param string $replace |
|
788 | - * |
|
789 | - * @return bool |
|
790 | - */ |
|
791 | - public function replaceText($path, $search, $replace): bool |
|
792 | - { |
|
793 | - if ( ! $this->open($path, 'r+')) { |
|
794 | - return false; |
|
795 | - } |
|
796 | - |
|
797 | - if ($this->lock !== null) { |
|
798 | - if (flock($this->handler, LOCK_EX) === false) |
|
799 | - { |
|
800 | - return false; |
|
801 | - } |
|
802 | - } |
|
803 | - |
|
804 | - $replaced = $this->write($path, str_replace($search, $replace, $this->get($path)), true); |
|
805 | - |
|
806 | - if ($this->lock !== null) { |
|
807 | - flock($this->handler, LOCK_UN); |
|
808 | - } |
|
809 | - |
|
810 | - $this->close(); |
|
811 | - |
|
812 | - return $replaced; |
|
813 | - } |
|
814 | - |
|
815 | - /** |
|
816 | - * Closes the current file if it is opened. |
|
817 | - * |
|
818 | - * @return bool |
|
819 | - */ |
|
820 | - public function close(): bool |
|
821 | - { |
|
822 | - if ( ! is_resource($this->handler)) { |
|
823 | - return true; |
|
824 | - } |
|
825 | - |
|
826 | - return fclose($this->handler); |
|
827 | - } |
|
828 | - |
|
829 | - /** |
|
830 | - * Write given data to this file. |
|
831 | - * |
|
832 | - * @param string $path |
|
833 | - * @param string $data Data to write to this File |
|
834 | - * @param bool $force The file to open |
|
835 | - * |
|
836 | - * @return bool |
|
837 | - */ |
|
838 | - public function write($path, $data, $force = false): bool |
|
839 | - { |
|
840 | - $success = false; |
|
841 | - |
|
842 | - if ($this->open($path, 'w', $force) === true) { |
|
843 | - if ($this->lock !== null) { |
|
844 | - if (flock($this->handler, LOCK_EX) === false) { |
|
845 | - return false; |
|
846 | - } |
|
847 | - } |
|
848 | - |
|
849 | - if (fwrite($this->handler, $data) !== false) { |
|
850 | - $success = true; |
|
851 | - } |
|
852 | - |
|
853 | - if ($this->lock !== null) { |
|
854 | - flock($this->handler, LOCK_UN); |
|
855 | - } |
|
856 | - } |
|
857 | - |
|
858 | - return $success; |
|
859 | - } |
|
779 | + $this->move($tempPath, $path); |
|
780 | + } |
|
781 | + |
|
782 | + /** |
|
783 | + * Searches for a given text and replaces the text if found. |
|
784 | + * |
|
785 | + * @param string $path |
|
786 | + * @param string $search |
|
787 | + * @param string $replace |
|
788 | + * |
|
789 | + * @return bool |
|
790 | + */ |
|
791 | + public function replaceText($path, $search, $replace): bool |
|
792 | + { |
|
793 | + if ( ! $this->open($path, 'r+')) { |
|
794 | + return false; |
|
795 | + } |
|
796 | + |
|
797 | + if ($this->lock !== null) { |
|
798 | + if (flock($this->handler, LOCK_EX) === false) |
|
799 | + { |
|
800 | + return false; |
|
801 | + } |
|
802 | + } |
|
803 | + |
|
804 | + $replaced = $this->write($path, str_replace($search, $replace, $this->get($path)), true); |
|
805 | + |
|
806 | + if ($this->lock !== null) { |
|
807 | + flock($this->handler, LOCK_UN); |
|
808 | + } |
|
809 | + |
|
810 | + $this->close(); |
|
811 | + |
|
812 | + return $replaced; |
|
813 | + } |
|
814 | + |
|
815 | + /** |
|
816 | + * Closes the current file if it is opened. |
|
817 | + * |
|
818 | + * @return bool |
|
819 | + */ |
|
820 | + public function close(): bool |
|
821 | + { |
|
822 | + if ( ! is_resource($this->handler)) { |
|
823 | + return true; |
|
824 | + } |
|
825 | + |
|
826 | + return fclose($this->handler); |
|
827 | + } |
|
828 | + |
|
829 | + /** |
|
830 | + * Write given data to this file. |
|
831 | + * |
|
832 | + * @param string $path |
|
833 | + * @param string $data Data to write to this File |
|
834 | + * @param bool $force The file to open |
|
835 | + * |
|
836 | + * @return bool |
|
837 | + */ |
|
838 | + public function write($path, $data, $force = false): bool |
|
839 | + { |
|
840 | + $success = false; |
|
841 | + |
|
842 | + if ($this->open($path, 'w', $force) === true) { |
|
843 | + if ($this->lock !== null) { |
|
844 | + if (flock($this->handler, LOCK_EX) === false) { |
|
845 | + return false; |
|
846 | + } |
|
847 | + } |
|
848 | + |
|
849 | + if (fwrite($this->handler, $data) !== false) { |
|
850 | + $success = true; |
|
851 | + } |
|
852 | + |
|
853 | + if ($this->lock !== null) { |
|
854 | + flock($this->handler, LOCK_UN); |
|
855 | + } |
|
856 | + } |
|
857 | + |
|
858 | + return $success; |
|
859 | + } |
|
860 | 860 | } |
861 | 861 | \ No newline at end of file |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | $__path = $path; |
227 | 227 | $__data = $data; |
228 | 228 | |
229 | - return (static function () use ($__path, $__data) { |
|
229 | + return (static function() use ($__path, $__data) { |
|
230 | 230 | extract($__data, EXTR_SKIP); |
231 | 231 | |
232 | 232 | return require $__path; |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | $__path = $path; |
253 | 253 | $__data = $data; |
254 | 254 | |
255 | - return (static function () use ($__path, $__data) { |
|
255 | + return (static function() use ($__path, $__data) { |
|
256 | 256 | extract($__data, EXTR_SKIP); |
257 | 257 | |
258 | 258 | return require_once $__path; |
@@ -739,7 +739,7 @@ discard block |
||
739 | 739 | * |
740 | 740 | * @return int|bool |
741 | 741 | */ |
742 | - public function put($path, $contents, $lock = false): int|bool |
|
742 | + public function put($path, $contents, $lock = false): int | bool |
|
743 | 743 | { |
744 | 744 | return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
745 | 745 | } |
@@ -433,7 +433,9 @@ discard block |
||
433 | 433 | $success = true; |
434 | 434 | |
435 | 435 | foreach ($paths as $path) { |
436 | - if ( ! @unlink($path)) $success = false; |
|
436 | + if ( ! @unlink($path)) { |
|
437 | + $success = false; |
|
438 | + } |
|
437 | 439 | } |
438 | 440 | |
439 | 441 | return $success; |
@@ -471,7 +473,9 @@ discard block |
||
471 | 473 | */ |
472 | 474 | public function copyDirectory($directory, $destination, $options = null): bool |
473 | 475 | { |
474 | - if ( ! $this->isDirectory($directory)) return false; |
|
476 | + if ( ! $this->isDirectory($directory)) { |
|
477 | + return false; |
|
478 | + } |
|
475 | 479 | |
476 | 480 | $options = $options ?: FilesystemIterator::SKIP_DOTS; |
477 | 481 | |
@@ -491,13 +495,17 @@ discard block |
||
491 | 495 | // a directory or a file. When it is actually a directory we will need to call |
492 | 496 | // back into this function recursively to keep copying these nested folders. |
493 | 497 | if ($iterator->isDir()) { |
494 | - if ( ! $this->copyDirectory($iterator->getPathname(), $target, $options)) return false; |
|
498 | + if ( ! $this->copyDirectory($iterator->getPathname(), $target, $options)) { |
|
499 | + return false; |
|
500 | + } |
|
495 | 501 | } |
496 | 502 | // If the current items is just a regular file, we will just copy this to the new |
497 | 503 | // location and keep looping. If for some reason the copy fails we'll bail out |
498 | 504 | // and return false, so the developer is aware that the copy process failed. |
499 | 505 | else { |
500 | - if ( ! $this->copy($iterator->getPathname(), $target)) return false; |
|
506 | + if ( ! $this->copy($iterator->getPathname(), $target)) { |
|
507 | + return false; |
|
508 | + } |
|
501 | 509 | } |
502 | 510 | } |
503 | 511 | |
@@ -515,7 +523,9 @@ discard block |
||
515 | 523 | */ |
516 | 524 | public function deleteDirectory($directory, $keep = false): bool |
517 | 525 | { |
518 | - if ( ! $this->isDirectory($directory)) return false; |
|
526 | + if ( ! $this->isDirectory($directory)) { |
|
527 | + return false; |
|
528 | + } |
|
519 | 529 | |
520 | 530 | $iterators = new filesystemIterator($directory); |
521 | 531 | |
@@ -534,7 +544,9 @@ discard block |
||
534 | 544 | } |
535 | 545 | } |
536 | 546 | |
537 | - if ( ! $keep) @rmdir($directory); |
|
547 | + if ( ! $keep) { |
|
548 | + @rmdir($directory); |
|
549 | + } |
|
538 | 550 | |
539 | 551 | return true; |
540 | 552 | } |
@@ -563,7 +575,9 @@ discard block |
||
563 | 575 | */ |
564 | 576 | public function moveDirectory($from, $to, $overwrite = false): bool |
565 | 577 | { |
566 | - if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) return false; |
|
578 | + if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) { |
|
579 | + return false; |
|
580 | + } |
|
567 | 581 | |
568 | 582 | if (false === @rename($from, $to)) { |
569 | 583 | $error = error_get_last(); |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | public function storagePath($path = ''): string |
435 | 435 | { |
436 | 436 | return ($this->storagePath ?: $this->basePath.DIRECTORY_SEPARATOR.'storage'). |
437 | - ($path != '' ? DIRECTORY_SEPARATOR.$path : ''); |
|
437 | + ($path != '' ? DIRECTORY_SEPARATOR.$path : ''); |
|
438 | 438 | } |
439 | 439 | |
440 | 440 | /** |
@@ -1177,16 +1177,16 @@ discard block |
||
1177 | 1177 | } |
1178 | 1178 | |
1179 | 1179 | /** |
1180 | - * Shutdown the application. |
|
1181 | - * |
|
1182 | - * @return void |
|
1183 | - */ |
|
1184 | - public function shutdown(): void |
|
1185 | - { |
|
1186 | - foreach ($this->shutdownCallbacks as $shutdown) { |
|
1180 | + * Shutdown the application. |
|
1181 | + * |
|
1182 | + * @return void |
|
1183 | + */ |
|
1184 | + public function shutdown(): void |
|
1185 | + { |
|
1186 | + foreach ($this->shutdownCallbacks as $shutdown) { |
|
1187 | 1187 | $this->call($shutdown); |
1188 | 1188 | } |
1189 | - } |
|
1189 | + } |
|
1190 | 1190 | |
1191 | 1191 | /** |
1192 | 1192 | * Register the core class aliases in the container. |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | $this->instance('path.resources', $this->resourcePath()); |
272 | 272 | $this->instance('path.bootstrap', $this->bootstrapPath()); |
273 | 273 | |
274 | - $this->setLangPath(value(function () { |
|
274 | + $this->setLangPath(value(function() { |
|
275 | 275 | if (is_dir($directory = $this->resourcePath('lang'))) { |
276 | 276 | return $directory; |
277 | 277 | } |
@@ -786,7 +786,7 @@ discard block |
||
786 | 786 | { |
787 | 787 | $name = is_string($provider) ? $provider : getClass($provider, true); |
788 | 788 | |
789 | - return Arr::where($this->serviceProviders, function ($value) use ($name) { |
|
789 | + return Arr::where($this->serviceProviders, function($value) use ($name) { |
|
790 | 790 | return $value instanceof $name; |
791 | 791 | }); |
792 | 792 | } |
@@ -33,42 +33,42 @@ |
||
33 | 33 | */ |
34 | 34 | class ServiceUnavailableHttpException extends HttpException |
35 | 35 | { |
36 | - /** |
|
37 | - * Get the HTTP status code. |
|
38 | - * |
|
39 | - * @var int $code |
|
40 | - */ |
|
41 | - protected $code = 503; |
|
36 | + /** |
|
37 | + * Get the HTTP status code. |
|
38 | + * |
|
39 | + * @var int $code |
|
40 | + */ |
|
41 | + protected $code = 503; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Initialize constructor. |
|
45 | - * |
|
46 | - * @param int|string|null $retryAfter The number of seconds or HTTP-date after |
|
47 | - * which the request may be retried |
|
48 | - * @param string|null $message |
|
49 | - * @param \Throwable|null $previous |
|
50 | - * @param int $code |
|
51 | - * @param array $headers |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function __construct( |
|
56 | - $retryAfter = null, |
|
57 | - string $message = null, |
|
58 | - Throwable $previous = null, |
|
59 | - ?int $code = 0, |
|
60 | - array $headers = [] |
|
61 | - ) { |
|
62 | - if ($retryAfter) { |
|
63 | - $headers['Retry-After'] = $retryAfter; |
|
64 | - } |
|
43 | + /** |
|
44 | + * Initialize constructor. |
|
45 | + * |
|
46 | + * @param int|string|null $retryAfter The number of seconds or HTTP-date after |
|
47 | + * which the request may be retried |
|
48 | + * @param string|null $message |
|
49 | + * @param \Throwable|null $previous |
|
50 | + * @param int $code |
|
51 | + * @param array $headers |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function __construct( |
|
56 | + $retryAfter = null, |
|
57 | + string $message = null, |
|
58 | + Throwable $previous = null, |
|
59 | + ?int $code = 0, |
|
60 | + array $headers = [] |
|
61 | + ) { |
|
62 | + if ($retryAfter) { |
|
63 | + $headers['Retry-After'] = $retryAfter; |
|
64 | + } |
|
65 | 65 | |
66 | - parent::__construct( |
|
67 | - $this->code, |
|
68 | - $message, |
|
69 | - $previous, |
|
70 | - $headers, |
|
71 | - $code |
|
72 | - ); |
|
73 | - } |
|
66 | + parent::__construct( |
|
67 | + $this->code, |
|
68 | + $message, |
|
69 | + $previous, |
|
70 | + $headers, |
|
71 | + $code |
|
72 | + ); |
|
73 | + } |
|
74 | 74 | } |
75 | 75 | \ No newline at end of file |