|
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 1.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
|
|
|
* Construct the loader with the passed entity |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $entity The passed entity |
|
32
|
|
|
* @param bool $recursive Search the directories recursively |
|
33
|
|
|
*/ |
|
34
|
7 |
|
public function __construct($entity, $recursive) |
|
35
|
|
|
{ |
|
36
|
7 |
|
parent::__construct($entity); |
|
37
|
7 |
|
$this->recursive = $recursive; |
|
|
|
|
|
|
38
|
7 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
7 |
|
public function load() |
|
44
|
|
|
{ |
|
45
|
7 |
|
$paths = array(); |
|
46
|
7 |
|
$files = ($this->recursive) ? $this->getSupportedFilesRecursively() : $this->getSupportedFiles(); |
|
47
|
|
|
|
|
48
|
7 |
|
foreach ($files as $path => $file) { |
|
49
|
5 |
|
if ($file->isFile()) { |
|
50
|
5 |
|
$paths[] = $path; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
7 |
|
$this->content = $this->makeResources($paths); |
|
54
|
|
|
|
|
55
|
7 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns the supported files in the directory recursively |
|
60
|
|
|
* |
|
61
|
|
|
* @return \RegexIterator The supported files in the directories |
|
62
|
|
|
*/ |
|
63
|
2 |
|
private function getSupportedFilesRecursively() |
|
64
|
|
|
{ |
|
65
|
2 |
|
$dir_it = new \RecursiveDirectoryIterator($this->entity, \RecursiveDirectoryIterator::SKIP_DOTS); |
|
66
|
|
|
|
|
67
|
2 |
|
$files = new \RecursiveIteratorIterator( |
|
68
|
2 |
|
$dir_it, |
|
69
|
2 |
|
\RecursiveIteratorIterator::LEAVES_ONLY, |
|
70
|
2 |
|
\RecursiveIteratorIterator::CATCH_GET_CHILD |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
2 |
|
return $this->createRegexIterator($files); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns the supported files in the directory |
|
78
|
|
|
* |
|
79
|
|
|
* @return \RegexIterator The supported files in the directory |
|
80
|
|
|
*/ |
|
81
|
5 |
|
private function getSupportedFiles() |
|
82
|
|
|
{ |
|
83
|
5 |
|
$files = new \FilesystemIterator($this->entity); |
|
84
|
|
|
|
|
85
|
5 |
|
return $this->createRegexIterator($files); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the supported files in the directory |
|
90
|
|
|
* |
|
91
|
|
|
* @param \FilesystemIterator|\RecursiveIteratorIterator $files The found files in the directory/ies |
|
92
|
|
|
* |
|
93
|
|
|
* @return \RegexIterator The supported files in the directory using the regexiterator |
|
94
|
|
|
*/ |
|
95
|
7 |
|
private function createRegexIterator($files) |
|
96
|
|
|
{ |
|
97
|
7 |
|
return new \RegexIterator( |
|
98
|
7 |
|
$files, |
|
99
|
7 |
|
'/^.*\.(' . implode('|', static::$supported) . ')$/i' |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Makes usable resource paths from path strings |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $paths The path strings |
|
107
|
|
|
* |
|
108
|
|
|
* @return array|bool The usable resources if any, else false |
|
109
|
|
|
*/ |
|
110
|
7 |
|
private function makeResources($paths) |
|
111
|
|
|
{ |
|
112
|
7 |
|
if ($paths && !empty($paths)) { |
|
|
|
|
|
|
113
|
5 |
|
$resources = array(); |
|
114
|
|
|
|
|
115
|
5 |
|
foreach ($paths as $path) { |
|
116
|
5 |
|
$resources[] = $path; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
5 |
|
return $resources; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: