|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File locater |
|
4
|
|
|
* |
|
5
|
|
|
* @package SugiPHP.Config |
|
6
|
|
|
* @author Plamen Popov <[email protected]> |
|
7
|
|
|
* @license http://opensource.org/licenses/mit-license.php (MIT License) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SugiPHP\Config; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* File Locator searches for a file in registered search paths. |
|
14
|
|
|
*/ |
|
15
|
|
|
class FileLocator implements LocatorInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Search for a file in one or more directories. |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $paths; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* File Locator creator. |
|
25
|
|
|
* |
|
26
|
|
|
* @param array|string $paths |
|
27
|
|
|
* |
|
28
|
|
|
* @return void |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($paths) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->addPath($paths); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function locate($resource) |
|
39
|
|
|
{ |
|
40
|
|
|
// empty string |
|
41
|
|
|
if (empty($resource)) { |
|
42
|
|
|
return ; |
|
43
|
|
|
} |
|
44
|
|
|
if ($this->isFullPath($resource)) { |
|
45
|
|
|
if (is_file($resource)) { |
|
46
|
|
|
return $resource; |
|
47
|
|
|
} |
|
48
|
|
|
} else { |
|
49
|
|
|
foreach ($this->paths as $path) { |
|
50
|
|
|
$file = "{$path}{$resource}"; |
|
51
|
|
|
if (is_file($file)) { |
|
52
|
|
|
return $file; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Adds a search paths. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string|array $path or several paths |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function addPath($path) |
|
66
|
|
|
{ |
|
67
|
|
|
$paths = (array) $path; |
|
68
|
|
|
foreach ($paths as $path) { |
|
69
|
|
|
$this->paths[] = rtrim($path, "\\/") . DIRECTORY_SEPARATOR; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Remove last search path. |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function popPath() |
|
79
|
|
|
{ |
|
80
|
|
|
array_pop($this->paths); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @deprecated Use unshiftPath() method |
|
85
|
|
|
*/ |
|
86
|
|
|
public function prependPath($path) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->unshiftPath($path); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Prepends one path to the beginning of the search paths. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $path |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function unshiftPath($path) |
|
99
|
|
|
{ |
|
100
|
|
|
array_unshift($this->paths, rtrim($path, "\\/") . DIRECTORY_SEPARATOR); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Remove first path from the search paths. |
|
105
|
|
|
* |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
|
|
public function shiftPath() |
|
109
|
|
|
{ |
|
110
|
|
|
array_shift($this->paths); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Returns all registered search paths. |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getPaths() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->paths; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Check if the file/path is given with absolute path. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $path |
|
127
|
|
|
* |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function isFullPath($path) |
|
131
|
|
|
{ |
|
132
|
|
|
// *nix style |
|
133
|
|
|
if ($path[0] == "/") { |
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// windows style |
|
138
|
|
|
if (preg_match("#[A-Z]:\\.+#U", $path)) { |
|
139
|
|
|
return true; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.