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