1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the m1\vars library |
5
|
|
|
* |
6
|
|
|
* (c) m1 <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @package m1/vars |
12
|
|
|
* @version 0.1.0 |
13
|
|
|
* @author Miles Croxford <[email protected]> |
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
15
|
|
|
* @license http://github.com/m1/vars/blob/master/LICENSE |
16
|
|
|
* @link http://github.com/m1/vars/blob/master/README.MD Documentation |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace M1\Vars\Loader; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This file provides dir loading support for Vars |
23
|
|
|
* |
24
|
|
|
* @since 0.1.1 |
25
|
|
|
*/ |
26
|
|
|
class DirectoryLoader extends AbstractLoader |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
4 |
|
public function load() |
32
|
|
|
{ |
33
|
4 |
|
$paths = array(); |
34
|
|
|
|
35
|
4 |
|
foreach ($this->getSupportedFiles() as $path => $file) { |
36
|
2 |
|
if ($file->isFile()) { |
37
|
2 |
|
$paths[] = $path; |
38
|
2 |
|
} |
39
|
4 |
|
} |
40
|
4 |
|
$this->content = $this->makeResources($paths); |
41
|
|
|
|
42
|
4 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns the supported files in the directory |
47
|
|
|
* |
48
|
|
|
* @return array The supported files in the directory |
49
|
|
|
*/ |
50
|
4 |
|
private function getSupportedFiles() |
51
|
|
|
{ |
52
|
4 |
|
$dir_it = new \RecursiveDirectoryIterator($this->entity, \RecursiveDirectoryIterator::SKIP_DOTS); |
53
|
|
|
|
54
|
4 |
|
$dir_files = new \RecursiveIteratorIterator( |
55
|
4 |
|
$dir_it, |
56
|
4 |
|
\RecursiveIteratorIterator::LEAVES_ONLY, |
57
|
|
|
\RecursiveIteratorIterator::CATCH_GET_CHILD |
58
|
4 |
|
); |
59
|
|
|
|
60
|
4 |
|
$supported_files = new \RegexIterator( |
61
|
4 |
|
$dir_files, |
62
|
4 |
|
'/^.*\.(' . implode('|', static::$supported) . ')$/i' |
63
|
4 |
|
); |
64
|
|
|
|
65
|
4 |
|
return $supported_files; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Makes usable resource paths from path strings |
70
|
|
|
* |
71
|
|
|
* @param array $paths The path strings |
72
|
|
|
* |
73
|
|
|
* @return array|bool The usable resources if any, else false |
74
|
|
|
*/ |
75
|
4 |
|
private function makeResources($paths) |
76
|
|
|
{ |
77
|
4 |
|
if ($paths && !empty($paths)) { |
|
|
|
|
78
|
2 |
|
$resources = array(); |
79
|
|
|
|
80
|
2 |
|
foreach ($paths as $path) { |
81
|
2 |
|
$resources[] = $path; |
82
|
2 |
|
} |
83
|
|
|
|
84
|
2 |
|
return $resources; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.