1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\PersistentCollection; |
21
|
|
|
|
22
|
|
|
use Doctrine\ODM\MongoDB\Configuration; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Default generator for custom PersistentCollection classes. |
26
|
|
|
* |
27
|
|
|
* @since 1.1 |
28
|
|
|
*/ |
29
|
|
|
final class DefaultPersistentCollectionGenerator implements PersistentCollectionGenerator |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The namespace that contains all persistent collection classes. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $collectionNamespace; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The directory that contains all persistent collection classes. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $collectionDir; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $collectionDir |
47
|
|
|
* @param string $collectionNs |
48
|
|
|
*/ |
49
|
11 |
|
public function __construct($collectionDir, $collectionNs) |
50
|
|
|
{ |
51
|
11 |
|
$this->collectionDir = $collectionDir; |
52
|
11 |
|
$this->collectionNamespace = $collectionNs; |
53
|
11 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function generateClass($class, $dir) |
59
|
|
|
{ |
60
|
|
|
$collClassName = str_replace('\\', '', $class) . 'Persistent'; |
61
|
|
|
$className = $this->collectionNamespace . '\\' . $collClassName; |
62
|
|
|
$fileName = $dir . DIRECTORY_SEPARATOR . $collClassName . '.php'; |
63
|
|
|
$this->generateCollectionClass($class, $className, $fileName); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
10 |
|
public function loadClass($collectionClass, $autoGenerate) |
70
|
|
|
{ |
71
|
|
|
// These checks are not in __construct() because of BC and should be moved for 2.0 |
72
|
10 |
|
if ( ! $this->collectionDir) { |
73
|
|
|
throw PersistentCollectionException::directoryRequired(); |
74
|
|
|
} |
75
|
10 |
|
if ( ! $this->collectionNamespace) { |
76
|
|
|
throw PersistentCollectionException::namespaceRequired(); |
77
|
|
|
} |
78
|
|
|
|
79
|
10 |
|
$collClassName = str_replace('\\', '', $collectionClass) . 'Persistent'; |
80
|
10 |
|
$className = $this->collectionNamespace . '\\' . $collClassName; |
81
|
10 |
View Code Duplication |
if ( ! class_exists($className, false)) { |
|
|
|
|
82
|
6 |
|
$fileName = $this->collectionDir . DIRECTORY_SEPARATOR . $collClassName . '.php'; |
83
|
|
|
switch ($autoGenerate) { |
84
|
6 |
|
case Configuration::AUTOGENERATE_NEVER: |
85
|
|
|
require $fileName; |
86
|
|
|
break; |
87
|
|
|
|
88
|
6 |
|
case Configuration::AUTOGENERATE_ALWAYS: |
89
|
3 |
|
$this->generateCollectionClass($collectionClass, $className, $fileName); |
90
|
3 |
|
require $fileName; |
91
|
3 |
|
break; |
92
|
|
|
|
93
|
3 |
|
case Configuration::AUTOGENERATE_FILE_NOT_EXISTS: |
94
|
|
|
if ( ! file_exists($fileName)) { |
95
|
|
|
$this->generateCollectionClass($collectionClass, $className, $fileName); |
96
|
|
|
} |
97
|
|
|
require $fileName; |
98
|
|
|
break; |
99
|
|
|
|
100
|
3 |
|
case Configuration::AUTOGENERATE_EVAL: |
101
|
3 |
|
$this->generateCollectionClass($collectionClass, $className, false); |
102
|
3 |
|
break; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
10 |
|
return $className; |
107
|
|
|
} |
108
|
|
|
|
109
|
6 |
|
private function generateCollectionClass($for, $targetFqcn, $fileName) |
110
|
|
|
{ |
111
|
6 |
|
$exploded = explode('\\', $targetFqcn); |
112
|
6 |
|
$class = array_pop($exploded); |
113
|
6 |
|
$namespace = join('\\', $exploded); |
114
|
|
|
$code = <<<CODE |
115
|
|
|
<?php |
116
|
|
|
|
117
|
6 |
|
namespace $namespace; |
118
|
|
|
|
119
|
|
|
use Doctrine\Common\Collections\Collection as BaseCollection; |
120
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
121
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
122
|
|
|
use Doctrine\ODM\MongoDB\MongoDBException; |
123
|
|
|
use Doctrine\ODM\MongoDB\UnitOfWork; |
124
|
|
|
use Doctrine\ODM\MongoDB\Utility\CollectionHelper; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* DO NOT EDIT THIS FILE - IT WAS CREATED BY DOCTRINE\'S PERSISTENT COLLECTION GENERATOR |
128
|
|
|
*/ |
129
|
6 |
|
class $class extends \\$for implements \\Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionInterface |
130
|
|
|
{ |
131
|
|
|
use \\Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionTrait; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param BaseCollection \$coll |
135
|
|
|
* @param DocumentManager \$dm |
136
|
|
|
* @param UnitOfWork \$uow |
137
|
|
|
*/ |
138
|
|
|
public function __construct(BaseCollection \$coll, DocumentManager \$dm, UnitOfWork \$uow) |
139
|
|
|
{ |
140
|
|
|
\$this->coll = \$coll; |
141
|
|
|
\$this->dm = \$dm; |
142
|
|
|
\$this->uow = \$uow; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
CODE; |
146
|
6 |
|
$rc = new \ReflectionClass($for); |
147
|
6 |
|
$rt = new \ReflectionClass('Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionTrait'); |
148
|
6 |
|
foreach ($rc->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
149
|
|
|
if ( |
150
|
6 |
|
$rt->hasMethod($method->name) || |
151
|
6 |
|
$method->isConstructor() || |
152
|
6 |
|
$method->isFinal() || |
153
|
6 |
|
$method->isStatic() |
154
|
|
|
) { |
155
|
6 |
|
continue; |
156
|
|
|
} |
157
|
6 |
|
$code .= $this->generateMethod($method); |
158
|
|
|
} |
159
|
6 |
|
$code .= "}\n"; |
160
|
|
|
|
161
|
6 |
|
if ($fileName === false) { |
162
|
3 |
|
if ( ! class_exists($targetFqcn)) { |
163
|
3 |
|
eval(substr($code, 5)); |
164
|
|
|
} |
165
|
|
View Code Duplication |
} else { |
|
|
|
|
166
|
3 |
|
$parentDirectory = dirname($fileName); |
167
|
|
|
|
168
|
3 |
|
if ( ! is_dir($parentDirectory) && (false === @mkdir($parentDirectory, 0775, true))) { |
169
|
|
|
throw PersistentCollectionException::directoryNotWritable(); |
170
|
|
|
} |
171
|
|
|
|
172
|
3 |
|
if ( ! is_writable($parentDirectory)) { |
173
|
|
|
throw PersistentCollectionException::directoryNotWritable(); |
174
|
|
|
} |
175
|
|
|
|
176
|
3 |
|
$tmpFileName = $fileName . '.' . uniqid('', true); |
177
|
3 |
|
file_put_contents($tmpFileName, $code); |
178
|
3 |
|
rename($tmpFileName, $fileName); |
179
|
|
|
} |
180
|
6 |
|
} |
181
|
|
|
|
182
|
6 |
|
private function generateMethod(\ReflectionMethod $method) |
183
|
|
|
{ |
184
|
6 |
|
$parametersString = $this->buildParametersString($method); |
185
|
6 |
|
$callParamsString = implode(', ', $this->getParameterNamesForDecoratedCall($method->getParameters())); |
186
|
|
|
|
187
|
|
|
$method = <<<CODE |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritDoc} |
191
|
|
|
*/ |
192
|
6 |
|
public function {$method->name}($parametersString){$this->getMethodReturnType($method)} |
193
|
|
|
{ |
194
|
|
|
\$this->initialize(); |
195
|
|
|
if (\$this->needsSchedulingForDirtyCheck()) { |
196
|
|
|
\$this->changed(); |
197
|
|
|
} |
198
|
6 |
|
return \$this->coll->{$method->name}($callParamsString); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
CODE; |
202
|
6 |
|
return $method; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param \ReflectionMethod $method |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
6 |
|
private function buildParametersString(\ReflectionMethod $method) |
211
|
|
|
{ |
212
|
6 |
|
$parameters = $method->getParameters(); |
213
|
6 |
|
$parameterDefinitions = array(); |
214
|
|
|
|
215
|
|
|
/* @var $param \ReflectionParameter */ |
216
|
6 |
|
foreach ($parameters as $param) { |
217
|
6 |
|
$parameterDefinition = ''; |
218
|
|
|
|
219
|
6 |
|
if ($parameterType = $this->getParameterType($param)) { |
|
|
|
|
220
|
6 |
|
$parameterDefinition .= $parameterType . ' '; |
221
|
|
|
} |
222
|
|
|
|
223
|
6 |
|
if ($param->isPassedByReference()) { |
224
|
|
|
$parameterDefinition .= '&'; |
225
|
|
|
} |
226
|
|
|
|
227
|
6 |
|
if (method_exists($param, 'isVariadic')) { |
228
|
6 |
|
if ($param->isVariadic()) { |
229
|
|
|
$parameterDefinition .= '...'; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
6 |
|
$parameters[] = '$' . $param->name; |
234
|
6 |
|
$parameterDefinition .= '$' . $param->name; |
235
|
|
|
|
236
|
6 |
|
if ($param->isDefaultValueAvailable()) { |
237
|
|
|
$parameterDefinition .= ' = ' . var_export($param->getDefaultValue(), true); |
238
|
|
|
} |
239
|
|
|
|
240
|
6 |
|
$parameterDefinitions[] = $parameterDefinition; |
241
|
|
|
} |
242
|
|
|
|
243
|
6 |
|
return implode(', ', $parameterDefinitions); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param \ReflectionParameter $parameter |
248
|
|
|
* |
249
|
|
|
* @return string|null |
250
|
|
|
*/ |
251
|
6 |
|
private function getParameterType(\ReflectionParameter $parameter) |
252
|
|
|
{ |
253
|
|
|
// We need to pick the type hint class too |
254
|
6 |
|
if ($parameter->isArray()) { |
255
|
|
|
return 'array'; |
256
|
|
|
} |
257
|
|
|
|
258
|
6 |
|
if (method_exists($parameter, 'isCallable') && $parameter->isCallable()) { |
259
|
|
|
return 'callable'; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
try { |
263
|
6 |
|
$parameterClass = $parameter->getClass(); |
264
|
|
|
|
265
|
6 |
|
if ($parameterClass) { |
266
|
6 |
|
return '\\' . $parameterClass->name; |
267
|
|
|
} |
268
|
|
|
} catch (\ReflectionException $previous) { |
269
|
|
|
// @todo ProxyGenerator throws specialized exceptions |
270
|
|
|
throw $previous; |
271
|
|
|
} |
272
|
|
|
|
273
|
2 |
|
return null; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param \ReflectionParameter[] $parameters |
278
|
|
|
* |
279
|
|
|
* @return string[] |
280
|
|
|
*/ |
281
|
6 |
|
private function getParameterNamesForDecoratedCall(array $parameters) |
282
|
|
|
{ |
283
|
6 |
|
return array_map( |
284
|
6 |
|
function (\ReflectionParameter $parameter) { |
285
|
6 |
|
$name = ''; |
286
|
|
|
|
287
|
6 |
|
if (method_exists($parameter, 'isVariadic')) { |
288
|
6 |
|
if ($parameter->isVariadic()) { |
289
|
|
|
$name .= '...'; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
6 |
|
$name .= '$' . $parameter->name; |
294
|
|
|
|
295
|
6 |
|
return $name; |
296
|
6 |
|
}, |
297
|
|
|
$parameters |
298
|
|
|
); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param \ReflectionMethod $method |
303
|
|
|
* |
304
|
|
|
* @return string |
305
|
|
|
* |
306
|
|
|
* @see \Doctrine\Common\Proxy\ProxyGenerator::getMethodReturnType() |
307
|
|
|
*/ |
308
|
6 |
|
private function getMethodReturnType(\ReflectionMethod $method) |
309
|
|
|
{ |
310
|
6 |
|
if ( ! method_exists($method, 'hasReturnType') || ! $method->hasReturnType()) { |
311
|
6 |
|
return ''; |
312
|
|
|
} |
313
|
2 |
|
return ': ' . $this->formatType($method->getReturnType(), $method); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param \ReflectionType $type |
318
|
|
|
* @param \ReflectionMethod $method |
319
|
|
|
* @param \ReflectionParameter|null $parameter |
320
|
|
|
* |
321
|
|
|
* @return string |
322
|
|
|
* |
323
|
|
|
* @see \Doctrine\Common\Proxy\ProxyGenerator::formatType() |
324
|
|
|
*/ |
325
|
2 |
|
private function formatType( |
326
|
|
|
\ReflectionType $type, |
327
|
|
|
\ReflectionMethod $method, |
328
|
|
|
\ReflectionParameter $parameter = null |
329
|
|
|
) { |
330
|
2 |
|
$name = method_exists($type, 'getName') ? $type->getName() : (string) $type; |
331
|
2 |
|
$nameLower = strtolower($name); |
332
|
2 |
|
if ('self' === $nameLower) { |
333
|
|
|
$name = $method->getDeclaringClass()->getName(); |
|
|
|
|
334
|
|
|
} |
335
|
2 |
|
if ('parent' === $nameLower) { |
336
|
|
|
$name = $method->getDeclaringClass()->getParentClass()->getName(); |
337
|
|
|
} |
338
|
2 |
|
if ( ! $type->isBuiltin() && ! class_exists($name) && ! interface_exists($name)) { |
339
|
|
|
if (null !== $parameter) { |
340
|
|
|
throw PersistentCollectionException::invalidParameterTypeHint( |
341
|
|
|
$method->getDeclaringClass()->getName(), |
|
|
|
|
342
|
|
|
$method->getName(), |
|
|
|
|
343
|
|
|
$parameter->getName() |
|
|
|
|
344
|
|
|
); |
345
|
|
|
} |
346
|
|
|
throw PersistentCollectionException::invalidReturnTypeHint( |
347
|
|
|
$method->getDeclaringClass()->getName(), |
|
|
|
|
348
|
|
|
$method->getName() |
|
|
|
|
349
|
|
|
); |
350
|
|
|
} |
351
|
2 |
|
if ( ! $type->isBuiltin()) { |
352
|
2 |
|
$name = '\\' . $name; |
353
|
|
|
} |
354
|
2 |
|
if ($type->allowsNull() |
355
|
2 |
|
&& (null === $parameter || ! $parameter->isDefaultValueAvailable() || null !== $parameter->getDefaultValue()) |
356
|
|
|
) { |
357
|
1 |
|
$name = '?' . $name; |
358
|
|
|
} |
359
|
2 |
|
return $name; |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.