|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Persistence\Mapping\Driver; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata; |
|
8
|
|
|
use Doctrine\Persistence\Mapping\MappingException; |
|
9
|
|
|
use function array_keys; |
|
10
|
|
|
use function array_merge; |
|
11
|
|
|
use function array_unique; |
|
12
|
|
|
use function is_file; |
|
13
|
|
|
use function str_replace; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Base driver for file-based metadata drivers. |
|
17
|
|
|
* |
|
18
|
|
|
* A file driver operates in a mode where it loads the mapping files of individual |
|
19
|
|
|
* classes on demand. This requires the user to adhere to the convention of 1 mapping |
|
20
|
|
|
* file per class and the file names of the mapping files must correspond to the full |
|
21
|
|
|
* class name, including namespace, with the namespace delimiters '\', replaced by dots '.'. |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class FileDriver implements MappingDriver |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var FileLocator */ |
|
26
|
|
|
protected $locator; |
|
27
|
|
|
|
|
28
|
|
|
/** @var ClassMetadata[]|null */ |
|
29
|
|
|
protected $classCache; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
protected $globalBasename = ''; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Initializes a new FileDriver that looks in the given path(s) for mapping |
|
36
|
|
|
* documents and operates in the specified operating mode. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string|array<int, string>|FileLocator $locator A FileLocator or one/multiple paths |
|
39
|
|
|
* where mapping documents can be found. |
|
40
|
|
|
*/ |
|
41
|
12 |
|
public function __construct($locator, ?string $fileExtension = null) |
|
42
|
|
|
{ |
|
43
|
12 |
|
if ($locator instanceof FileLocator) { |
|
44
|
9 |
|
$this->locator = $locator; |
|
45
|
|
|
} else { |
|
46
|
3 |
|
$this->locator = new DefaultFileLocator((array) $locator, $fileExtension); |
|
47
|
|
|
} |
|
48
|
12 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Sets the global basename. |
|
52
|
|
|
*/ |
|
53
|
6 |
|
public function setGlobalBasename(string $file) : void |
|
54
|
|
|
{ |
|
55
|
6 |
|
$this->globalBasename = $file; |
|
56
|
6 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Retrieves the global basename. |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function getGlobalBasename() : string |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->globalBasename; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Gets the element of schema meta data for the class from the mapping file. |
|
68
|
|
|
* This will lazily load the mapping file if it is not loaded yet. |
|
69
|
|
|
* |
|
70
|
|
|
* @return ClassMetadata The element of schema meta data. |
|
71
|
|
|
* |
|
72
|
|
|
* @throws MappingException |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function getElement(string $className) : ClassMetadata |
|
75
|
|
|
{ |
|
76
|
4 |
|
if ($this->classCache === null) { |
|
77
|
4 |
|
$this->initialize(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
if (isset($this->classCache[$className])) { |
|
81
|
2 |
|
return $this->classCache[$className]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
$result = $this->loadMappingFile($this->locator->findMappingFile($className)); |
|
85
|
|
|
|
|
86
|
3 |
|
if (! isset($result[$className])) { |
|
87
|
|
|
throw MappingException::invalidMappingFile( |
|
88
|
|
|
$className, |
|
89
|
|
|
str_replace('\\', '.', $className) . $this->locator->getFileExtension() |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
$this->classCache[$className] = $result[$className]; |
|
94
|
|
|
|
|
95
|
3 |
|
return $result[$className]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritDoc} |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function isTransient(string $className) : bool |
|
102
|
|
|
{ |
|
103
|
3 |
|
if ($this->classCache === null) { |
|
104
|
3 |
|
$this->initialize(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
3 |
|
if (isset($this->classCache[$className])) { |
|
108
|
1 |
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
3 |
|
return ! $this->locator->fileExists($className); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritDoc} |
|
116
|
|
|
*/ |
|
117
|
4 |
|
public function getAllClassNames() : array |
|
118
|
|
|
{ |
|
119
|
4 |
|
if ($this->classCache === null) { |
|
120
|
3 |
|
$this->initialize(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
4 |
|
if ($this->classCache === []) { |
|
124
|
1 |
|
return $this->locator->getAllClassNames($this->globalBasename); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** @var ClassMetadata[] $classCache */ |
|
128
|
3 |
|
$classCache = $this->classCache; |
|
129
|
|
|
|
|
130
|
|
|
/** @var string[] $keys */ |
|
131
|
3 |
|
$keys = array_keys($classCache); |
|
132
|
|
|
|
|
133
|
|
|
/** @var string[] $merged */ |
|
134
|
3 |
|
$merged = array_unique(array_merge( |
|
135
|
3 |
|
$keys, |
|
136
|
3 |
|
$this->locator->getAllClassNames($this->globalBasename) |
|
137
|
|
|
)); |
|
138
|
|
|
|
|
139
|
3 |
|
return $merged; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Loads a mapping file with the given name and returns a map |
|
144
|
|
|
* from class/entity names to their corresponding file driver elements. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $file The mapping file to load. |
|
147
|
|
|
* |
|
148
|
|
|
* @return ClassMetadata[] |
|
149
|
|
|
*/ |
|
150
|
|
|
abstract protected function loadMappingFile(string $file) : array; |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Initializes the class cache from all the global files. |
|
154
|
|
|
* |
|
155
|
|
|
* Using this feature adds a substantial performance hit to file drivers as |
|
156
|
|
|
* more metadata has to be loaded into memory than might actually be |
|
157
|
|
|
* necessary. This may not be relevant to scenarios where caching of |
|
158
|
|
|
* metadata is in place, however hits very hard in scenarios where no |
|
159
|
|
|
* caching is used. |
|
160
|
|
|
*/ |
|
161
|
10 |
|
protected function initialize() : void |
|
162
|
|
|
{ |
|
163
|
10 |
|
$this->classCache = []; |
|
164
|
10 |
|
if ($this->globalBasename === null) { |
|
165
|
|
|
return; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
10 |
|
foreach ($this->locator->getPaths() as $path) { |
|
169
|
10 |
|
$file = $path . '/' . $this->globalBasename . $this->locator->getFileExtension(); |
|
170
|
10 |
|
if (! is_file($file)) { |
|
171
|
5 |
|
continue; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
5 |
|
$this->classCache = array_merge( |
|
175
|
5 |
|
$this->classCache, |
|
176
|
5 |
|
$this->loadMappingFile($file) |
|
177
|
|
|
); |
|
178
|
|
|
} |
|
179
|
10 |
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Retrieves the locator used to discover mapping files by className. |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getLocator() : FileLocator |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->locator; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Sets the locator used to discover mapping files by className. |
|
191
|
|
|
*/ |
|
192
|
|
|
public function setLocator(FileLocator $locator) : void |
|
193
|
|
|
{ |
|
194
|
|
|
$this->locator = $locator; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|