@@ -39,7 +39,7 @@ |
||
39 | 39 | */ |
40 | 40 | public function register() |
41 | 41 | { |
42 | - $this->app->singleton('events', function ($app) { |
|
42 | + $this->app->singleton('events', function($app) { |
|
43 | 43 | return new Dispatcher($app); |
44 | 44 | }); |
45 | 45 | } |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | $__path = $path; |
244 | 244 | $__data = $data; |
245 | 245 | |
246 | - return (static function () use ($__path, $__data) { |
|
246 | + return (static function() use ($__path, $__data) { |
|
247 | 247 | extract($__data, EXTR_SKIP); |
248 | 248 | |
249 | 249 | return require $__path; |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | $__path = $path; |
270 | 270 | $__data = $data; |
271 | 271 | |
272 | - return (static function () use ($__path, $__data) { |
|
272 | + return (static function() use ($__path, $__data) { |
|
273 | 273 | extract($__data, EXTR_SKIP); |
274 | 274 | |
275 | 275 | return require_once $__path; |
@@ -35,842 +35,842 @@ |
||
35 | 35 | */ |
36 | 36 | class Filesystem |
37 | 37 | { |
38 | - /** |
|
39 | - * Enable locking for file reading and writing. |
|
40 | - * |
|
41 | - * @var null|bool $lock |
|
42 | - */ |
|
43 | - public $lock = null; |
|
44 | - |
|
45 | - /** |
|
46 | - * Holds the file handler resource if the file is opened. |
|
47 | - * |
|
48 | - * @var resource $handler |
|
49 | - */ |
|
50 | - protected $handler; |
|
51 | - |
|
52 | - /** |
|
53 | - * The files size in bytes. |
|
54 | - * |
|
55 | - * @var float $size |
|
56 | - */ |
|
57 | - protected $size; |
|
58 | - |
|
59 | - /** |
|
60 | - * Append given data string to this file. |
|
61 | - * |
|
62 | - * @param string $path |
|
63 | - * @param string $data |
|
64 | - * |
|
65 | - * @return bool |
|
66 | - */ |
|
67 | - public function append($path, $data) |
|
68 | - { |
|
69 | - return file_put_contents($path, $data, FILE_APPEND); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Copy a file to a new location. |
|
74 | - * |
|
75 | - * @param string $path |
|
76 | - * @param string $target |
|
77 | - * |
|
78 | - * @return bool |
|
79 | - */ |
|
80 | - public function copy($path, $target): bool |
|
81 | - { |
|
82 | - return copy($path, $target); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Get the contents of a file. |
|
87 | - * |
|
88 | - * @param string $path |
|
89 | - * @param bool $lock |
|
90 | - * @param bool $force |
|
91 | - * |
|
92 | - * @return string |
|
93 | - * |
|
94 | - * @throws FileNotFoundException |
|
95 | - */ |
|
96 | - public function get($path, $lock = false, $force = false): string |
|
97 | - { |
|
98 | - if ($this->isFile($path)) { |
|
99 | - return $lock ? $this->read($path, $force) : file_get_contents($path); |
|
100 | - } |
|
101 | - |
|
102 | - throw new FileNotFoundException($path); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Get contents of a file with shared access. |
|
107 | - * |
|
108 | - * @param string $path |
|
109 | - * @param bool $force |
|
110 | - * |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - protected function read($path, $force = false): string |
|
114 | - { |
|
115 | - $contents = ''; |
|
116 | - |
|
117 | - $this->open($path, 'rb', $force); |
|
38 | + /** |
|
39 | + * Enable locking for file reading and writing. |
|
40 | + * |
|
41 | + * @var null|bool $lock |
|
42 | + */ |
|
43 | + public $lock = null; |
|
44 | + |
|
45 | + /** |
|
46 | + * Holds the file handler resource if the file is opened. |
|
47 | + * |
|
48 | + * @var resource $handler |
|
49 | + */ |
|
50 | + protected $handler; |
|
51 | + |
|
52 | + /** |
|
53 | + * The files size in bytes. |
|
54 | + * |
|
55 | + * @var float $size |
|
56 | + */ |
|
57 | + protected $size; |
|
58 | + |
|
59 | + /** |
|
60 | + * Append given data string to this file. |
|
61 | + * |
|
62 | + * @param string $path |
|
63 | + * @param string $data |
|
64 | + * |
|
65 | + * @return bool |
|
66 | + */ |
|
67 | + public function append($path, $data) |
|
68 | + { |
|
69 | + return file_put_contents($path, $data, FILE_APPEND); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Copy a file to a new location. |
|
74 | + * |
|
75 | + * @param string $path |
|
76 | + * @param string $target |
|
77 | + * |
|
78 | + * @return bool |
|
79 | + */ |
|
80 | + public function copy($path, $target): bool |
|
81 | + { |
|
82 | + return copy($path, $target); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Get the contents of a file. |
|
87 | + * |
|
88 | + * @param string $path |
|
89 | + * @param bool $lock |
|
90 | + * @param bool $force |
|
91 | + * |
|
92 | + * @return string |
|
93 | + * |
|
94 | + * @throws FileNotFoundException |
|
95 | + */ |
|
96 | + public function get($path, $lock = false, $force = false): string |
|
97 | + { |
|
98 | + if ($this->isFile($path)) { |
|
99 | + return $lock ? $this->read($path, $force) : file_get_contents($path); |
|
100 | + } |
|
101 | + |
|
102 | + throw new FileNotFoundException($path); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Get contents of a file with shared access. |
|
107 | + * |
|
108 | + * @param string $path |
|
109 | + * @param bool $force |
|
110 | + * |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + protected function read($path, $force = false): string |
|
114 | + { |
|
115 | + $contents = ''; |
|
116 | + |
|
117 | + $this->open($path, 'rb', $force); |
|
118 | 118 | |
119 | - if ($this->handler) { |
|
120 | - try { |
|
121 | - if (flock($this->handler, LOCK_SH)) { |
|
122 | - $this->clearStatCache($path); |
|
119 | + if ($this->handler) { |
|
120 | + try { |
|
121 | + if (flock($this->handler, LOCK_SH)) { |
|
122 | + $this->clearStatCache($path); |
|
123 | 123 | |
124 | - $contents = fread($this->handler, $this->getSize($path) ?: 1); |
|
124 | + $contents = fread($this->handler, $this->getSize($path) ?: 1); |
|
125 | 125 | |
126 | - while ( ! feof($this->handler)) { |
|
127 | - $contents .= fgets($this->handler, 4096); |
|
128 | - } |
|
129 | - |
|
130 | - flock($this->handler, LOCK_UN); |
|
131 | - } |
|
132 | - } finally { |
|
133 | - $this->close(); |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - return trim($contents); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Opens the current file with a given $mode. |
|
142 | - * |
|
143 | - * @param string $path |
|
144 | - * @param string $mode A valid 'fopen' mode string (r|w|a ...) |
|
145 | - * @param bool $force |
|
146 | - * |
|
147 | - * @return bool |
|
148 | - */ |
|
149 | - public function open($path, $mode, $force = false): bool |
|
150 | - { |
|
151 | - if ( ! $force && is_resource($this->handler)) { |
|
152 | - return true; |
|
153 | - } |
|
154 | - |
|
155 | - if ($this->exists($path) === false) { |
|
156 | - if ($this->create($path) === false) { |
|
157 | - return false; |
|
158 | - } |
|
159 | - } |
|
160 | - |
|
161 | - $this->handler = fopen($path, $mode); |
|
162 | - |
|
163 | - return is_resource($this->handler); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Creates the file. |
|
168 | - * |
|
169 | - * @param string $path |
|
170 | - * |
|
171 | - * @return bool |
|
172 | - */ |
|
173 | - public function create($path): bool |
|
174 | - { |
|
175 | - if (($this->isDirectory($path)) && ($this->isWritable($path)) || ( ! $this->exists($path))) { |
|
176 | - if (touch($path)) { |
|
177 | - return true; |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - return false; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Determine if a file exists. |
|
186 | - * |
|
187 | - * @param string $path |
|
188 | - * |
|
189 | - * @return bool |
|
190 | - */ |
|
191 | - public function exists($path): bool |
|
192 | - { |
|
193 | - $this->clearStatCache($path); |
|
194 | - |
|
195 | - return file_exists($path); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Clear PHP's internal stat cache. |
|
200 | - * |
|
201 | - * @param string $path |
|
202 | - * @param bool $all Clear all cache or not |
|
203 | - * |
|
204 | - * @return void |
|
205 | - */ |
|
206 | - public function clearStatCache($path, $all = false): void |
|
207 | - { |
|
208 | - if ($all === false) { |
|
209 | - clearstatcache(true, $path); |
|
210 | - } |
|
211 | - |
|
212 | - clearstatcache(); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Get the returned value of a file. |
|
217 | - * |
|
218 | - * @param string $path |
|
219 | - * @param array $data |
|
220 | - * |
|
221 | - * @return mixed |
|
222 | - * |
|
223 | - * @throws \Syscodes\Filesystem\Exceptions\FileNotFoundException |
|
224 | - */ |
|
225 | - public function getRequire($path, array $data = []) |
|
226 | - { |
|
227 | - if ($this->isFile($path)) { |
|
228 | - $__path = $path; |
|
229 | - $__data = $data; |
|
230 | - |
|
231 | - return (static function () use ($__path, $__data) { |
|
232 | - extract($__data, EXTR_SKIP); |
|
233 | - |
|
234 | - return require $__path; |
|
235 | - })(); |
|
236 | - } |
|
237 | - |
|
238 | - throw new FileNotFoundException($path); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Require the given file once. |
|
243 | - * |
|
244 | - * @param string $path |
|
245 | - * @param array $data |
|
246 | - * |
|
247 | - * @return mixed |
|
248 | - * |
|
249 | - * @throws \Syscodes\Filesystem\Exceptions\FileNotFoundException |
|
250 | - */ |
|
251 | - public function getRequireOnce($path, array $data = []) |
|
252 | - { |
|
253 | - if ($this->isFile($path)) { |
|
254 | - $__path = $path; |
|
255 | - $__data = $data; |
|
256 | - |
|
257 | - return (static function () use ($__path, $__data) { |
|
258 | - extract($__data, EXTR_SKIP); |
|
259 | - |
|
260 | - return require_once $__path; |
|
261 | - })(); |
|
262 | - } |
|
263 | - |
|
264 | - throw new FileNotFoundException($path); |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Retrieve the file size. |
|
269 | - * |
|
270 | - * Implementations SHOULD return the value stored in the "size" key of |
|
271 | - * the file in the $_FILES array if available, as PHP calculates this |
|
272 | - * based on the actual size transmitted. |
|
273 | - * |
|
274 | - * @param string $path |
|
275 | - * @param string $unit ('b' by default) |
|
276 | - * |
|
277 | - * @return int|null The file size in bytes or null if unknown |
|
278 | - */ |
|
279 | - public function getSize($path, $unit = 'b') |
|
280 | - { |
|
281 | - if ($this->exists($path)) { |
|
282 | - if (is_null($this->size)) { |
|
283 | - $this->size = filesize($path); |
|
284 | - } |
|
285 | - |
|
286 | - switch (strtolower($unit)) { |
|
287 | - case 'kb': |
|
288 | - return number_format($this->size / 1024, 3); |
|
289 | - break; |
|
290 | - case 'mb': |
|
291 | - return number_format(($this->size / 1024) / 1024, 3); |
|
292 | - break; |
|
293 | - } |
|
294 | - |
|
295 | - return $this->size; |
|
296 | - } |
|
297 | - } |
|
126 | + while ( ! feof($this->handler)) { |
|
127 | + $contents .= fgets($this->handler, 4096); |
|
128 | + } |
|
129 | + |
|
130 | + flock($this->handler, LOCK_UN); |
|
131 | + } |
|
132 | + } finally { |
|
133 | + $this->close(); |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + return trim($contents); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Opens the current file with a given $mode. |
|
142 | + * |
|
143 | + * @param string $path |
|
144 | + * @param string $mode A valid 'fopen' mode string (r|w|a ...) |
|
145 | + * @param bool $force |
|
146 | + * |
|
147 | + * @return bool |
|
148 | + */ |
|
149 | + public function open($path, $mode, $force = false): bool |
|
150 | + { |
|
151 | + if ( ! $force && is_resource($this->handler)) { |
|
152 | + return true; |
|
153 | + } |
|
154 | + |
|
155 | + if ($this->exists($path) === false) { |
|
156 | + if ($this->create($path) === false) { |
|
157 | + return false; |
|
158 | + } |
|
159 | + } |
|
160 | + |
|
161 | + $this->handler = fopen($path, $mode); |
|
162 | + |
|
163 | + return is_resource($this->handler); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Creates the file. |
|
168 | + * |
|
169 | + * @param string $path |
|
170 | + * |
|
171 | + * @return bool |
|
172 | + */ |
|
173 | + public function create($path): bool |
|
174 | + { |
|
175 | + if (($this->isDirectory($path)) && ($this->isWritable($path)) || ( ! $this->exists($path))) { |
|
176 | + if (touch($path)) { |
|
177 | + return true; |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + return false; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Determine if a file exists. |
|
186 | + * |
|
187 | + * @param string $path |
|
188 | + * |
|
189 | + * @return bool |
|
190 | + */ |
|
191 | + public function exists($path): bool |
|
192 | + { |
|
193 | + $this->clearStatCache($path); |
|
194 | + |
|
195 | + return file_exists($path); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Clear PHP's internal stat cache. |
|
200 | + * |
|
201 | + * @param string $path |
|
202 | + * @param bool $all Clear all cache or not |
|
203 | + * |
|
204 | + * @return void |
|
205 | + */ |
|
206 | + public function clearStatCache($path, $all = false): void |
|
207 | + { |
|
208 | + if ($all === false) { |
|
209 | + clearstatcache(true, $path); |
|
210 | + } |
|
211 | + |
|
212 | + clearstatcache(); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Get the returned value of a file. |
|
217 | + * |
|
218 | + * @param string $path |
|
219 | + * @param array $data |
|
220 | + * |
|
221 | + * @return mixed |
|
222 | + * |
|
223 | + * @throws \Syscodes\Filesystem\Exceptions\FileNotFoundException |
|
224 | + */ |
|
225 | + public function getRequire($path, array $data = []) |
|
226 | + { |
|
227 | + if ($this->isFile($path)) { |
|
228 | + $__path = $path; |
|
229 | + $__data = $data; |
|
230 | + |
|
231 | + return (static function () use ($__path, $__data) { |
|
232 | + extract($__data, EXTR_SKIP); |
|
233 | + |
|
234 | + return require $__path; |
|
235 | + })(); |
|
236 | + } |
|
237 | + |
|
238 | + throw new FileNotFoundException($path); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Require the given file once. |
|
243 | + * |
|
244 | + * @param string $path |
|
245 | + * @param array $data |
|
246 | + * |
|
247 | + * @return mixed |
|
248 | + * |
|
249 | + * @throws \Syscodes\Filesystem\Exceptions\FileNotFoundException |
|
250 | + */ |
|
251 | + public function getRequireOnce($path, array $data = []) |
|
252 | + { |
|
253 | + if ($this->isFile($path)) { |
|
254 | + $__path = $path; |
|
255 | + $__data = $data; |
|
256 | + |
|
257 | + return (static function () use ($__path, $__data) { |
|
258 | + extract($__data, EXTR_SKIP); |
|
259 | + |
|
260 | + return require_once $__path; |
|
261 | + })(); |
|
262 | + } |
|
263 | + |
|
264 | + throw new FileNotFoundException($path); |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Retrieve the file size. |
|
269 | + * |
|
270 | + * Implementations SHOULD return the value stored in the "size" key of |
|
271 | + * the file in the $_FILES array if available, as PHP calculates this |
|
272 | + * based on the actual size transmitted. |
|
273 | + * |
|
274 | + * @param string $path |
|
275 | + * @param string $unit ('b' by default) |
|
276 | + * |
|
277 | + * @return int|null The file size in bytes or null if unknown |
|
278 | + */ |
|
279 | + public function getSize($path, $unit = 'b') |
|
280 | + { |
|
281 | + if ($this->exists($path)) { |
|
282 | + if (is_null($this->size)) { |
|
283 | + $this->size = filesize($path); |
|
284 | + } |
|
285 | + |
|
286 | + switch (strtolower($unit)) { |
|
287 | + case 'kb': |
|
288 | + return number_format($this->size / 1024, 3); |
|
289 | + break; |
|
290 | + case 'mb': |
|
291 | + return number_format(($this->size / 1024) / 1024, 3); |
|
292 | + break; |
|
293 | + } |
|
294 | + |
|
295 | + return $this->size; |
|
296 | + } |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Returns the file's group. |
|
301 | - * |
|
302 | - * @param string $path |
|
303 | - * |
|
304 | - * @return int|bool The file group, or false in case of an error |
|
305 | - */ |
|
306 | - public function group($path) |
|
307 | - { |
|
308 | - if ($this->exists($path)) { |
|
309 | - return filegroup($path); |
|
310 | - } |
|
311 | - |
|
312 | - return false; |
|
313 | - } |
|
299 | + /** |
|
300 | + * Returns the file's group. |
|
301 | + * |
|
302 | + * @param string $path |
|
303 | + * |
|
304 | + * @return int|bool The file group, or false in case of an error |
|
305 | + */ |
|
306 | + public function group($path) |
|
307 | + { |
|
308 | + if ($this->exists($path)) { |
|
309 | + return filegroup($path); |
|
310 | + } |
|
311 | + |
|
312 | + return false; |
|
313 | + } |
|
314 | 314 | |
315 | - /** |
|
316 | - * Returns true if the file is executable. |
|
317 | - * |
|
318 | - * @param string $path |
|
319 | - * |
|
320 | - * @return bool True if file is executable, false otherwise |
|
321 | - */ |
|
322 | - public function exec($path): bool |
|
323 | - { |
|
324 | - return is_executable($path); |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Determine if the given path is a directory. |
|
329 | - * |
|
330 | - * @param string $directory |
|
331 | - * |
|
332 | - * @return bool |
|
333 | - */ |
|
334 | - public function isDirectory($directory): bool |
|
335 | - { |
|
336 | - return is_dir($directory); |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Determine if the given path is a file. |
|
341 | - * |
|
342 | - * @param string $file |
|
343 | - * |
|
344 | - * @return bool |
|
345 | - */ |
|
346 | - public function isFile($file): bool |
|
347 | - { |
|
348 | - return is_file($file); |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * Determine if the given path is writable. |
|
353 | - * |
|
354 | - * @param string $path |
|
355 | - * |
|
356 | - * @return bool |
|
357 | - */ |
|
358 | - public function isWritable($path): bool |
|
359 | - { |
|
360 | - return is_writable($path); |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Returns if true the file is readable. |
|
365 | - * |
|
366 | - * @param string $path |
|
367 | - * |
|
368 | - * @return bool True if file is readable, false otherwise |
|
369 | - */ |
|
370 | - public function isReadable($path): bool |
|
371 | - { |
|
372 | - return is_readable($path); |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Returns last access time. |
|
377 | - * |
|
378 | - * @param string $path |
|
379 | - * |
|
380 | - * @return int|bool Timestamp of last access time, or false in case of an error |
|
381 | - */ |
|
382 | - public function lastAccess($path) |
|
383 | - { |
|
384 | - if ($this->exists($path)) { |
|
385 | - return fileatime($path); |
|
386 | - } |
|
387 | - |
|
388 | - return false; |
|
389 | - } |
|
390 | - |
|
391 | - /** |
|
392 | - * Returns last modified time. |
|
393 | - * |
|
394 | - * @param string $path |
|
395 | - * |
|
396 | - * @return int|bool Timestamp of last modified time, or false in case of an error |
|
397 | - */ |
|
398 | - public function lastModified($path) |
|
399 | - { |
|
400 | - if ($this->exists($path)) { |
|
401 | - return filemtime($path); |
|
402 | - } |
|
403 | - |
|
404 | - return false; |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * Get all of the directories within a given directory. |
|
409 | - * |
|
410 | - * @param string $directory |
|
411 | - * |
|
412 | - * @return array |
|
413 | - */ |
|
414 | - public function directories($directory): array |
|
415 | - { |
|
416 | - $directories = []; |
|
417 | - |
|
418 | - $iterators = new FilesystemIterator($directory); |
|
419 | - |
|
420 | - foreach ($iterators as $iterator) { |
|
421 | - $directories[] = trim($iterator->getPathname(), '/').'/'; |
|
422 | - } |
|
423 | - |
|
424 | - return $directories; |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
428 | - * Delete the file at a given path. |
|
429 | - * |
|
430 | - * @param string $paths |
|
431 | - * |
|
432 | - * @return bool |
|
433 | - */ |
|
434 | - public function delete($paths): bool |
|
435 | - { |
|
436 | - if (is_resource($this->handler)) { |
|
437 | - fclose($this->handler); |
|
438 | - $this->handler = null; |
|
439 | - } |
|
440 | - |
|
441 | - $paths = is_array($paths) ? $paths : func_get_args(); |
|
442 | - |
|
443 | - $success = true; |
|
444 | - |
|
445 | - foreach ($paths as $path) { |
|
446 | - try { |
|
447 | - if ( ! @unlink($path)) { |
|
448 | - return $success = false; |
|
449 | - } |
|
450 | - } catch (ErrorException $e) { |
|
451 | - return $success = false; |
|
452 | - } |
|
453 | - } |
|
454 | - |
|
455 | - return $success; |
|
456 | - } |
|
457 | - |
|
458 | - /** |
|
459 | - * Create a directory. |
|
460 | - * |
|
461 | - * @param string $path |
|
462 | - * @param int $mode |
|
463 | - * @param bool $recursive |
|
464 | - * @param bool $force |
|
465 | - * |
|
466 | - * @return bool |
|
467 | - * |
|
468 | - * @throws FileException |
|
469 | - */ |
|
470 | - public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false): bool |
|
471 | - { |
|
472 | - if ($force) { |
|
473 | - return @mkdir($path, $mode, $recursive); |
|
474 | - } |
|
475 | - |
|
476 | - mkdir($path, $mode, $recursive); |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Copy a directory from one location to another. |
|
481 | - * |
|
482 | - * @param string $directory |
|
483 | - * @param string $destination |
|
484 | - * @param int $options |
|
485 | - * |
|
486 | - * @return bool |
|
487 | - */ |
|
488 | - public function copyDirectory($directory, $destination, $options = null): bool |
|
489 | - { |
|
490 | - if ( ! $this->isDirectory($directory)) return false; |
|
491 | - |
|
492 | - $options = $options ?: FilesystemIterator::SKIP_DOTS; |
|
315 | + /** |
|
316 | + * Returns true if the file is executable. |
|
317 | + * |
|
318 | + * @param string $path |
|
319 | + * |
|
320 | + * @return bool True if file is executable, false otherwise |
|
321 | + */ |
|
322 | + public function exec($path): bool |
|
323 | + { |
|
324 | + return is_executable($path); |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Determine if the given path is a directory. |
|
329 | + * |
|
330 | + * @param string $directory |
|
331 | + * |
|
332 | + * @return bool |
|
333 | + */ |
|
334 | + public function isDirectory($directory): bool |
|
335 | + { |
|
336 | + return is_dir($directory); |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Determine if the given path is a file. |
|
341 | + * |
|
342 | + * @param string $file |
|
343 | + * |
|
344 | + * @return bool |
|
345 | + */ |
|
346 | + public function isFile($file): bool |
|
347 | + { |
|
348 | + return is_file($file); |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * Determine if the given path is writable. |
|
353 | + * |
|
354 | + * @param string $path |
|
355 | + * |
|
356 | + * @return bool |
|
357 | + */ |
|
358 | + public function isWritable($path): bool |
|
359 | + { |
|
360 | + return is_writable($path); |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Returns if true the file is readable. |
|
365 | + * |
|
366 | + * @param string $path |
|
367 | + * |
|
368 | + * @return bool True if file is readable, false otherwise |
|
369 | + */ |
|
370 | + public function isReadable($path): bool |
|
371 | + { |
|
372 | + return is_readable($path); |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Returns last access time. |
|
377 | + * |
|
378 | + * @param string $path |
|
379 | + * |
|
380 | + * @return int|bool Timestamp of last access time, or false in case of an error |
|
381 | + */ |
|
382 | + public function lastAccess($path) |
|
383 | + { |
|
384 | + if ($this->exists($path)) { |
|
385 | + return fileatime($path); |
|
386 | + } |
|
387 | + |
|
388 | + return false; |
|
389 | + } |
|
390 | + |
|
391 | + /** |
|
392 | + * Returns last modified time. |
|
393 | + * |
|
394 | + * @param string $path |
|
395 | + * |
|
396 | + * @return int|bool Timestamp of last modified time, or false in case of an error |
|
397 | + */ |
|
398 | + public function lastModified($path) |
|
399 | + { |
|
400 | + if ($this->exists($path)) { |
|
401 | + return filemtime($path); |
|
402 | + } |
|
403 | + |
|
404 | + return false; |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * Get all of the directories within a given directory. |
|
409 | + * |
|
410 | + * @param string $directory |
|
411 | + * |
|
412 | + * @return array |
|
413 | + */ |
|
414 | + public function directories($directory): array |
|
415 | + { |
|
416 | + $directories = []; |
|
417 | + |
|
418 | + $iterators = new FilesystemIterator($directory); |
|
419 | + |
|
420 | + foreach ($iterators as $iterator) { |
|
421 | + $directories[] = trim($iterator->getPathname(), '/').'/'; |
|
422 | + } |
|
423 | + |
|
424 | + return $directories; |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | + * Delete the file at a given path. |
|
429 | + * |
|
430 | + * @param string $paths |
|
431 | + * |
|
432 | + * @return bool |
|
433 | + */ |
|
434 | + public function delete($paths): bool |
|
435 | + { |
|
436 | + if (is_resource($this->handler)) { |
|
437 | + fclose($this->handler); |
|
438 | + $this->handler = null; |
|
439 | + } |
|
440 | + |
|
441 | + $paths = is_array($paths) ? $paths : func_get_args(); |
|
442 | + |
|
443 | + $success = true; |
|
444 | + |
|
445 | + foreach ($paths as $path) { |
|
446 | + try { |
|
447 | + if ( ! @unlink($path)) { |
|
448 | + return $success = false; |
|
449 | + } |
|
450 | + } catch (ErrorException $e) { |
|
451 | + return $success = false; |
|
452 | + } |
|
453 | + } |
|
454 | + |
|
455 | + return $success; |
|
456 | + } |
|
457 | + |
|
458 | + /** |
|
459 | + * Create a directory. |
|
460 | + * |
|
461 | + * @param string $path |
|
462 | + * @param int $mode |
|
463 | + * @param bool $recursive |
|
464 | + * @param bool $force |
|
465 | + * |
|
466 | + * @return bool |
|
467 | + * |
|
468 | + * @throws FileException |
|
469 | + */ |
|
470 | + public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false): bool |
|
471 | + { |
|
472 | + if ($force) { |
|
473 | + return @mkdir($path, $mode, $recursive); |
|
474 | + } |
|
475 | + |
|
476 | + mkdir($path, $mode, $recursive); |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Copy a directory from one location to another. |
|
481 | + * |
|
482 | + * @param string $directory |
|
483 | + * @param string $destination |
|
484 | + * @param int $options |
|
485 | + * |
|
486 | + * @return bool |
|
487 | + */ |
|
488 | + public function copyDirectory($directory, $destination, $options = null): bool |
|
489 | + { |
|
490 | + if ( ! $this->isDirectory($directory)) return false; |
|
491 | + |
|
492 | + $options = $options ?: FilesystemIterator::SKIP_DOTS; |
|
493 | 493 | |
494 | - // If the destination directory does not actually exist, we will go ahead and |
|
495 | - // create it recursively, which just gets the destination prepared to copy |
|
496 | - // the files over. Once we make the directory we'll proceed the copying. |
|
497 | - if ( ! $this->isdirectory($destination)) { |
|
498 | - $this->makeDirectory($destination, 0777, true); |
|
499 | - } |
|
494 | + // If the destination directory does not actually exist, we will go ahead and |
|
495 | + // create it recursively, which just gets the destination prepared to copy |
|
496 | + // the files over. Once we make the directory we'll proceed the copying. |
|
497 | + if ( ! $this->isdirectory($destination)) { |
|
498 | + $this->makeDirectory($destination, 0777, true); |
|
499 | + } |
|
500 | 500 | |
501 | - $iterators = new FilesystemIterator($directory, $options); |
|
501 | + $iterators = new FilesystemIterator($directory, $options); |
|
502 | 502 | |
503 | - foreach ($iterators as $iterator) { |
|
504 | - $target = $destination.DIRECTORY_SEPARATOR.$iterator->getBasename(); |
|
503 | + foreach ($iterators as $iterator) { |
|
504 | + $target = $destination.DIRECTORY_SEPARATOR.$iterator->getBasename(); |
|
505 | 505 | |
506 | - // As we spin through items, we will check to see if the current file is actually |
|
506 | + // As we spin through items, we will check to see if the current file is actually |
|
507 | 507 | // a directory or a file. When it is actually a directory we will need to call |
508 | 508 | // back into this function recursively to keep copying these nested folders. |
509 | - if ($iterator->isDir()) { |
|
510 | - if ( ! $this->copyDirectory($iterator->getPathname(), $target, $options)) return false; |
|
511 | - } |
|
512 | - // If the current items is just a regular file, we will just copy this to the new |
|
513 | - // location and keep looping. If for some reason the copy fails we'll bail out |
|
514 | - // and return false, so the developer is aware that the copy process failed. |
|
515 | - else { |
|
516 | - if ( ! $this->copy($iterator->getPathname(), $target)) return false; |
|
517 | - } |
|
518 | - } |
|
519 | - |
|
520 | - return true; |
|
521 | - } |
|
522 | - |
|
523 | - /** |
|
524 | - * Recursively delete a directory and optionally you can keep |
|
525 | - * the directory if you wish. |
|
526 | - * |
|
527 | - * @param string $directory |
|
528 | - * @param bool $keep |
|
529 | - * |
|
530 | - * @return bool |
|
531 | - */ |
|
532 | - public function deleteDirectory($directory, $keep = false): bool |
|
533 | - { |
|
534 | - if ( ! $this->isDirectory($directory)) return false; |
|
535 | - |
|
536 | - $iterators = new filesystemIterator($directory); |
|
537 | - |
|
538 | - foreach ($iterators as $iterator) { |
|
539 | - // If the item is a directory, we can just recurse into the function and delete |
|
540 | - // that sub-directory otherwise we'll just delete the file and keep iterating |
|
541 | - // through each file until the directory is cleaned. |
|
542 | - if ($iterator->isDir() && ! $iterator->isLink()) { |
|
543 | - $this->deleteDirectory($iterator->getPathname()); |
|
544 | - } |
|
545 | - // If the item is just a file, we can go ahead and delete it since we're |
|
546 | - // just looping through and waxing all of the files in this directory |
|
547 | - // and calling directories recursively, so we delete the real path. |
|
548 | - else { |
|
549 | - $this->delete($iterator->getPathname()); |
|
550 | - } |
|
551 | - } |
|
552 | - |
|
553 | - if ( ! $keep) @rmdir($directory); |
|
554 | - |
|
555 | - return true; |
|
556 | - } |
|
557 | - |
|
558 | - /** |
|
559 | - * Empty the specified directory of all files and folders. |
|
560 | - * |
|
561 | - * |
|
562 | - * @param string $directory |
|
563 | - * |
|
564 | - * @return bool |
|
565 | - */ |
|
566 | - public function cleanDirectory($directory): bool |
|
567 | - { |
|
568 | - return $this->deleteDirectory($directory, true); |
|
569 | - } |
|
570 | - |
|
571 | - /** |
|
572 | - * Moves a file to a new location. |
|
573 | - * |
|
574 | - * @param string $from |
|
575 | - * @param string $to |
|
576 | - * @param bool $overwrite |
|
577 | - * |
|
578 | - * @return bool |
|
579 | - */ |
|
580 | - public function moveDirectory($from, $to, $overwrite = false): bool |
|
581 | - { |
|
582 | - if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) return false; |
|
583 | - |
|
584 | - if (false === @rename($from, $to)) { |
|
585 | - $error = error_get_last(); |
|
586 | - |
|
587 | - throw new FileUnableToMoveException($from, $to, strip_tags($error['message'])); |
|
588 | - } |
|
589 | - |
|
590 | - $this->perms($to, 0777 & ~umask()); |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * Attempts to determine the file extension based on the trusted |
|
595 | - * getType() method. If the mime type is unknown, will return null. |
|
596 | - * |
|
597 | - * @param string $path |
|
598 | - * |
|
599 | - * @return string|null |
|
600 | - */ |
|
601 | - public function guessExtension($path) |
|
602 | - { |
|
603 | - return FileMimeType::guessExtensionFromType($this->getMimeType($path)); |
|
604 | - } |
|
605 | - |
|
606 | - /** |
|
607 | - * Retrieve the media type of the file. |
|
608 | - * |
|
609 | - * @param string $path |
|
610 | - * |
|
611 | - * @return string|null |
|
612 | - */ |
|
613 | - public function getMimeType($path) |
|
614 | - { |
|
615 | - $finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
616 | - $mimeType = finfo_file($finfo, $path); |
|
617 | - |
|
618 | - finfo_close($finfo); |
|
619 | - |
|
620 | - return $mimeType; |
|
621 | - } |
|
622 | - |
|
623 | - /** |
|
624 | - * Move a file to a new location. |
|
625 | - * |
|
626 | - * @param string $path |
|
627 | - * @param string $target |
|
628 | - * |
|
629 | - * @return bool |
|
630 | - */ |
|
631 | - public function move($path, $target): bool |
|
632 | - { |
|
633 | - if ($this->exists($path)) { |
|
634 | - return rename($path, $target); |
|
635 | - } |
|
636 | - } |
|
637 | - |
|
638 | - /** |
|
639 | - * Extract the file name from a file path. |
|
640 | - * |
|
641 | - * @param string $path |
|
642 | - * |
|
643 | - * @return string |
|
644 | - */ |
|
645 | - public function name($path) |
|
646 | - { |
|
647 | - return pathinfo($path, PATHINFO_FILENAME); |
|
648 | - } |
|
649 | - |
|
650 | - /** |
|
651 | - * Extract the trailing name component from a file path. |
|
652 | - * |
|
653 | - * @param string $path |
|
654 | - * |
|
655 | - * @return string |
|
656 | - */ |
|
657 | - public function basename($path) |
|
658 | - { |
|
659 | - return pathinfo($path, PATHINFO_BASENAME); |
|
660 | - } |
|
661 | - |
|
662 | - /** |
|
663 | - * Extract the parent directory from a file path. |
|
664 | - * |
|
665 | - * @param string $path |
|
666 | - * |
|
667 | - * @return string |
|
668 | - */ |
|
669 | - public function dirname($path) |
|
670 | - { |
|
671 | - return pathinfo($path, PATHINFO_DIRNAME); |
|
672 | - } |
|
673 | - |
|
674 | - /** |
|
675 | - * Extract the file extension from a file path. |
|
676 | - * |
|
677 | - * @param string $path |
|
678 | - * |
|
679 | - * @return string |
|
680 | - */ |
|
681 | - public function extension($path) |
|
682 | - { |
|
683 | - return pathinfo($path, PATHINFO_EXTENSION); |
|
684 | - } |
|
685 | - |
|
686 | - /** |
|
687 | - * Find path names matching a given pattern. |
|
688 | - * |
|
689 | - * @param string $pattern |
|
690 | - * @param int $flags (0 by default) |
|
691 | - * |
|
692 | - * @return array |
|
693 | - */ |
|
694 | - public function glob($pattern, $flags = 0): bool |
|
695 | - { |
|
696 | - return glob($pattern, $flags); |
|
697 | - } |
|
698 | - |
|
699 | - /** |
|
700 | - * Returns the file's owner. |
|
701 | - * |
|
702 | - * @param string $path |
|
703 | - * |
|
704 | - * @return int|bool The file owner, or false in case of an error |
|
705 | - */ |
|
706 | - public function owner($path) |
|
707 | - { |
|
708 | - if ($this->exists($path)) { |
|
709 | - return fileowner($path); |
|
710 | - } |
|
711 | - |
|
712 | - return false; |
|
713 | - } |
|
714 | - |
|
715 | - /** |
|
716 | - * Returns the "chmod" (permissions) of the file. |
|
717 | - * |
|
718 | - * @param string $path |
|
719 | - * @param int|null $mode |
|
720 | - * |
|
721 | - * @return mixed Permissions for the file, or false in case of an error |
|
722 | - */ |
|
723 | - public function perms($path, $mode = null) |
|
724 | - { |
|
725 | - if ($mode) { |
|
726 | - chmod($path, $mode); |
|
727 | - } |
|
728 | - |
|
729 | - return substr(sprintf('%o', fileperms($path)), -4); |
|
730 | - } |
|
731 | - |
|
732 | - /** |
|
733 | - * Prepend to a file. |
|
734 | - * |
|
735 | - * @param string $path |
|
736 | - * @param string $data |
|
737 | - * |
|
738 | - * @return int |
|
739 | - */ |
|
740 | - public function prepend($path, $data): int |
|
741 | - { |
|
742 | - if ($this->exists($path)) { |
|
743 | - $this->put($path, $data.$this->get($path)); |
|
744 | - } |
|
745 | - |
|
746 | - return $this->put($path, $data); |
|
747 | - } |
|
748 | - |
|
749 | - /** |
|
750 | - * Write the content of a file. |
|
751 | - * |
|
752 | - * @param string $path |
|
753 | - * @param string $contents |
|
754 | - * @param bool $lock |
|
755 | - * |
|
756 | - * @return int |
|
757 | - */ |
|
758 | - public function put($path, $contents, $lock = false): int |
|
759 | - { |
|
760 | - return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
|
761 | - } |
|
762 | - |
|
763 | - /** |
|
764 | - * Get the file type of a given file. |
|
765 | - * |
|
766 | - * @param string $path |
|
767 | - * |
|
768 | - * @return string |
|
769 | - */ |
|
770 | - public function type($path): string |
|
771 | - { |
|
772 | - return filetype($path); |
|
773 | - } |
|
509 | + if ($iterator->isDir()) { |
|
510 | + if ( ! $this->copyDirectory($iterator->getPathname(), $target, $options)) return false; |
|
511 | + } |
|
512 | + // If the current items is just a regular file, we will just copy this to the new |
|
513 | + // location and keep looping. If for some reason the copy fails we'll bail out |
|
514 | + // and return false, so the developer is aware that the copy process failed. |
|
515 | + else { |
|
516 | + if ( ! $this->copy($iterator->getPathname(), $target)) return false; |
|
517 | + } |
|
518 | + } |
|
519 | + |
|
520 | + return true; |
|
521 | + } |
|
522 | + |
|
523 | + /** |
|
524 | + * Recursively delete a directory and optionally you can keep |
|
525 | + * the directory if you wish. |
|
526 | + * |
|
527 | + * @param string $directory |
|
528 | + * @param bool $keep |
|
529 | + * |
|
530 | + * @return bool |
|
531 | + */ |
|
532 | + public function deleteDirectory($directory, $keep = false): bool |
|
533 | + { |
|
534 | + if ( ! $this->isDirectory($directory)) return false; |
|
535 | + |
|
536 | + $iterators = new filesystemIterator($directory); |
|
537 | + |
|
538 | + foreach ($iterators as $iterator) { |
|
539 | + // If the item is a directory, we can just recurse into the function and delete |
|
540 | + // that sub-directory otherwise we'll just delete the file and keep iterating |
|
541 | + // through each file until the directory is cleaned. |
|
542 | + if ($iterator->isDir() && ! $iterator->isLink()) { |
|
543 | + $this->deleteDirectory($iterator->getPathname()); |
|
544 | + } |
|
545 | + // If the item is just a file, we can go ahead and delete it since we're |
|
546 | + // just looping through and waxing all of the files in this directory |
|
547 | + // and calling directories recursively, so we delete the real path. |
|
548 | + else { |
|
549 | + $this->delete($iterator->getPathname()); |
|
550 | + } |
|
551 | + } |
|
552 | + |
|
553 | + if ( ! $keep) @rmdir($directory); |
|
554 | + |
|
555 | + return true; |
|
556 | + } |
|
557 | + |
|
558 | + /** |
|
559 | + * Empty the specified directory of all files and folders. |
|
560 | + * |
|
561 | + * |
|
562 | + * @param string $directory |
|
563 | + * |
|
564 | + * @return bool |
|
565 | + */ |
|
566 | + public function cleanDirectory($directory): bool |
|
567 | + { |
|
568 | + return $this->deleteDirectory($directory, true); |
|
569 | + } |
|
570 | + |
|
571 | + /** |
|
572 | + * Moves a file to a new location. |
|
573 | + * |
|
574 | + * @param string $from |
|
575 | + * @param string $to |
|
576 | + * @param bool $overwrite |
|
577 | + * |
|
578 | + * @return bool |
|
579 | + */ |
|
580 | + public function moveDirectory($from, $to, $overwrite = false): bool |
|
581 | + { |
|
582 | + if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) return false; |
|
583 | + |
|
584 | + if (false === @rename($from, $to)) { |
|
585 | + $error = error_get_last(); |
|
586 | + |
|
587 | + throw new FileUnableToMoveException($from, $to, strip_tags($error['message'])); |
|
588 | + } |
|
589 | + |
|
590 | + $this->perms($to, 0777 & ~umask()); |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * Attempts to determine the file extension based on the trusted |
|
595 | + * getType() method. If the mime type is unknown, will return null. |
|
596 | + * |
|
597 | + * @param string $path |
|
598 | + * |
|
599 | + * @return string|null |
|
600 | + */ |
|
601 | + public function guessExtension($path) |
|
602 | + { |
|
603 | + return FileMimeType::guessExtensionFromType($this->getMimeType($path)); |
|
604 | + } |
|
605 | + |
|
606 | + /** |
|
607 | + * Retrieve the media type of the file. |
|
608 | + * |
|
609 | + * @param string $path |
|
610 | + * |
|
611 | + * @return string|null |
|
612 | + */ |
|
613 | + public function getMimeType($path) |
|
614 | + { |
|
615 | + $finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
616 | + $mimeType = finfo_file($finfo, $path); |
|
617 | + |
|
618 | + finfo_close($finfo); |
|
619 | + |
|
620 | + return $mimeType; |
|
621 | + } |
|
622 | + |
|
623 | + /** |
|
624 | + * Move a file to a new location. |
|
625 | + * |
|
626 | + * @param string $path |
|
627 | + * @param string $target |
|
628 | + * |
|
629 | + * @return bool |
|
630 | + */ |
|
631 | + public function move($path, $target): bool |
|
632 | + { |
|
633 | + if ($this->exists($path)) { |
|
634 | + return rename($path, $target); |
|
635 | + } |
|
636 | + } |
|
637 | + |
|
638 | + /** |
|
639 | + * Extract the file name from a file path. |
|
640 | + * |
|
641 | + * @param string $path |
|
642 | + * |
|
643 | + * @return string |
|
644 | + */ |
|
645 | + public function name($path) |
|
646 | + { |
|
647 | + return pathinfo($path, PATHINFO_FILENAME); |
|
648 | + } |
|
649 | + |
|
650 | + /** |
|
651 | + * Extract the trailing name component from a file path. |
|
652 | + * |
|
653 | + * @param string $path |
|
654 | + * |
|
655 | + * @return string |
|
656 | + */ |
|
657 | + public function basename($path) |
|
658 | + { |
|
659 | + return pathinfo($path, PATHINFO_BASENAME); |
|
660 | + } |
|
661 | + |
|
662 | + /** |
|
663 | + * Extract the parent directory from a file path. |
|
664 | + * |
|
665 | + * @param string $path |
|
666 | + * |
|
667 | + * @return string |
|
668 | + */ |
|
669 | + public function dirname($path) |
|
670 | + { |
|
671 | + return pathinfo($path, PATHINFO_DIRNAME); |
|
672 | + } |
|
673 | + |
|
674 | + /** |
|
675 | + * Extract the file extension from a file path. |
|
676 | + * |
|
677 | + * @param string $path |
|
678 | + * |
|
679 | + * @return string |
|
680 | + */ |
|
681 | + public function extension($path) |
|
682 | + { |
|
683 | + return pathinfo($path, PATHINFO_EXTENSION); |
|
684 | + } |
|
685 | + |
|
686 | + /** |
|
687 | + * Find path names matching a given pattern. |
|
688 | + * |
|
689 | + * @param string $pattern |
|
690 | + * @param int $flags (0 by default) |
|
691 | + * |
|
692 | + * @return array |
|
693 | + */ |
|
694 | + public function glob($pattern, $flags = 0): bool |
|
695 | + { |
|
696 | + return glob($pattern, $flags); |
|
697 | + } |
|
698 | + |
|
699 | + /** |
|
700 | + * Returns the file's owner. |
|
701 | + * |
|
702 | + * @param string $path |
|
703 | + * |
|
704 | + * @return int|bool The file owner, or false in case of an error |
|
705 | + */ |
|
706 | + public function owner($path) |
|
707 | + { |
|
708 | + if ($this->exists($path)) { |
|
709 | + return fileowner($path); |
|
710 | + } |
|
711 | + |
|
712 | + return false; |
|
713 | + } |
|
714 | + |
|
715 | + /** |
|
716 | + * Returns the "chmod" (permissions) of the file. |
|
717 | + * |
|
718 | + * @param string $path |
|
719 | + * @param int|null $mode |
|
720 | + * |
|
721 | + * @return mixed Permissions for the file, or false in case of an error |
|
722 | + */ |
|
723 | + public function perms($path, $mode = null) |
|
724 | + { |
|
725 | + if ($mode) { |
|
726 | + chmod($path, $mode); |
|
727 | + } |
|
728 | + |
|
729 | + return substr(sprintf('%o', fileperms($path)), -4); |
|
730 | + } |
|
731 | + |
|
732 | + /** |
|
733 | + * Prepend to a file. |
|
734 | + * |
|
735 | + * @param string $path |
|
736 | + * @param string $data |
|
737 | + * |
|
738 | + * @return int |
|
739 | + */ |
|
740 | + public function prepend($path, $data): int |
|
741 | + { |
|
742 | + if ($this->exists($path)) { |
|
743 | + $this->put($path, $data.$this->get($path)); |
|
744 | + } |
|
745 | + |
|
746 | + return $this->put($path, $data); |
|
747 | + } |
|
748 | + |
|
749 | + /** |
|
750 | + * Write the content of a file. |
|
751 | + * |
|
752 | + * @param string $path |
|
753 | + * @param string $contents |
|
754 | + * @param bool $lock |
|
755 | + * |
|
756 | + * @return int |
|
757 | + */ |
|
758 | + public function put($path, $contents, $lock = false): int |
|
759 | + { |
|
760 | + return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
|
761 | + } |
|
762 | + |
|
763 | + /** |
|
764 | + * Get the file type of a given file. |
|
765 | + * |
|
766 | + * @param string $path |
|
767 | + * |
|
768 | + * @return string |
|
769 | + */ |
|
770 | + public function type($path): string |
|
771 | + { |
|
772 | + return filetype($path); |
|
773 | + } |
|
774 | 774 | |
775 | - /** |
|
776 | - * Write the contents of a file, replacing it atomically if it already exists. |
|
777 | - * |
|
778 | - * @param string $path |
|
779 | - * @param string $content |
|
780 | - * |
|
781 | - * @return void |
|
782 | - */ |
|
783 | - public function replace($path, $content): void |
|
784 | - { |
|
785 | - $this->clearstatcache($path); |
|
775 | + /** |
|
776 | + * Write the contents of a file, replacing it atomically if it already exists. |
|
777 | + * |
|
778 | + * @param string $path |
|
779 | + * @param string $content |
|
780 | + * |
|
781 | + * @return void |
|
782 | + */ |
|
783 | + public function replace($path, $content): void |
|
784 | + { |
|
785 | + $this->clearstatcache($path); |
|
786 | 786 | |
787 | - $path = realpath($path) ?: $path; |
|
787 | + $path = realpath($path) ?: $path; |
|
788 | 788 | |
789 | - $tempPath = tempnam(dirname($path), basename($path)); |
|
789 | + $tempPath = tempnam(dirname($path), basename($path)); |
|
790 | 790 | |
791 | - $this->perms($tempPath, 0777 - umask()); |
|
791 | + $this->perms($tempPath, 0777 - umask()); |
|
792 | 792 | |
793 | - $this->put($tempPath, $content); |
|
793 | + $this->put($tempPath, $content); |
|
794 | 794 | |
795 | - $this->move($tempPath, $path); |
|
796 | - } |
|
797 | - |
|
798 | - /** |
|
799 | - * Searches for a given text and replaces the text if found. |
|
800 | - * |
|
801 | - * @param string $path |
|
802 | - * @param string $search |
|
803 | - * @param string $replace |
|
804 | - * |
|
805 | - * @return bool |
|
806 | - */ |
|
807 | - public function replaceText($path, $search, $replace) |
|
808 | - { |
|
809 | - if ( ! $this->open($path, 'r+')) { |
|
810 | - return false; |
|
811 | - } |
|
812 | - |
|
813 | - if ($this->lock !== null) { |
|
814 | - if (flock($this->handler, LOCK_EX) === false) |
|
815 | - { |
|
816 | - return false; |
|
817 | - } |
|
818 | - } |
|
819 | - |
|
820 | - $replaced = $this->write($path, str_replace($search, $replace, $this->get($path)), true); |
|
821 | - |
|
822 | - if ($this->lock !== null) { |
|
823 | - flock($this->handler, LOCK_UN); |
|
824 | - } |
|
825 | - |
|
826 | - $this->close(); |
|
827 | - |
|
828 | - return $replaced; |
|
829 | - } |
|
830 | - |
|
831 | - /** |
|
832 | - * Closes the current file if it is opened. |
|
833 | - * |
|
834 | - * @return bool |
|
835 | - */ |
|
836 | - public function close(): bool |
|
837 | - { |
|
838 | - if ( ! is_resource($this->handler)) { |
|
839 | - return true; |
|
840 | - } |
|
841 | - |
|
842 | - return fclose($this->handler); |
|
843 | - } |
|
844 | - |
|
845 | - /** |
|
846 | - * Write given data to this file. |
|
847 | - * |
|
848 | - * @param string $path |
|
849 | - * @param string $data Data to write to this File |
|
850 | - * @param bool $force The file to open |
|
851 | - * |
|
852 | - * @return bool |
|
853 | - */ |
|
854 | - public function write($path, $data, $force = false): bool |
|
855 | - { |
|
856 | - $success = false; |
|
857 | - |
|
858 | - if ($this->open($path, 'w', $force) === true) { |
|
859 | - if ($this->lock !== null) { |
|
860 | - if (flock($this->handler, LOCK_EX) === false) { |
|
861 | - return false; |
|
862 | - } |
|
863 | - } |
|
864 | - |
|
865 | - if (fwrite($this->handler, $data) !== false) { |
|
866 | - $success = true; |
|
867 | - } |
|
868 | - |
|
869 | - if ($this->lock !== null) { |
|
870 | - flock($this->handler, LOCK_UN); |
|
871 | - } |
|
872 | - } |
|
873 | - |
|
874 | - return $success; |
|
875 | - } |
|
795 | + $this->move($tempPath, $path); |
|
796 | + } |
|
797 | + |
|
798 | + /** |
|
799 | + * Searches for a given text and replaces the text if found. |
|
800 | + * |
|
801 | + * @param string $path |
|
802 | + * @param string $search |
|
803 | + * @param string $replace |
|
804 | + * |
|
805 | + * @return bool |
|
806 | + */ |
|
807 | + public function replaceText($path, $search, $replace) |
|
808 | + { |
|
809 | + if ( ! $this->open($path, 'r+')) { |
|
810 | + return false; |
|
811 | + } |
|
812 | + |
|
813 | + if ($this->lock !== null) { |
|
814 | + if (flock($this->handler, LOCK_EX) === false) |
|
815 | + { |
|
816 | + return false; |
|
817 | + } |
|
818 | + } |
|
819 | + |
|
820 | + $replaced = $this->write($path, str_replace($search, $replace, $this->get($path)), true); |
|
821 | + |
|
822 | + if ($this->lock !== null) { |
|
823 | + flock($this->handler, LOCK_UN); |
|
824 | + } |
|
825 | + |
|
826 | + $this->close(); |
|
827 | + |
|
828 | + return $replaced; |
|
829 | + } |
|
830 | + |
|
831 | + /** |
|
832 | + * Closes the current file if it is opened. |
|
833 | + * |
|
834 | + * @return bool |
|
835 | + */ |
|
836 | + public function close(): bool |
|
837 | + { |
|
838 | + if ( ! is_resource($this->handler)) { |
|
839 | + return true; |
|
840 | + } |
|
841 | + |
|
842 | + return fclose($this->handler); |
|
843 | + } |
|
844 | + |
|
845 | + /** |
|
846 | + * Write given data to this file. |
|
847 | + * |
|
848 | + * @param string $path |
|
849 | + * @param string $data Data to write to this File |
|
850 | + * @param bool $force The file to open |
|
851 | + * |
|
852 | + * @return bool |
|
853 | + */ |
|
854 | + public function write($path, $data, $force = false): bool |
|
855 | + { |
|
856 | + $success = false; |
|
857 | + |
|
858 | + if ($this->open($path, 'w', $force) === true) { |
|
859 | + if ($this->lock !== null) { |
|
860 | + if (flock($this->handler, LOCK_EX) === false) { |
|
861 | + return false; |
|
862 | + } |
|
863 | + } |
|
864 | + |
|
865 | + if (fwrite($this->handler, $data) !== false) { |
|
866 | + $success = true; |
|
867 | + } |
|
868 | + |
|
869 | + if ($this->lock !== null) { |
|
870 | + flock($this->handler, LOCK_UN); |
|
871 | + } |
|
872 | + } |
|
873 | + |
|
874 | + return $success; |
|
875 | + } |
|
876 | 876 | } |
877 | 877 | \ No newline at end of file |
@@ -487,7 +487,9 @@ discard block |
||
487 | 487 | */ |
488 | 488 | public function copyDirectory($directory, $destination, $options = null): bool |
489 | 489 | { |
490 | - if ( ! $this->isDirectory($directory)) return false; |
|
490 | + if ( ! $this->isDirectory($directory)) { |
|
491 | + return false; |
|
492 | + } |
|
491 | 493 | |
492 | 494 | $options = $options ?: FilesystemIterator::SKIP_DOTS; |
493 | 495 | |
@@ -507,13 +509,17 @@ discard block |
||
507 | 509 | // a directory or a file. When it is actually a directory we will need to call |
508 | 510 | // back into this function recursively to keep copying these nested folders. |
509 | 511 | if ($iterator->isDir()) { |
510 | - if ( ! $this->copyDirectory($iterator->getPathname(), $target, $options)) return false; |
|
512 | + if ( ! $this->copyDirectory($iterator->getPathname(), $target, $options)) { |
|
513 | + return false; |
|
514 | + } |
|
511 | 515 | } |
512 | 516 | // If the current items is just a regular file, we will just copy this to the new |
513 | 517 | // location and keep looping. If for some reason the copy fails we'll bail out |
514 | 518 | // and return false, so the developer is aware that the copy process failed. |
515 | 519 | else { |
516 | - if ( ! $this->copy($iterator->getPathname(), $target)) return false; |
|
520 | + if ( ! $this->copy($iterator->getPathname(), $target)) { |
|
521 | + return false; |
|
522 | + } |
|
517 | 523 | } |
518 | 524 | } |
519 | 525 | |
@@ -531,7 +537,9 @@ discard block |
||
531 | 537 | */ |
532 | 538 | public function deleteDirectory($directory, $keep = false): bool |
533 | 539 | { |
534 | - if ( ! $this->isDirectory($directory)) return false; |
|
540 | + if ( ! $this->isDirectory($directory)) { |
|
541 | + return false; |
|
542 | + } |
|
535 | 543 | |
536 | 544 | $iterators = new filesystemIterator($directory); |
537 | 545 | |
@@ -550,7 +558,9 @@ discard block |
||
550 | 558 | } |
551 | 559 | } |
552 | 560 | |
553 | - if ( ! $keep) @rmdir($directory); |
|
561 | + if ( ! $keep) { |
|
562 | + @rmdir($directory); |
|
563 | + } |
|
554 | 564 | |
555 | 565 | return true; |
556 | 566 | } |
@@ -579,7 +589,9 @@ discard block |
||
579 | 589 | */ |
580 | 590 | public function moveDirectory($from, $to, $overwrite = false): bool |
581 | 591 | { |
582 | - if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) return false; |
|
592 | + if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) { |
|
593 | + return false; |
|
594 | + } |
|
583 | 595 | |
584 | 596 | if (false === @rename($from, $to)) { |
585 | 597 | $error = error_get_last(); |
@@ -40,15 +40,15 @@ |
||
40 | 40 | */ |
41 | 41 | public function register() |
42 | 42 | { |
43 | - $this->app->singleton('cache', function ($app) { |
|
43 | + $this->app->singleton('cache', function($app) { |
|
44 | 44 | return (new CacheManager($app))->driver(); |
45 | 45 | }); |
46 | 46 | |
47 | - $this->app->singleton('cache.store', function ($app) { |
|
47 | + $this->app->singleton('cache.store', function($app) { |
|
48 | 48 | return $app['cache']->driver(); |
49 | 49 | }); |
50 | 50 | |
51 | - $this->app->singleton('memcached.connector', function () { |
|
51 | + $this->app->singleton('memcached.connector', function() { |
|
52 | 52 | return new MemcachedConnector; |
53 | 53 | }); |
54 | 54 | } |
@@ -51,7 +51,9 @@ |
||
51 | 51 | */ |
52 | 52 | public function get($key) |
53 | 53 | { |
54 | - if ( ! isset($this->storage[$key])) return; |
|
54 | + if ( ! isset($this->storage[$key])) { |
|
55 | + return; |
|
56 | + } |
|
55 | 57 | |
56 | 58 | $item = $this->storage[$key]; |
57 | 59 |
@@ -101,7 +101,7 @@ |
||
101 | 101 | public function increment($key, $value = 1) |
102 | 102 | { |
103 | 103 | if ( ! is_null($existing = $this->get($key))) { |
104 | - return take(((int) $existing) + $value, function ($incremented) use ($key) { |
|
104 | + return take(((int) $existing) + $value, function($incremented) use ($key) { |
|
105 | 105 | $value = $this->serialized ? serialize($incremented) : $incremented; |
106 | 106 | |
107 | 107 | $this->storage[$key]['value'] = $value; |
@@ -39,13 +39,13 @@ |
||
39 | 39 | */ |
40 | 40 | public function register() |
41 | 41 | { |
42 | - $this->app->singleton('redis', function ($app) { |
|
42 | + $this->app->singleton('redis', function($app) { |
|
43 | 43 | $config = $app['config']->get('database.redis', []); |
44 | 44 | |
45 | 45 | return new RedisManager($config); |
46 | 46 | }); |
47 | 47 | |
48 | - $this->app->bind('redis.connection', function ($app) { |
|
48 | + $this->app->bind('redis.connection', function($app) { |
|
49 | 49 | return $app['redis']->connection(); |
50 | 50 | }); |
51 | 51 | } |
@@ -113,9 +113,9 @@ |
||
113 | 113 | protected function createEnv($app) |
114 | 114 | { |
115 | 115 | return Dotenv::create( |
116 | - Environment::getRepositoryCreator(), |
|
117 | - $app->environmentPath(), |
|
118 | - $app->environmentFile() |
|
116 | + Environment::getRepositoryCreator(), |
|
117 | + $app->environmentPath(), |
|
118 | + $app->environmentFile() |
|
119 | 119 | ); |
120 | 120 | } |
121 | 121 | } |
122 | 122 | \ No newline at end of file |
@@ -259,7 +259,7 @@ |
||
259 | 259 | $quote = $value[0]; |
260 | 260 | |
261 | 261 | $regexPattern = sprintf( |
262 | - '/^ |
|
262 | + '/^ |
|
263 | 263 | %1$s # match a quote at the start of the value |
264 | 264 | ( # capturing sub-pattern used |
265 | 265 | (?: # we do not need to capture this |
@@ -274,7 +274,7 @@ |
||
274 | 274 | if (strpos($value, '$') !== false) { |
275 | 275 | $repository = $this->repository; |
276 | 276 | |
277 | - $value = preg_replace_callback('~\${([a-zA-Z0-9_]+)}~', function ($pattern) use ($repository) { |
|
277 | + $value = preg_replace_callback('~\${([a-zA-Z0-9_]+)}~', function($pattern) use ($repository) { |
|
278 | 278 | $nestedVariable = $repository->get($pattern[1]); |
279 | 279 | |
280 | 280 | if (is_null($nestedVariable)) { |
@@ -142,8 +142,7 @@ |
||
142 | 142 | } |
143 | 143 | |
144 | 144 | $Quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit(); |
145 | - } |
|
146 | - finally { |
|
145 | + } finally { |
|
147 | 146 | // Returns the contents of the output buffer |
148 | 147 | $output = $this->system->CleanOutputBuffer(); |
149 | 148 | } |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | $this->system->endOutputBuffering(); |
157 | 157 | } |
158 | 158 | |
159 | - if (Misc::sendHeaders() && $handlerContentType) { |
|
159 | + if (Misc::sendHeaders() && $handlerContentType) { |
|
160 | 160 | header("Content-Type: {$handlerContentType}"); |
161 | 161 | } |
162 | 162 | } |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | |
318 | 318 | if ( ! $handler instanceof MainHandler) { |
319 | 319 | throw new InvalidArgumentException( |
320 | - "Argument to " . __METHOD__ . " must be a callable, or instance of ". |
|
320 | + "Argument to ".__METHOD__." must be a callable, or instance of ". |
|
321 | 321 | "Syscodes\Components\\Contracts\\Debug\\Handler" |
322 | 322 | ); |
323 | 323 | } |
@@ -40,351 +40,351 @@ |
||
40 | 40 | */ |
41 | 41 | class GDebug implements DebugContract |
42 | 42 | { |
43 | - /** |
|
44 | - * Allow Handlers to force the script to quit. |
|
45 | - * |
|
46 | - * @var bool $allowQuit |
|
47 | - */ |
|
48 | - protected $allowQuit = true; |
|
43 | + /** |
|
44 | + * Allow Handlers to force the script to quit. |
|
45 | + * |
|
46 | + * @var bool $allowQuit |
|
47 | + */ |
|
48 | + protected $allowQuit = true; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Benchmark instance. |
|
52 | - * |
|
53 | - * @var string|object $benchmark |
|
54 | - */ |
|
55 | - protected $benchmark; |
|
56 | - |
|
57 | - /** |
|
58 | - * The handler stack. |
|
59 | - * |
|
60 | - * @var array $handlerStack |
|
61 | - */ |
|
62 | - protected $handlerStack = []; |
|
63 | - |
|
64 | - /** |
|
65 | - * The send Http code by default: 500 Internal Server Error. |
|
66 | - * |
|
67 | - * @var bool $sendHttpCode |
|
68 | - */ |
|
69 | - protected $sendHttpCode = 500; |
|
70 | - |
|
71 | - /** |
|
72 | - * The send output. |
|
73 | - * |
|
74 | - * @var bool $sendOutput |
|
75 | - */ |
|
76 | - protected $sendOutput = true; |
|
77 | - |
|
78 | - /** |
|
79 | - * The functions of system what control errors and exceptions. |
|
80 | - * |
|
81 | - * @var string|object $system |
|
82 | - */ |
|
83 | - protected $system; |
|
84 | - |
|
85 | - /** |
|
86 | - * In certain scenarios, like in shutdown handler, we can not throw exceptions. |
|
87 | - * |
|
88 | - * @var bool $throwExceptions |
|
89 | - */ |
|
90 | - protected $throwExceptions = true; |
|
91 | - |
|
92 | - /** |
|
93 | - * Constructor. The Debug class instance. |
|
94 | - * |
|
95 | - * @param \Syscodes\Components\Debug\Util\System|null $system |
|
96 | - * |
|
97 | - * @return void |
|
98 | - */ |
|
99 | - public function __construct(System $system = null) |
|
100 | - { |
|
101 | - $this->system = $system ?: new System; |
|
102 | - $this->benchmark = new Benchmark; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * {@inheritdoc} |
|
107 | - */ |
|
108 | - public function handleException(Throwable $exception): string |
|
109 | - { |
|
110 | - // The start benchmark |
|
111 | - $this->benchmark->start('total_execution', LENEVOR_START); |
|
112 | - |
|
113 | - $supervisor = $this->getSupervisor($exception); |
|
114 | - |
|
115 | - // Start buffer |
|
116 | - $this->system->startOutputBuferring(); |
|
117 | - |
|
118 | - $handlerResponse = null; |
|
119 | - $handlerContentType = null; |
|
50 | + /** |
|
51 | + * Benchmark instance. |
|
52 | + * |
|
53 | + * @var string|object $benchmark |
|
54 | + */ |
|
55 | + protected $benchmark; |
|
56 | + |
|
57 | + /** |
|
58 | + * The handler stack. |
|
59 | + * |
|
60 | + * @var array $handlerStack |
|
61 | + */ |
|
62 | + protected $handlerStack = []; |
|
63 | + |
|
64 | + /** |
|
65 | + * The send Http code by default: 500 Internal Server Error. |
|
66 | + * |
|
67 | + * @var bool $sendHttpCode |
|
68 | + */ |
|
69 | + protected $sendHttpCode = 500; |
|
70 | + |
|
71 | + /** |
|
72 | + * The send output. |
|
73 | + * |
|
74 | + * @var bool $sendOutput |
|
75 | + */ |
|
76 | + protected $sendOutput = true; |
|
77 | + |
|
78 | + /** |
|
79 | + * The functions of system what control errors and exceptions. |
|
80 | + * |
|
81 | + * @var string|object $system |
|
82 | + */ |
|
83 | + protected $system; |
|
84 | + |
|
85 | + /** |
|
86 | + * In certain scenarios, like in shutdown handler, we can not throw exceptions. |
|
87 | + * |
|
88 | + * @var bool $throwExceptions |
|
89 | + */ |
|
90 | + protected $throwExceptions = true; |
|
91 | + |
|
92 | + /** |
|
93 | + * Constructor. The Debug class instance. |
|
94 | + * |
|
95 | + * @param \Syscodes\Components\Debug\Util\System|null $system |
|
96 | + * |
|
97 | + * @return void |
|
98 | + */ |
|
99 | + public function __construct(System $system = null) |
|
100 | + { |
|
101 | + $this->system = $system ?: new System; |
|
102 | + $this->benchmark = new Benchmark; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * {@inheritdoc} |
|
107 | + */ |
|
108 | + public function handleException(Throwable $exception): string |
|
109 | + { |
|
110 | + // The start benchmark |
|
111 | + $this->benchmark->start('total_execution', LENEVOR_START); |
|
112 | + |
|
113 | + $supervisor = $this->getSupervisor($exception); |
|
114 | + |
|
115 | + // Start buffer |
|
116 | + $this->system->startOutputBuferring(); |
|
117 | + |
|
118 | + $handlerResponse = null; |
|
119 | + $handlerContentType = null; |
|
120 | 120 | |
121 | - try { |
|
122 | - foreach ($this->handlerStack as $handler) { |
|
123 | - $handler->setDebug($this); |
|
124 | - $handler->setException($exception); |
|
125 | - $handler->setSupervisor($supervisor); |
|
121 | + try { |
|
122 | + foreach ($this->handlerStack as $handler) { |
|
123 | + $handler->setDebug($this); |
|
124 | + $handler->setException($exception); |
|
125 | + $handler->setSupervisor($supervisor); |
|
126 | 126 | |
127 | - $handlerResponse = $handler->handle(); |
|
127 | + $handlerResponse = $handler->handle(); |
|
128 | 128 | |
129 | - // Collect the content type for possible sending in the headers |
|
130 | - $handlerContentType = method_exists($handler, 'contentType') ? $handler->contentType() : null; |
|
129 | + // Collect the content type for possible sending in the headers |
|
130 | + $handlerContentType = method_exists($handler, 'contentType') ? $handler->contentType() : null; |
|
131 | 131 | |
132 | - if (in_array($handlerResponse, [MainHandler::LAST_HANDLER, MainHandler::QUIT])) { |
|
133 | - break; |
|
134 | - } |
|
135 | - } |
|
132 | + if (in_array($handlerResponse, [MainHandler::LAST_HANDLER, MainHandler::QUIT])) { |
|
133 | + break; |
|
134 | + } |
|
135 | + } |
|
136 | 136 | |
137 | - $Quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit(); |
|
138 | - } |
|
139 | - finally { |
|
140 | - // Returns the contents of the output buffer |
|
141 | - $output = $this->system->CleanOutputBuffer(); |
|
142 | - } |
|
143 | - |
|
144 | - // Returns the contents of the output buffer for loading time of page |
|
145 | - $totalTime = $this->benchmark->getElapsedTime('total_execution'); |
|
146 | - $output = str_replace('{elapsed_time}', $totalTime, $output); |
|
147 | - |
|
148 | - if ($this->writeToOutput()) { |
|
149 | - if ($Quit) { |
|
150 | - while ($this->system->getOutputBufferLevel() > 0) { |
|
151 | - // Cleanes the output buffer |
|
152 | - $this->system->endOutputBuffering(); |
|
153 | - } |
|
154 | - |
|
155 | - if (Misc::sendHeaders() && $handlerContentType) { |
|
156 | - header("Content-Type: {$handlerContentType}"); |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - $this->writeToOutputBuffer($output); |
|
161 | - } |
|
162 | - |
|
163 | - if ($Quit) { |
|
164 | - $this->system->flushOutputBuffer(); |
|
165 | - $this->system->stopException(1); |
|
166 | - } |
|
167 | - |
|
168 | - return $output; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * {@inheritdoc} |
|
173 | - */ |
|
174 | - public function allowQuit($exit = null) |
|
175 | - { |
|
176 | - if (func_num_args() == 0) { |
|
177 | - return $this->allowQuit; |
|
178 | - } |
|
179 | - |
|
180 | - return $this->allowQuit = (bool) $exit; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * {@inheritdoc} |
|
185 | - */ |
|
186 | - public function writeToOutput($send = null) |
|
187 | - { |
|
188 | - if (func_num_args() == 0) { |
|
189 | - return $this->sendOutput; |
|
190 | - } |
|
137 | + $Quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit(); |
|
138 | + } |
|
139 | + finally { |
|
140 | + // Returns the contents of the output buffer |
|
141 | + $output = $this->system->CleanOutputBuffer(); |
|
142 | + } |
|
143 | + |
|
144 | + // Returns the contents of the output buffer for loading time of page |
|
145 | + $totalTime = $this->benchmark->getElapsedTime('total_execution'); |
|
146 | + $output = str_replace('{elapsed_time}', $totalTime, $output); |
|
147 | + |
|
148 | + if ($this->writeToOutput()) { |
|
149 | + if ($Quit) { |
|
150 | + while ($this->system->getOutputBufferLevel() > 0) { |
|
151 | + // Cleanes the output buffer |
|
152 | + $this->system->endOutputBuffering(); |
|
153 | + } |
|
154 | + |
|
155 | + if (Misc::sendHeaders() && $handlerContentType) { |
|
156 | + header("Content-Type: {$handlerContentType}"); |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + $this->writeToOutputBuffer($output); |
|
161 | + } |
|
162 | + |
|
163 | + if ($Quit) { |
|
164 | + $this->system->flushOutputBuffer(); |
|
165 | + $this->system->stopException(1); |
|
166 | + } |
|
167 | + |
|
168 | + return $output; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * {@inheritdoc} |
|
173 | + */ |
|
174 | + public function allowQuit($exit = null) |
|
175 | + { |
|
176 | + if (func_num_args() == 0) { |
|
177 | + return $this->allowQuit; |
|
178 | + } |
|
179 | + |
|
180 | + return $this->allowQuit = (bool) $exit; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * {@inheritdoc} |
|
185 | + */ |
|
186 | + public function writeToOutput($send = null) |
|
187 | + { |
|
188 | + if (func_num_args() == 0) { |
|
189 | + return $this->sendOutput; |
|
190 | + } |
|
191 | 191 | |
192 | - return $this->sendOutput = (bool) $send; |
|
193 | - } |
|
192 | + return $this->sendOutput = (bool) $send; |
|
193 | + } |
|
194 | 194 | |
195 | - /** |
|
196 | - * Generate output to the browser. |
|
197 | - * |
|
198 | - * @param string $output |
|
199 | - * |
|
200 | - * @return self |
|
201 | - */ |
|
202 | - protected function writeToOutputBuffer($output): self |
|
203 | - { |
|
204 | - if ($this->sendHttpCode() && Misc::sendHeaders()) { |
|
205 | - $this->system->setHttpResponseCode($this->sendHttpCode()); |
|
206 | - } |
|
195 | + /** |
|
196 | + * Generate output to the browser. |
|
197 | + * |
|
198 | + * @param string $output |
|
199 | + * |
|
200 | + * @return self |
|
201 | + */ |
|
202 | + protected function writeToOutputBuffer($output): self |
|
203 | + { |
|
204 | + if ($this->sendHttpCode() && Misc::sendHeaders()) { |
|
205 | + $this->system->setHttpResponseCode($this->sendHttpCode()); |
|
206 | + } |
|
207 | 207 | |
208 | - echo $output; |
|
208 | + echo $output; |
|
209 | 209 | |
210 | - return $this; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * {@inheritdoc} |
|
215 | - */ |
|
216 | - public function handleError( |
|
217 | - int $level, |
|
218 | - string $message, |
|
219 | - string $file = null, |
|
220 | - int $line = null |
|
221 | - ) { |
|
222 | - if ($level & $this->system->getErrorReportingLevel()) { |
|
223 | - $exception = new ErrorException($message, $level, $level, $file, $line); |
|
224 | - |
|
225 | - if ($this->throwExceptions) { |
|
226 | - throw $exception; |
|
227 | - } else { |
|
228 | - $this->handleException($exception); |
|
229 | - } |
|
230 | - |
|
231 | - return true; |
|
232 | - } |
|
233 | - |
|
234 | - return false; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * {@inheritdoc} |
|
239 | - */ |
|
240 | - public function pushHandler($handler) |
|
241 | - { |
|
242 | - return $this->prependHandler($handler); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * {@inheritdoc} |
|
247 | - */ |
|
248 | - public function appendHandler($handler): self |
|
249 | - { |
|
250 | - array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
251 | - |
|
252 | - return $this; |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * {@inheritdoc} |
|
257 | - */ |
|
258 | - public function prependHandler($handler): self |
|
259 | - { |
|
260 | - array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
261 | - |
|
262 | - return $this; |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Create a CallbackHandler from callable and throw if handler is invalid. |
|
267 | - * |
|
268 | - * @param \Callable|\Syscodes\Components\Contracts\Debug\Handler $handler |
|
269 | - * |
|
270 | - * @return \Syscodes\Components\Contracts\Debug\Handler |
|
271 | - * |
|
272 | - * @throws \InvalidArgumentException If argument is not callable or instance of \Syscodes\Components\Contracts\Debug\Handler |
|
273 | - */ |
|
274 | - protected function resolveHandler($handler) |
|
275 | - { |
|
276 | - if (is_callable($handler)) { |
|
277 | - $handler = new CallbackHandler($handler); |
|
278 | - } |
|
279 | - |
|
280 | - if ( ! $handler instanceof MainHandler) { |
|
281 | - throw new InvalidArgumentException( |
|
282 | - "Argument to " . __METHOD__ . " must be a callable, or instance of ". |
|
283 | - "Syscodes\Components\\Contracts\\Debug\\Handler" |
|
284 | - ); |
|
285 | - } |
|
286 | - |
|
287 | - return $handler; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * {@inheritdoc} |
|
292 | - */ |
|
293 | - public function getHandlers(): array |
|
294 | - { |
|
295 | - return $this->handlerStack; |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * {@inheritdoc} |
|
300 | - */ |
|
301 | - public function clearHandlers(): self |
|
302 | - { |
|
303 | - $this->handlerStack = []; |
|
304 | - |
|
305 | - return $this; |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * {@inheritdoc} |
|
310 | - */ |
|
311 | - public function popHandler() |
|
312 | - { |
|
313 | - return array_pop($this->handlerStack); |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Gets supervisor already specified. |
|
318 | - * |
|
319 | - * @param \Throwable $exception |
|
320 | - * |
|
321 | - * @return \Syscodes\Components\Debug\Engine\Supervisor |
|
322 | - */ |
|
323 | - protected function getSupervisor(Throwable $exception) |
|
324 | - { |
|
325 | - return new Supervisor($exception); |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * {@inheritdoc} |
|
330 | - */ |
|
331 | - public function off(): void |
|
332 | - { |
|
333 | - $this->system->restoreExceptionHandler(); |
|
334 | - $this->system->restoreErrorHandler(); |
|
335 | - } |
|
210 | + return $this; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * {@inheritdoc} |
|
215 | + */ |
|
216 | + public function handleError( |
|
217 | + int $level, |
|
218 | + string $message, |
|
219 | + string $file = null, |
|
220 | + int $line = null |
|
221 | + ) { |
|
222 | + if ($level & $this->system->getErrorReportingLevel()) { |
|
223 | + $exception = new ErrorException($message, $level, $level, $file, $line); |
|
224 | + |
|
225 | + if ($this->throwExceptions) { |
|
226 | + throw $exception; |
|
227 | + } else { |
|
228 | + $this->handleException($exception); |
|
229 | + } |
|
230 | + |
|
231 | + return true; |
|
232 | + } |
|
233 | + |
|
234 | + return false; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * {@inheritdoc} |
|
239 | + */ |
|
240 | + public function pushHandler($handler) |
|
241 | + { |
|
242 | + return $this->prependHandler($handler); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * {@inheritdoc} |
|
247 | + */ |
|
248 | + public function appendHandler($handler): self |
|
249 | + { |
|
250 | + array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
251 | + |
|
252 | + return $this; |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * {@inheritdoc} |
|
257 | + */ |
|
258 | + public function prependHandler($handler): self |
|
259 | + { |
|
260 | + array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
261 | + |
|
262 | + return $this; |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Create a CallbackHandler from callable and throw if handler is invalid. |
|
267 | + * |
|
268 | + * @param \Callable|\Syscodes\Components\Contracts\Debug\Handler $handler |
|
269 | + * |
|
270 | + * @return \Syscodes\Components\Contracts\Debug\Handler |
|
271 | + * |
|
272 | + * @throws \InvalidArgumentException If argument is not callable or instance of \Syscodes\Components\Contracts\Debug\Handler |
|
273 | + */ |
|
274 | + protected function resolveHandler($handler) |
|
275 | + { |
|
276 | + if (is_callable($handler)) { |
|
277 | + $handler = new CallbackHandler($handler); |
|
278 | + } |
|
279 | + |
|
280 | + if ( ! $handler instanceof MainHandler) { |
|
281 | + throw new InvalidArgumentException( |
|
282 | + "Argument to " . __METHOD__ . " must be a callable, or instance of ". |
|
283 | + "Syscodes\Components\\Contracts\\Debug\\Handler" |
|
284 | + ); |
|
285 | + } |
|
286 | + |
|
287 | + return $handler; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * {@inheritdoc} |
|
292 | + */ |
|
293 | + public function getHandlers(): array |
|
294 | + { |
|
295 | + return $this->handlerStack; |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * {@inheritdoc} |
|
300 | + */ |
|
301 | + public function clearHandlers(): self |
|
302 | + { |
|
303 | + $this->handlerStack = []; |
|
304 | + |
|
305 | + return $this; |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * {@inheritdoc} |
|
310 | + */ |
|
311 | + public function popHandler() |
|
312 | + { |
|
313 | + return array_pop($this->handlerStack); |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Gets supervisor already specified. |
|
318 | + * |
|
319 | + * @param \Throwable $exception |
|
320 | + * |
|
321 | + * @return \Syscodes\Components\Debug\Engine\Supervisor |
|
322 | + */ |
|
323 | + protected function getSupervisor(Throwable $exception) |
|
324 | + { |
|
325 | + return new Supervisor($exception); |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * {@inheritdoc} |
|
330 | + */ |
|
331 | + public function off(): void |
|
332 | + { |
|
333 | + $this->system->restoreExceptionHandler(); |
|
334 | + $this->system->restoreErrorHandler(); |
|
335 | + } |
|
336 | 336 | |
337 | - /** |
|
338 | - * {@inheritdoc} |
|
339 | - */ |
|
340 | - public function on() : void |
|
341 | - { |
|
342 | - // Set the exception handler |
|
343 | - $this->system->setExceptionHandler([$this, self::EXCEPTION_HANDLER]); |
|
344 | - // Set the error handler |
|
345 | - $this->system->setErrorHandler([$this, self::ERROR_HANDLER]); |
|
346 | - // Set the handler for shutdown to catch Parse errors |
|
347 | - $this->system->registerShutdownFunction([$this, self::SHUTDOWN_HANDLER]); |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * {@inheritdoc} |
|
352 | - */ |
|
353 | - public function sendHttpCode($code = null) |
|
354 | - { |
|
355 | - if (func_num_args() == 0) { |
|
356 | - return $this->sendHttpCode; |
|
357 | - } |
|
337 | + /** |
|
338 | + * {@inheritdoc} |
|
339 | + */ |
|
340 | + public function on() : void |
|
341 | + { |
|
342 | + // Set the exception handler |
|
343 | + $this->system->setExceptionHandler([$this, self::EXCEPTION_HANDLER]); |
|
344 | + // Set the error handler |
|
345 | + $this->system->setErrorHandler([$this, self::ERROR_HANDLER]); |
|
346 | + // Set the handler for shutdown to catch Parse errors |
|
347 | + $this->system->registerShutdownFunction([$this, self::SHUTDOWN_HANDLER]); |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * {@inheritdoc} |
|
352 | + */ |
|
353 | + public function sendHttpCode($code = null) |
|
354 | + { |
|
355 | + if (func_num_args() == 0) { |
|
356 | + return $this->sendHttpCode; |
|
357 | + } |
|
358 | 358 | |
359 | - if ( ! $code) { |
|
360 | - return $this->sendHttpCode = false; |
|
361 | - } |
|
359 | + if ( ! $code) { |
|
360 | + return $this->sendHttpCode = false; |
|
361 | + } |
|
362 | 362 | |
363 | - if ($code === true) { |
|
364 | - $code = 500; |
|
365 | - } |
|
363 | + if ($code === true) { |
|
364 | + $code = 500; |
|
365 | + } |
|
366 | 366 | |
367 | - if ($code < 400 || 600 <= $code) { |
|
368 | - throw new InvalidArgumentException("Invalid status code {$code}, must be 4xx or 5xx"); |
|
369 | - } |
|
367 | + if ($code < 400 || 600 <= $code) { |
|
368 | + throw new InvalidArgumentException("Invalid status code {$code}, must be 4xx or 5xx"); |
|
369 | + } |
|
370 | 370 | |
371 | - return $this->sendHttpCode = $code; |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * {@inheritdoc} |
|
376 | - */ |
|
377 | - public function handleShutdown() |
|
378 | - { |
|
379 | - $this->throwExceptions = false; |
|
380 | - |
|
381 | - $error = $this->system->getLastError(); |
|
382 | - |
|
383 | - // If we've got an error that hasn't been displayed, then convert |
|
384 | - // it to an Exception and use the Exception handler to display it |
|
385 | - // to the user |
|
386 | - if ($error && Misc::isFatalError($error['type'])) { |
|
387 | - $this->handleError($error['type'], $error['message'], $error['file'], $error['line']); |
|
388 | - } |
|
389 | - } |
|
371 | + return $this->sendHttpCode = $code; |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * {@inheritdoc} |
|
376 | + */ |
|
377 | + public function handleShutdown() |
|
378 | + { |
|
379 | + $this->throwExceptions = false; |
|
380 | + |
|
381 | + $error = $this->system->getLastError(); |
|
382 | + |
|
383 | + // If we've got an error that hasn't been displayed, then convert |
|
384 | + // it to an Exception and use the Exception handler to display it |
|
385 | + // to the user |
|
386 | + if ($error && Misc::isFatalError($error['type'])) { |
|
387 | + $this->handleError($error['type'], $error['message'], $error['file'], $error['line']); |
|
388 | + } |
|
389 | + } |
|
390 | 390 | } |
391 | 391 | \ No newline at end of file |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | |
160 | 160 | $caughtLength = $this->caughtLength = 0; |
161 | 161 | |
162 | - ob_start(function ($buffer) { |
|
162 | + ob_start(function($buffer) { |
|
163 | 163 | $this->caughtBuffer = $buffer; |
164 | 164 | |
165 | 165 | return ''; |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | $this->sendPhpResponse($exception); |
169 | 169 | |
170 | 170 | if (isset($this->caughtBuffer[0])) { |
171 | - ob_start(function ($buffer) { |
|
171 | + ob_start(function($buffer) { |
|
172 | 172 | if ($this->caughtLength) { |
173 | 173 | $cleanBuffer = substr_replace($buffer, '', 0, $this->caughtLength); |
174 | 174 | |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | } |
459 | 459 | |
460 | 460 | if (is_string($frmt)) { |
461 | - $index = strpos($f = $frmt, '&', max(strrpos($f, '%f'), strrpos($f, '%l')) ?: strlen($f)); |
|
461 | + $index = strpos($f = $frmt, '&', max(strrpos($f, '%f'), strrpos($f, '%l')) ?: strlen($f)); |
|
462 | 462 | $frmt = [substr($f, 0, $index)] + preg_split('/&([^>]++)>/', substr($f, $index), -1, PREG_SPLIT_DELIM_CAPTURE); |
463 | 463 | |
464 | 464 | for ($index = 1; isset($frmt[$index]); ++$index) { |