1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Persistence\Mapping\Driver; |
4
|
|
|
|
5
|
|
|
use Doctrine\Persistence\Mapping\MappingException; |
6
|
|
|
use RecursiveDirectoryIterator; |
7
|
|
|
use RecursiveIteratorIterator; |
8
|
|
|
use const DIRECTORY_SEPARATOR; |
9
|
|
|
use function array_merge; |
10
|
|
|
use function array_unique; |
11
|
|
|
use function is_dir; |
12
|
|
|
use function is_file; |
13
|
|
|
use function str_replace; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Locates the file that contains the metadata information for a given class name. |
17
|
|
|
* |
18
|
|
|
* This behavior is independent of the actual content of the file. It just detects |
19
|
|
|
* the file which is responsible for the given class name. |
20
|
|
|
*/ |
21
|
|
|
class DefaultFileLocator implements FileLocator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The paths where to look for mapping files. |
25
|
|
|
* |
26
|
|
|
* @var string[] |
27
|
|
|
*/ |
28
|
|
|
protected $paths = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The file extension of mapping documents. |
32
|
|
|
* |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected $fileExtension; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initializes a new FileDriver that looks in the given path(s) for mapping |
39
|
|
|
* documents and operates in the specified operating mode. |
40
|
|
|
* |
41
|
|
|
* @param string|string[] $paths One or multiple paths where mapping documents can be found. |
42
|
|
|
* @param string|null $fileExtension The file extension of mapping documents, usually prefixed with a dot. |
43
|
|
|
*/ |
44
|
11 |
|
public function __construct($paths, $fileExtension = null) |
45
|
|
|
{ |
46
|
11 |
|
$this->addPaths((array) $paths); |
47
|
11 |
|
$this->fileExtension = $fileExtension; |
48
|
11 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Appends lookup paths to metadata driver. |
52
|
|
|
* |
53
|
|
|
* @param string[] $paths |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
11 |
|
public function addPaths(array $paths) |
58
|
|
|
{ |
59
|
11 |
|
$this->paths = array_unique(array_merge($this->paths, $paths)); |
60
|
11 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieves the defined metadata lookup paths. |
64
|
|
|
* |
65
|
|
|
* @return string[] |
66
|
|
|
*/ |
67
|
2 |
|
public function getPaths() |
68
|
|
|
{ |
69
|
2 |
|
return $this->paths; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets the file extension used to look for mapping files under. |
74
|
|
|
* |
75
|
|
|
* @return string|null |
76
|
|
|
*/ |
77
|
1 |
|
public function getFileExtension() |
78
|
|
|
{ |
79
|
1 |
|
return $this->fileExtension; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets the file extension used to look for mapping files under. |
84
|
|
|
* |
85
|
|
|
* @param string|null $fileExtension The file extension to set. |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
1 |
|
public function setFileExtension($fileExtension) |
90
|
|
|
{ |
91
|
1 |
|
$this->fileExtension = $fileExtension; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
3 |
|
public function findMappingFile($className) |
98
|
|
|
{ |
99
|
3 |
|
$fileName = str_replace('\\', '.', $className) . $this->fileExtension; |
100
|
|
|
|
101
|
|
|
// Check whether file exists |
102
|
3 |
|
foreach ($this->paths as $path) { |
103
|
3 |
|
if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) { |
104
|
3 |
|
return $path . DIRECTORY_SEPARATOR . $fileName; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
throw MappingException::mappingFileNotFound($className, $fileName); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritDoc} |
113
|
|
|
*/ |
114
|
2 |
|
public function getAllClassNames($globalBasename) |
115
|
|
|
{ |
116
|
2 |
|
$classes = []; |
117
|
|
|
|
118
|
2 |
|
if ($this->paths) { |
|
|
|
|
119
|
2 |
|
foreach ($this->paths as $path) { |
120
|
2 |
|
if (! is_dir($path)) { |
121
|
|
|
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
$iterator = new RecursiveIteratorIterator( |
125
|
2 |
|
new RecursiveDirectoryIterator($path), |
126
|
2 |
|
RecursiveIteratorIterator::LEAVES_ONLY |
127
|
|
|
); |
128
|
|
|
|
129
|
2 |
|
foreach ($iterator as $file) { |
130
|
2 |
|
$fileName = $file->getBasename($this->fileExtension); |
131
|
|
|
|
132
|
2 |
|
if ($fileName === $file->getBasename() || $fileName === $globalBasename) { |
133
|
2 |
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// NOTE: All files found here means classes are not transient! |
137
|
2 |
|
$classes[] = str_replace('.', '\\', $fileName); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
return $classes; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
*/ |
148
|
2 |
|
public function fileExists($className) |
149
|
|
|
{ |
150
|
2 |
|
$fileName = str_replace('\\', '.', $className) . $this->fileExtension; |
151
|
|
|
|
152
|
|
|
// Check whether file exists |
153
|
2 |
|
foreach ((array) $this->paths as $path) { |
154
|
2 |
|
if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) { |
155
|
2 |
|
return true; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
2 |
|
return false; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.