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