1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
6
|
|
|
use Faker; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @template TObject of object |
10
|
|
|
* @abstract |
11
|
|
|
* |
12
|
|
|
* @author Kevin Bond <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Factory |
15
|
|
|
{ |
16
|
|
|
/** @var Configuration|null */ |
17
|
|
|
private static $configuration; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
* @psalm-var class-string<TObject> |
22
|
|
|
*/ |
23
|
|
|
private $class; |
24
|
|
|
|
25
|
|
|
/** @var callable|null */ |
26
|
|
|
private $instantiator; |
27
|
|
|
|
28
|
|
|
/** @var bool */ |
29
|
|
|
private $persist = true; |
30
|
|
|
|
31
|
|
|
/** @var bool */ |
32
|
|
|
private $cascadePersist = false; |
33
|
|
|
|
34
|
|
|
/** @var array<array|callable> */ |
35
|
|
|
private $attributeSet = []; |
36
|
|
|
|
37
|
|
|
/** @var callable[] */ |
38
|
|
|
private $beforeInstantiate = []; |
39
|
|
|
|
40
|
|
|
/** @var callable[] */ |
41
|
|
|
private $afterInstantiate = []; |
42
|
|
|
|
43
|
|
|
/** @var callable[] */ |
44
|
|
|
private $afterPersist = []; |
45
|
|
|
|
46
|
834 |
|
/** |
47
|
|
|
* @param array|callable $defaultAttributes |
48
|
834 |
|
* |
49
|
834 |
|
* @psalm-param class-string<TObject> $class |
50
|
834 |
|
*/ |
51
|
|
|
public function __construct(string $class, $defaultAttributes = []) |
52
|
|
|
{ |
53
|
|
|
if (self::class === static::class) { |
|
|
|
|
54
|
|
|
trigger_deprecation('zenstruck/foundry', '1.9', 'Instantiating "%s" directly is deprecated and this class will be abstract in 2.0, use "%s" instead.', self::class, AnonymousFactory::class); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->class = $class; |
58
|
|
|
$this->attributeSet[] = $defaultAttributes; |
59
|
813 |
|
} |
60
|
|
|
|
61
|
|
|
public function __call(string $name, array $arguments) |
62
|
813 |
|
{ |
63
|
|
|
if ('createMany' !== $name) { |
64
|
|
|
throw new \BadMethodCallException(\sprintf('Call to undefined method "%s::%s".', static::class, $name)); |
65
|
813 |
|
} |
66
|
|
|
|
67
|
813 |
|
trigger_deprecation('zenstruck/foundry', '1.7', 'Calling instance method "%1$s::createMany()" is deprecated and will be removed in 2.0, use the static "%1$s:createMany()" method instead.', static::class); |
68
|
20 |
|
|
69
|
|
|
return $this->many($arguments[0])->create($arguments[1] ?? []); |
70
|
20 |
|
} |
71
|
10 |
|
|
72
|
|
|
/** |
73
|
|
|
* @param array|callable $attributes |
74
|
|
|
* |
75
|
|
|
* @return Proxy<TObject>&TObject |
76
|
803 |
|
* @psalm-return Proxy<TObject> |
77
|
|
|
*/ |
78
|
753 |
|
final public function create($attributes = []): Proxy |
79
|
803 |
|
{ |
80
|
803 |
|
// merge the factory attribute set with the passed attributes |
81
|
|
|
$attributeSet = \array_merge($this->attributeSet, [$attributes]); |
82
|
|
|
|
83
|
|
|
// normalize each attribute set and collapse |
84
|
803 |
|
$attributes = \array_merge(...\array_map([$this, 'normalizeAttributes'], $attributeSet)); |
85
|
|
|
|
86
|
803 |
|
foreach ($this->beforeInstantiate as $callback) { |
87
|
10 |
|
$attributes = $callback($attributes); |
88
|
|
|
|
89
|
|
|
if (!\is_array($attributes)) { |
90
|
803 |
|
throw new \LogicException('Before Instantiate event callback must return an array.'); |
91
|
|
|
} |
92
|
803 |
|
} |
93
|
190 |
|
|
94
|
|
|
// filter each attribute to convert proxies and factories to objects |
95
|
|
|
$attributes = \array_map( |
96
|
|
|
function($value) { |
97
|
625 |
|
return $this->normalizeAttribute($value); |
98
|
30 |
|
}, |
99
|
|
|
$attributes |
100
|
625 |
|
); |
101
|
|
|
|
102
|
|
|
// instantiate the object with the users instantiator or if not set, the default instantiator |
103
|
|
|
$object = ($this->instantiator ?? self::configuration()->instantiator())($attributes, $this->class); |
104
|
|
|
|
105
|
|
|
foreach ($this->afterInstantiate as $callback) { |
106
|
|
|
$callback($object, $attributes); |
107
|
|
|
} |
108
|
240 |
|
|
109
|
|
|
$proxy = new Proxy($object); |
110
|
240 |
|
|
111
|
|
|
if (!$this->isPersisting() || true === $this->cascadePersist) { |
112
|
|
|
return $proxy; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $proxy->save()->withoutAutoRefresh(function(Proxy $proxy) use ($attributes) { |
116
|
|
|
foreach ($this->afterPersist as $callback) { |
117
|
|
|
$proxy->executeCallback($callback, $attributes); |
118
|
|
|
} |
119
|
|
|
}); |
120
|
190 |
|
} |
121
|
|
|
|
122
|
190 |
|
/** |
123
|
|
|
* @see FactoryCollection::__construct() |
124
|
|
|
* |
125
|
|
|
* @return FactoryCollection<TObject> |
126
|
|
|
*/ |
127
|
|
|
final public function many(int $min, ?int $max = null): FactoryCollection |
128
|
60 |
|
{ |
129
|
|
|
return new FactoryCollection($this, $min, $max); |
130
|
60 |
|
} |
131
|
60 |
|
|
132
|
|
|
/** |
133
|
60 |
|
* @return static |
134
|
|
|
*/ |
135
|
|
|
public function withoutPersisting(): self |
136
|
|
|
{ |
137
|
|
|
$cloned = clone $this; |
138
|
|
|
$cloned->persist = false; |
139
|
|
|
|
140
|
|
|
return $cloned; |
141
|
634 |
|
} |
142
|
|
|
|
143
|
634 |
|
/** |
144
|
634 |
|
* @param array|callable $attributes |
145
|
|
|
* |
146
|
634 |
|
* @return static |
147
|
|
|
*/ |
148
|
|
|
final public function withAttributes($attributes = []): self |
149
|
|
|
{ |
150
|
|
|
$cloned = clone $this; |
151
|
|
|
$cloned->attributeSet[] = $attributes; |
152
|
|
|
|
153
|
|
|
return $cloned; |
154
|
30 |
|
} |
155
|
|
|
|
156
|
30 |
|
/** |
157
|
30 |
|
* @param callable $callback (array $attributes): array |
158
|
|
|
* |
159
|
30 |
|
* @return static |
160
|
|
|
*/ |
161
|
|
|
final public function beforeInstantiate(callable $callback): self |
162
|
|
|
{ |
163
|
|
|
$cloned = clone $this; |
164
|
|
|
$cloned->beforeInstantiate[] = $callback; |
165
|
|
|
|
166
|
|
|
return $cloned; |
167
|
20 |
|
} |
168
|
|
|
|
169
|
20 |
|
/** |
170
|
20 |
|
* @param callable $callback (object $object, array $attributes): void |
171
|
|
|
* |
172
|
20 |
|
* @return static |
173
|
|
|
*/ |
174
|
|
|
final public function afterInstantiate(callable $callback): self |
175
|
|
|
{ |
176
|
|
|
$cloned = clone $this; |
177
|
|
|
$cloned->afterInstantiate[] = $callback; |
178
|
|
|
|
179
|
|
|
return $cloned; |
180
|
20 |
|
} |
181
|
|
|
|
182
|
20 |
|
/** |
183
|
20 |
|
* @param callable $callback (object|Proxy $object, array $attributes): void |
184
|
|
|
* |
185
|
20 |
|
* @return static |
186
|
|
|
*/ |
187
|
|
|
final public function afterPersist(callable $callback): self |
188
|
|
|
{ |
189
|
|
|
$cloned = clone $this; |
190
|
|
|
$cloned->afterPersist[] = $callback; |
191
|
|
|
|
192
|
|
|
return $cloned; |
193
|
20 |
|
} |
194
|
|
|
|
195
|
20 |
|
/** |
196
|
20 |
|
* @param callable $instantiator (array $attributes, string $class): object |
197
|
|
|
* |
198
|
20 |
|
* @return static |
199
|
|
|
*/ |
200
|
|
|
final public function instantiateWith(callable $instantiator): self |
201
|
|
|
{ |
202
|
|
|
$cloned = clone $this; |
203
|
|
|
$cloned->instantiator = $instantiator; |
204
|
968 |
|
|
205
|
|
|
return $cloned; |
206
|
968 |
|
} |
207
|
968 |
|
|
208
|
|
|
/** |
209
|
|
|
* @internal |
210
|
|
|
*/ |
211
|
|
|
final public static function boot(Configuration $configuration): void |
212
|
964 |
|
{ |
213
|
|
|
self::$configuration = $configuration; |
214
|
964 |
|
} |
215
|
10 |
|
|
216
|
|
|
/** |
217
|
|
|
* @internal |
218
|
964 |
|
*/ |
219
|
964 |
|
final public static function shutdown(): void |
220
|
964 |
|
{ |
221
|
|
|
if (!self::isBooted()) { |
222
|
|
|
return; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
self::$configuration->faker()->unique(true); // reset unique |
|
|
|
|
226
|
|
|
self::$configuration = null; |
227
|
934 |
|
} |
228
|
|
|
|
229
|
934 |
|
/** |
230
|
|
|
* @internal |
231
|
|
|
* @psalm-suppress InvalidNullableReturnType |
232
|
|
|
* @psalm-suppress NullableReturnStatement |
233
|
934 |
|
*/ |
234
|
|
|
final public static function configuration(): Configuration |
235
|
|
|
{ |
236
|
|
|
if (!self::isBooted()) { |
237
|
|
|
throw new \RuntimeException('Foundry is not yet booted. Using in a test: is your Test case using the Factories trait? Using in a fixture: is ZenstruckFoundryBundle enabled for this environment?'); |
238
|
|
|
} |
239
|
1016 |
|
|
240
|
|
|
return self::$configuration; |
|
|
|
|
241
|
1016 |
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
643 |
|
* @internal |
245
|
|
|
*/ |
246
|
643 |
|
final public static function isBooted(): bool |
247
|
|
|
{ |
248
|
|
|
return null !== self::$configuration; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
final public static function faker(): Faker\Generator |
252
|
813 |
|
{ |
253
|
|
|
return self::configuration()->faker(); |
254
|
813 |
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @internal |
258
|
|
|
* |
259
|
|
|
* @psalm-return class-string<TObject> |
260
|
|
|
*/ |
261
|
|
|
final protected function class(): string |
262
|
753 |
|
{ |
263
|
|
|
return $this->class; |
264
|
753 |
|
} |
265
|
80 |
|
|
266
|
|
|
/** |
267
|
|
|
* @param array|callable $attributes |
268
|
753 |
|
*/ |
269
|
70 |
|
private static function normalizeAttributes($attributes): array |
270
|
|
|
{ |
271
|
|
|
return \is_callable($attributes) ? $attributes(self::faker()) : $attributes; |
|
|
|
|
272
|
753 |
|
} |
273
|
|
|
|
274
|
100 |
|
/** |
275
|
|
|
* @param mixed $value |
276
|
80 |
|
* |
277
|
100 |
|
* @return mixed |
278
|
100 |
|
*/ |
279
|
|
|
private function normalizeAttribute($value) |
280
|
|
|
{ |
281
|
|
|
if ($value instanceof Proxy) { |
282
|
753 |
|
return $value->isPersisted() ? $value->refresh()->object() : $value->object(); |
283
|
753 |
|
} |
284
|
|
|
|
285
|
|
|
if ($value instanceof FactoryCollection) { |
286
|
130 |
|
$value = $this->normalizeCollection($value); |
287
|
|
|
} |
288
|
30 |
|
|
289
|
|
|
if (\is_array($value)) { |
290
|
|
|
// possible OneToMany/ManyToMany relationship |
291
|
130 |
|
return \array_map( |
292
|
|
|
function($value) { |
293
|
|
|
return $this->normalizeAttribute($value); |
294
|
70 |
|
}, |
295
|
|
|
$value |
296
|
70 |
|
); |
297
|
|
|
} |
298
|
20 |
|
|
299
|
20 |
|
if (!$value instanceof self) { |
300
|
20 |
|
return \is_object($value) ? self::normalizeObject($value) : $value; |
301
|
|
|
} |
302
|
|
|
|
303
|
20 |
|
if (!$this->isPersisting()) { |
304
|
|
|
// ensure attribute Factory's are also not persisted |
305
|
|
|
$value = $value->withoutPersisting(); |
306
|
50 |
|
} |
307
|
|
|
|
308
|
|
|
// Check if the attribute is cascade persist |
309
|
50 |
|
if (self::configuration()->hasManagerRegistry()) { |
310
|
|
|
$relationField = $this->relationshipField($value); |
311
|
50 |
|
$value->cascadePersist = $this->hasCascadePersist($value, $relationField); |
312
|
50 |
|
} |
313
|
|
|
|
314
|
50 |
|
return $value->create()->object(); |
315
|
|
|
} |
316
|
50 |
|
|
317
|
20 |
|
private static function normalizeObject(object $object): object |
318
|
|
|
{ |
319
|
|
|
try { |
320
|
|
|
return Proxy::createFromPersisted($object)->refresh()->object(); |
321
|
30 |
|
} catch (\RuntimeException $e) { |
322
|
|
|
return $object; |
323
|
|
|
} |
324
|
803 |
|
} |
325
|
|
|
|
326
|
803 |
|
private function normalizeCollection(FactoryCollection $collection): array |
327
|
|
|
{ |
328
|
|
|
$field = $this->inverseRelationshipField($collection->factory()); |
329
|
|
|
$cascadePersist = $this->hasCascadePersist($collection->factory(), $field); |
330
|
|
|
|
331
|
|
|
if ($this->isPersisting() && $field && false === $cascadePersist) { |
332
|
|
|
$this->afterPersist[] = static function(Proxy $proxy) use ($collection, $field) { |
333
|
|
|
$collection->create([$field => $proxy]); |
334
|
|
|
$proxy->refresh(); |
335
|
|
|
}; |
336
|
|
|
|
337
|
|
|
// creation delegated to afterPersist event - return empty array here |
338
|
|
|
return []; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return \array_map( |
342
|
|
|
function(self $factory) { |
343
|
|
|
$factory->cascadePersist = $this->cascadePersist; |
344
|
|
|
|
345
|
|
|
return $factory; |
346
|
|
|
}, |
347
|
|
|
$collection->all() |
348
|
|
|
); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
private function relationshipField(self $factory): ?string |
352
|
|
|
{ |
353
|
|
|
$factoryClass = $this->class; |
354
|
|
|
$relationClass = $factory->class; |
355
|
|
|
|
356
|
|
|
// Check inversedBy side ($this is the owner of the relation) |
357
|
|
|
$factoryClassMetadata = self::configuration()->objectManagerFor($factoryClass)->getMetadataFactory()->getMetadataFor($factoryClass); |
358
|
|
|
foreach ($factoryClassMetadata->getAssociationNames() as $field) { |
359
|
|
|
if (!$factoryClassMetadata->isAssociationInverseSide($field) && $factoryClassMetadata->getAssociationTargetClass($field) === $relationClass) { |
360
|
|
|
return $field; |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
// Check mappedBy side ($factory is the owner of the relation) |
365
|
|
|
$relationClassMetadata = self::configuration()->objectManagerFor($relationClass)->getClassMetadata($relationClass); |
366
|
|
|
foreach ($relationClassMetadata->getAssociationNames() as $field) { |
367
|
|
|
if (($relationClassMetadata->isSingleValuedAssociation($field) || $relationClassMetadata->isCollectionValuedAssociation($field)) && $relationClassMetadata->getAssociationTargetClass($field) === $factoryClass) { |
368
|
|
|
return $field; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
return null; // no relationship found |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
private function inverseRelationshipField(self $factory): ?string |
376
|
|
|
{ |
377
|
|
|
$collectionClass = $factory->class; |
378
|
|
|
$collectionMetadata = self::configuration()->objectManagerFor($collectionClass)->getClassMetadata($collectionClass); |
379
|
|
|
|
380
|
|
|
foreach ($collectionMetadata->getAssociationNames() as $field) { |
381
|
|
|
// ensure 1-n and associated class matches |
382
|
|
|
if ($collectionMetadata->isSingleValuedAssociation($field) && $collectionMetadata->getAssociationTargetClass($field) === $this->class) { |
383
|
|
|
return $field; |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
return null; // no relationship found |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
private function hasCascadePersist(self $factory, ?string $field): bool |
391
|
|
|
{ |
392
|
|
|
if (null === $field) { |
393
|
|
|
return false; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
$factoryClass = $this->class; |
397
|
|
|
$relationClass = $factory->class; |
398
|
|
|
$classMetadataFactory = self::configuration()->objectManagerFor($factoryClass)->getMetadataFactory()->getMetadataFor($factoryClass); |
399
|
|
|
$relationClassMetadata = self::configuration()->objectManagerFor($relationClass)->getClassMetadata($relationClass); |
400
|
|
|
|
401
|
|
|
if (!$relationClassMetadata instanceof ClassMetadataInfo || !$classMetadataFactory instanceof ClassMetadataInfo) { |
402
|
|
|
return false; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
if ($relationClassMetadata->hasAssociation($field)) { |
406
|
|
|
$inversedBy = $relationClassMetadata->getAssociationMapping($field)['inversedBy']; |
407
|
|
|
if (null === $inversedBy) { |
408
|
|
|
return false; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
$cascadeMetadata = $classMetadataFactory->getAssociationMapping($inversedBy)['cascade'] ?? []; |
412
|
|
|
} else { |
413
|
|
|
$cascadeMetadata = $classMetadataFactory->getAssociationMapping($field)['cascade'] ?? []; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
return \in_array('persist', $cascadeMetadata, true); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
private function isPersisting(): bool |
420
|
|
|
{ |
421
|
|
|
return self::configuration()->hasManagerRegistry() ? $this->persist : false; |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|