|
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\Common\Collections\ArrayCollection; |
|
23
|
|
|
use Doctrine\Common\Collections\Collection as BaseCollection; |
|
24
|
|
|
use Doctrine\ODM\MongoDB\Configuration; |
|
25
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
26
|
|
|
use Doctrine\ODM\MongoDB\PersistentCollection; |
|
27
|
|
|
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionFactory; |
|
28
|
|
|
use Doctrine\ODM\MongoDB\UnitOfWork; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Default factory class for persistent collection classes. |
|
32
|
|
|
* |
|
33
|
|
|
* @since 1.1 |
|
34
|
|
|
* @author Maciej Malarz <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
final class DefaultPersistentCollectionFactory implements PersistentCollectionFactory |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* The DocumentManager this factory is bound to. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \Doctrine\ODM\MongoDB\DocumentManager |
|
42
|
|
|
*/ |
|
43
|
|
|
private $dm; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The UnitOfWork used to coordinate object-level transactions. |
|
47
|
|
|
* |
|
48
|
|
|
* @var \Doctrine\ODM\MongoDB\UnitOfWork |
|
49
|
|
|
*/ |
|
50
|
|
|
private $uow; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Which algorithm to use to automatically (re)generate persistent collection classes. |
|
54
|
|
|
* |
|
55
|
|
|
* @var integer |
|
56
|
|
|
*/ |
|
57
|
|
|
private $autoGenerate; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The namespace that contains all persistent collection classes. |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
private $collectionNamespace; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The directory that contains all persistent collection classes. |
|
68
|
|
|
* |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
private $collectionDir; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param \Doctrine\ODM\MongoDB\DocumentManager $dm |
|
75
|
|
|
* @param \Doctrine\ODM\MongoDB\UnitOfWork $uow |
|
76
|
|
|
* @param string $collectionDir |
|
77
|
|
|
* @param string $collectionNs |
|
78
|
|
|
* @param int $autoGenerate |
|
79
|
|
|
*/ |
|
80
|
929 |
|
public function __construct(DocumentManager $dm, UnitOfWork $uow, $collectionDir, $collectionNs, $autoGenerate) |
|
81
|
|
|
{ |
|
82
|
929 |
|
$this->dm = $dm; |
|
83
|
929 |
|
$this->uow = $uow; |
|
84
|
929 |
|
$this->collectionDir = $collectionDir; |
|
85
|
929 |
|
$this->collectionNamespace = $collectionNs; |
|
86
|
929 |
|
$this->autoGenerate = $autoGenerate; |
|
87
|
929 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
382 |
|
public function create(array $mapping, BaseCollection $coll = null) |
|
93
|
|
|
{ |
|
94
|
382 |
|
if ($coll === null) { |
|
95
|
253 |
|
$coll = ! empty($mapping['collectionClass']) |
|
96
|
253 |
|
? new $mapping['collectionClass'] |
|
97
|
253 |
|
: new ArrayCollection(); |
|
98
|
253 |
|
} |
|
99
|
382 |
|
if (empty($mapping['collectionClass'])) { |
|
100
|
382 |
|
return new PersistentCollection($coll, $this->dm, $this->uow); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// These checks are not in __construct() because of BC and should be moved for 2.0 |
|
104
|
2 |
|
if ( ! $this->collectionDir) { |
|
105
|
|
|
throw PersistentCollectionException::directoryRequired(); |
|
106
|
|
|
} |
|
107
|
2 |
|
if ( ! $this->collectionNamespace) { |
|
108
|
|
|
throw PersistentCollectionException::namespaceRequired(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
$collClassName = str_replace('\\', '', $mapping['collectionClass']) . 'Persistent'; |
|
112
|
2 |
|
$className = $this->collectionNamespace . '\\' . $collClassName; |
|
113
|
2 |
|
if ( ! class_exists($className, false)) { |
|
114
|
1 |
|
$fileName = $this->collectionDir . DIRECTORY_SEPARATOR . $collClassName . '.php'; |
|
115
|
1 |
|
switch ($this->autoGenerate) { |
|
116
|
1 |
|
case Configuration::AUTOGENERATE_NEVER: |
|
117
|
|
|
require $fileName; |
|
118
|
|
|
break; |
|
119
|
|
|
|
|
120
|
1 |
|
case Configuration::AUTOGENERATE_ALWAYS: |
|
121
|
1 |
|
$this->generateCollectionClass($mapping['collectionClass'], $className, $fileName); |
|
122
|
1 |
|
require $fileName; |
|
123
|
1 |
|
break; |
|
124
|
|
|
|
|
125
|
|
|
case Configuration::AUTOGENERATE_FILE_NOT_EXISTS: |
|
126
|
|
|
if ( ! file_exists($fileName)) { |
|
127
|
|
|
$this->generateCollectionClass($mapping['collectionClass'], $className, $fileName); |
|
128
|
|
|
} |
|
129
|
|
|
require $fileName; |
|
130
|
|
|
break; |
|
131
|
|
|
|
|
132
|
|
|
case Configuration::AUTOGENERATE_EVAL: |
|
133
|
|
|
$this->generateCollectionClass($mapping['collectionClass'], $className, false); |
|
134
|
|
|
break; |
|
135
|
1 |
|
} |
|
136
|
1 |
|
} |
|
137
|
2 |
|
return new $className($coll, $this->dm, $this->uow); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function generateClass($class, $dir) |
|
144
|
|
|
{ |
|
145
|
|
|
$collClassName = str_replace('\\', '', $class) . 'Persistent'; |
|
146
|
|
|
$className = $this->collectionNamespace . '\\' . $collClassName; |
|
147
|
|
|
$fileName = $dir . DIRECTORY_SEPARATOR . $collClassName . '.php'; |
|
148
|
|
|
$this->generateCollectionClass($class, $className, $fileName); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
1 |
|
private function generateCollectionClass($for, $targetFqcn, $fileName) |
|
152
|
|
|
{ |
|
153
|
1 |
|
$exploded = explode('\\', $targetFqcn); |
|
154
|
1 |
|
$class = array_pop($exploded); |
|
155
|
1 |
|
$namespace = join('\\', $exploded); |
|
156
|
|
|
$code = <<<CODE |
|
157
|
|
|
<?php |
|
158
|
|
|
|
|
159
|
|
|
namespace $namespace; |
|
160
|
|
|
|
|
161
|
|
|
use Doctrine\Common\Collections\Collection as BaseCollection; |
|
162
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
163
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
|
164
|
|
|
use Doctrine\ODM\MongoDB\MongoDBException; |
|
165
|
|
|
use Doctrine\ODM\MongoDB\UnitOfWork; |
|
166
|
|
|
use Doctrine\ODM\MongoDB\Utility\CollectionHelper; |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* DO NOT EDIT THIS FILE - IT WAS CREATED BY DOCTRINE\'S PERSISTENT COLLECTION GENERATOR |
|
170
|
|
|
*/ |
|
171
|
1 |
|
class $class extends \\$for implements \\Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionInterface |
|
172
|
|
|
{ |
|
173
|
|
|
use \\Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionTrait; |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param BaseCollection \$coll |
|
177
|
|
|
* @param DocumentManager \$dm |
|
178
|
|
|
* @param UnitOfWork \$uow |
|
179
|
|
|
*/ |
|
180
|
|
|
public function __construct(BaseCollection \$coll, DocumentManager \$dm, UnitOfWork \$uow) |
|
181
|
|
|
{ |
|
182
|
|
|
\$this->coll = \$coll; |
|
183
|
|
|
\$this->dm = \$dm; |
|
184
|
|
|
\$this->uow = \$uow; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
1 |
|
CODE; |
|
188
|
1 |
|
$rc = new \ReflectionClass($for); |
|
189
|
1 |
|
$rt = new \ReflectionClass('Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionTrait'); |
|
190
|
1 |
|
foreach ($rc->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
191
|
|
|
if ( |
|
192
|
1 |
|
$rt->hasMethod($method->getName()) || |
|
|
|
|
|
|
193
|
1 |
|
$method->isConstructor() || |
|
194
|
1 |
|
$method->isFinal() || |
|
195
|
1 |
|
$method->isStatic() |
|
196
|
1 |
|
) { |
|
197
|
1 |
|
continue; |
|
198
|
|
|
} |
|
199
|
1 |
|
$code .= $this->generateMethod($method); |
|
200
|
1 |
|
} |
|
201
|
1 |
|
$code .= "}\n"; |
|
202
|
|
|
|
|
203
|
1 |
|
if ($fileName === false) { |
|
204
|
|
|
if ( ! class_exists($targetFqcn)) { |
|
205
|
|
|
eval(substr($code, 5)); |
|
206
|
|
|
} |
|
207
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
208
|
1 |
|
$parentDirectory = dirname($fileName); |
|
209
|
|
|
|
|
210
|
1 |
|
if ( ! is_dir($parentDirectory) && (false === @mkdir($parentDirectory, 0775, true))) { |
|
211
|
|
|
throw PersistentCollectionException::directoryNotWritable(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
1 |
|
if ( ! is_writable($parentDirectory)) { |
|
215
|
|
|
throw PersistentCollectionException::directoryNotWritable(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
1 |
|
$tmpFileName = $fileName . '.' . uniqid('', true); |
|
219
|
1 |
|
file_put_contents($tmpFileName, $code); |
|
220
|
1 |
|
rename($tmpFileName, $fileName); |
|
221
|
|
|
} |
|
222
|
1 |
|
} |
|
223
|
|
|
|
|
224
|
1 |
|
private function generateMethod(\ReflectionMethod $method) |
|
225
|
|
|
{ |
|
226
|
1 |
|
$parametersString = $this->buildParametersString($method, $method->getParameters()); |
|
227
|
1 |
|
$callParamsString = implode(', ', $this->getParameterNamesForDecoratedCall($method->getParameters())); |
|
228
|
|
|
|
|
229
|
|
|
$method = <<<CODE |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* {@inheritDoc} |
|
233
|
|
|
*/ |
|
234
|
1 |
|
public function {$method->getName()}($parametersString) |
|
|
|
|
|
|
235
|
|
|
{ |
|
236
|
|
|
\$this->initialize(); |
|
237
|
1 |
|
return \$this->coll->{$method->getName()}($callParamsString); |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
1 |
|
CODE; |
|
241
|
1 |
|
return $method; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param \ReflectionMethod $method |
|
246
|
|
|
* @param \ReflectionParameter[] $parameters |
|
247
|
|
|
* |
|
248
|
|
|
* @return string |
|
249
|
|
|
*/ |
|
250
|
1 |
|
private function buildParametersString(\ReflectionMethod $method, array $parameters) |
|
|
|
|
|
|
251
|
|
|
{ |
|
252
|
1 |
|
$parameterDefinitions = array(); |
|
253
|
|
|
|
|
254
|
|
|
/* @var $param \ReflectionParameter */ |
|
255
|
1 |
|
foreach ($parameters as $param) { |
|
256
|
1 |
|
$parameterDefinition = ''; |
|
257
|
|
|
|
|
258
|
1 |
|
if ($parameterType = $this->getParameterType($param)) { |
|
|
|
|
|
|
259
|
1 |
|
$parameterDefinition .= $parameterType . ' '; |
|
260
|
1 |
|
} |
|
261
|
|
|
|
|
262
|
1 |
|
if ($param->isPassedByReference()) { |
|
263
|
|
|
$parameterDefinition .= '&'; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
1 |
|
if (method_exists($param, 'isVariadic')) { |
|
267
|
|
|
if ($param->isVariadic()) { |
|
268
|
|
|
$parameterDefinition .= '...'; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
1 |
|
$parameters[] = '$' . $param->getName(); |
|
273
|
1 |
|
$parameterDefinition .= '$' . $param->getName(); |
|
274
|
|
|
|
|
275
|
1 |
|
if ($param->isDefaultValueAvailable()) { |
|
276
|
|
|
$parameterDefinition .= ' = ' . var_export($param->getDefaultValue(), true); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
1 |
|
$parameterDefinitions[] = $parameterDefinition; |
|
280
|
1 |
|
} |
|
281
|
|
|
|
|
282
|
1 |
|
return implode(', ', $parameterDefinitions); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param \ReflectionParameter $parameter |
|
287
|
|
|
* |
|
288
|
|
|
* @return string|null |
|
289
|
|
|
*/ |
|
290
|
1 |
|
private function getParameterType(\ReflectionParameter $parameter) |
|
291
|
|
|
{ |
|
292
|
|
|
// We need to pick the type hint class too |
|
293
|
1 |
|
if ($parameter->isArray()) { |
|
294
|
|
|
return 'array'; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
1 |
|
if (method_exists($parameter, 'isCallable') && $parameter->isCallable()) { |
|
298
|
|
|
return 'callable'; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
try { |
|
302
|
1 |
|
$parameterClass = $parameter->getClass(); |
|
303
|
|
|
|
|
304
|
1 |
|
if ($parameterClass) { |
|
305
|
1 |
|
return '\\' . $parameterClass->getName(); |
|
|
|
|
|
|
306
|
|
|
} |
|
307
|
1 |
|
} catch (\ReflectionException $previous) { |
|
308
|
|
|
// @todo ProxyGenerator throws specialized exceptions |
|
309
|
|
|
throw $previous; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
1 |
|
return null; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* @param \ReflectionParameter[] $parameters |
|
317
|
|
|
* |
|
318
|
|
|
* @return string[] |
|
319
|
|
|
*/ |
|
320
|
1 |
|
private function getParameterNamesForDecoratedCall(array $parameters) |
|
321
|
|
|
{ |
|
322
|
1 |
|
return array_map( |
|
323
|
1 |
|
function (\ReflectionParameter $parameter) { |
|
324
|
1 |
|
$name = ''; |
|
325
|
|
|
|
|
326
|
1 |
|
if (method_exists($parameter, 'isVariadic')) { |
|
327
|
|
|
if ($parameter->isVariadic()) { |
|
328
|
|
|
$name .= '...'; |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
1 |
|
$name .= '$' . $parameter->getName(); |
|
|
|
|
|
|
333
|
|
|
|
|
334
|
1 |
|
return $name; |
|
335
|
1 |
|
}, |
|
336
|
|
|
$parameters |
|
337
|
1 |
|
); |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|