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