1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cubiche\Core\Metadata\Locator; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Metadata\Exception\MappingException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* DefaultFileLocator class. |
17
|
|
|
* |
18
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class DefaultFileLocator implements FileLocatorInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $paths = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $prefixes = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $separator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* @param array $prefixes |
41
|
|
|
* @param string $separator |
42
|
|
|
*/ |
43
|
|
|
public function __construct(array $prefixes, $separator = '.') |
44
|
|
|
{ |
45
|
|
|
$this->addNamespacePrefixes($prefixes); |
46
|
|
|
if (empty($separator)) { |
47
|
|
|
throw new \InvalidArgumentException('The separator should not be empty'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->separator = (string) $separator; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Adds Namespace Prefixes. |
55
|
|
|
* |
56
|
|
|
* @param array $prefixes |
57
|
|
|
*/ |
58
|
|
|
public function addNamespacePrefixes(array $prefixes) |
59
|
|
|
{ |
60
|
|
|
$this->prefixes = array_merge($this->prefixes, $prefixes); |
61
|
|
|
$this->paths = array_merge($this->paths, array_keys($prefixes)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function prefixes() |
68
|
|
|
{ |
69
|
|
|
return $this->prefixes; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function paths() |
76
|
|
|
{ |
77
|
|
|
return $this->paths; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function getAllClassNames($extension) |
84
|
|
|
{ |
85
|
|
|
$classes = []; |
86
|
|
|
|
87
|
|
|
if ($this->paths) { |
|
|
|
|
88
|
|
|
foreach ((array) $this->paths as $path) { |
89
|
|
|
if (!is_dir($path)) { |
90
|
|
|
throw MappingException::invalidDirectory($path); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$iterator = new \RecursiveIteratorIterator( |
94
|
|
|
new \RecursiveDirectoryIterator($path), |
95
|
|
|
\RecursiveIteratorIterator::LEAVES_ONLY |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
foreach ($iterator as $file) { |
99
|
|
|
$fileName = $file->getBasename($extension); |
100
|
|
|
if ($fileName == $file->getBasename()) { |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$nsSuffix = strtr( |
105
|
|
|
substr(realpath($file->getPath()), strlen(realpath($path))), |
106
|
|
|
$this->separator, |
107
|
|
|
'\\' |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$classes[] = $this->prefixes[$path]. |
111
|
|
|
str_replace(DIRECTORY_SEPARATOR, '\\', $nsSuffix).'\\'. |
112
|
|
|
str_replace($this->separator, '\\', $fileName) |
113
|
|
|
; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $classes; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function findMappingFile($className, $extension) |
125
|
|
|
{ |
126
|
|
|
foreach ($this->paths as $path) { |
127
|
|
|
$prefix = $this->prefixes[$path]; |
128
|
|
|
|
129
|
|
|
if (0 !== strpos($className, $prefix.'\\')) { |
130
|
|
|
continue; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$filename = $path.'/'.strtr(substr($className, strlen($prefix) + 1), '\\', $this->separator).$extension; |
134
|
|
|
if (is_file($filename)) { |
135
|
|
|
return $filename; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.