1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry; |
4
|
|
|
|
5
|
|
|
use Faker; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Kevin Bond <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class Factory |
11
|
|
|
{ |
12
|
|
|
/** @var Configuration|null */ |
13
|
|
|
private static $configuration; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $class; |
17
|
|
|
|
18
|
|
|
/** @var callable|null */ |
19
|
|
|
private $instantiator; |
20
|
|
|
|
21
|
|
|
/** @var bool */ |
22
|
|
|
private $persist = true; |
23
|
|
|
|
24
|
|
|
/** @var array<array|callable> */ |
25
|
|
|
private $attributeSet = []; |
26
|
|
|
|
27
|
|
|
/** @var callable[] */ |
28
|
|
|
private $beforeInstantiate = []; |
29
|
|
|
|
30
|
|
|
/** @var callable[] */ |
31
|
|
|
private $afterInstantiate = []; |
32
|
|
|
|
33
|
|
|
/** @var callable[] */ |
34
|
|
|
private $afterPersist = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array|callable $defaultAttributes |
38
|
|
|
*/ |
39
|
621 |
|
public function __construct(string $class, $defaultAttributes = []) |
40
|
|
|
{ |
41
|
621 |
|
$this->class = $class; |
42
|
621 |
|
$this->attributeSet[] = $defaultAttributes; |
43
|
621 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array|callable $attributes |
47
|
|
|
* |
48
|
|
|
* @return Proxy|object |
49
|
|
|
*/ |
50
|
604 |
|
final public function create($attributes = []): Proxy |
51
|
|
|
{ |
52
|
|
|
// merge the factory attribute set with the passed attributes |
53
|
604 |
|
$attributeSet = \array_merge($this->attributeSet, [$attributes]); |
54
|
|
|
|
55
|
|
|
// normalize each attribute set and collapse |
56
|
604 |
|
$attributes = \array_merge(...\array_map([$this, 'normalizeAttributes'], $attributeSet)); |
57
|
|
|
|
58
|
604 |
|
foreach ($this->beforeInstantiate as $callback) { |
59
|
16 |
|
$attributes = $callback($attributes); |
60
|
|
|
|
61
|
16 |
|
if (!\is_array($attributes)) { |
62
|
8 |
|
throw new \LogicException('Before Instantiate event callback must return an array.'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// filter each attribute to convert proxies and factories to objects |
67
|
596 |
|
$attributes = \array_map( |
68
|
|
|
function($value) { |
69
|
556 |
|
return $this->normalizeAttribute($value); |
70
|
596 |
|
}, |
71
|
596 |
|
$attributes |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
// instantiate the object with the users instantiator or if not set, the default instantiator |
75
|
596 |
|
$object = ($this->instantiator ?? self::configuration()->instantiator())($attributes, $this->class); |
76
|
|
|
|
77
|
596 |
|
foreach ($this->afterInstantiate as $callback) { |
78
|
8 |
|
$callback($object, $attributes); |
79
|
|
|
} |
80
|
|
|
|
81
|
596 |
|
$proxy = new Proxy($object); |
82
|
|
|
|
83
|
596 |
|
if (!$this->isPersisting()) { |
84
|
152 |
|
return $proxy; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $proxy->save()->withoutAutoRefresh(function(Proxy $proxy) use ($attributes) { |
88
|
452 |
|
foreach ($this->afterPersist as $callback) { |
89
|
24 |
|
$proxy->executeCallback($callback, $attributes); |
90
|
|
|
} |
91
|
452 |
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @see FactoryCollection::__construct() |
96
|
|
|
*/ |
97
|
176 |
|
final public function many(int $min, ?int $max = null): FactoryCollection |
98
|
|
|
{ |
99
|
176 |
|
return new FactoryCollection($this, $min, $max); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array|callable $attributes |
104
|
|
|
* |
105
|
|
|
* @return Proxy[]|object[] |
106
|
|
|
*/ |
107
|
136 |
|
final public function createMany(int $number, $attributes = []): array |
108
|
|
|
{ |
109
|
136 |
|
return $this->many($number)->create($attributes); |
110
|
|
|
} |
111
|
|
|
|
112
|
48 |
|
public function withoutPersisting(): self |
113
|
|
|
{ |
114
|
48 |
|
$cloned = clone $this; |
115
|
48 |
|
$cloned->persist = false; |
116
|
|
|
|
117
|
48 |
|
return $cloned; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array|callable $attributes |
122
|
|
|
*/ |
123
|
457 |
|
final public function withAttributes($attributes = []): self |
124
|
|
|
{ |
125
|
457 |
|
$cloned = clone $this; |
126
|
457 |
|
$cloned->attributeSet[] = $attributes; |
127
|
|
|
|
128
|
457 |
|
return $cloned; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param callable $callback (array $attributes): array |
133
|
|
|
*/ |
134
|
24 |
|
final public function beforeInstantiate(callable $callback): self |
135
|
|
|
{ |
136
|
24 |
|
$cloned = clone $this; |
137
|
24 |
|
$cloned->beforeInstantiate[] = $callback; |
138
|
|
|
|
139
|
24 |
|
return $cloned; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param callable $callback (object $object, array $attributes): void |
144
|
|
|
*/ |
145
|
16 |
|
final public function afterInstantiate(callable $callback): self |
146
|
|
|
{ |
147
|
16 |
|
$cloned = clone $this; |
148
|
16 |
|
$cloned->afterInstantiate[] = $callback; |
149
|
|
|
|
150
|
16 |
|
return $cloned; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param callable $callback (object|Proxy $object, array $attributes): void |
155
|
|
|
*/ |
156
|
16 |
|
final public function afterPersist(callable $callback): self |
157
|
|
|
{ |
158
|
16 |
|
$cloned = clone $this; |
159
|
16 |
|
$cloned->afterPersist[] = $callback; |
160
|
|
|
|
161
|
16 |
|
return $cloned; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param callable $instantiator (array $attributes, string $class): object |
166
|
|
|
*/ |
167
|
16 |
|
final public function instantiateWith(callable $instantiator): self |
168
|
|
|
{ |
169
|
16 |
|
$cloned = clone $this; |
170
|
16 |
|
$cloned->instantiator = $instantiator; |
171
|
|
|
|
172
|
16 |
|
return $cloned; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @internal |
177
|
|
|
*/ |
178
|
734 |
|
final public static function boot(Configuration $configuration): void |
179
|
|
|
{ |
180
|
734 |
|
self::$configuration = $configuration; |
181
|
734 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @internal |
185
|
|
|
*/ |
186
|
731 |
|
final public static function shutdown(): void |
187
|
|
|
{ |
188
|
731 |
|
if (!self::isBooted()) { |
189
|
8 |
|
return; |
190
|
|
|
} |
191
|
|
|
|
192
|
731 |
|
self::$configuration->faker()->unique(true); // reset unique |
|
|
|
|
193
|
731 |
|
self::$configuration = null; |
194
|
731 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @internal |
198
|
|
|
*/ |
199
|
707 |
|
final public static function configuration(): Configuration |
200
|
|
|
{ |
201
|
707 |
|
if (!self::isBooted()) { |
202
|
|
|
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?'); |
203
|
|
|
} |
204
|
|
|
|
205
|
707 |
|
return self::$configuration; |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @internal |
210
|
|
|
*/ |
211
|
770 |
|
final public static function isBooted(): bool |
212
|
|
|
{ |
213
|
770 |
|
return null !== self::$configuration; |
214
|
|
|
} |
215
|
|
|
|
216
|
464 |
|
final public static function faker(): Faker\Generator |
217
|
|
|
{ |
218
|
464 |
|
return self::configuration()->faker(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param array|callable $attributes |
223
|
|
|
*/ |
224
|
604 |
|
private static function normalizeAttributes($attributes): array |
225
|
|
|
{ |
226
|
604 |
|
return \is_callable($attributes) ? $attributes(self::faker()) : $attributes; |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param mixed $value |
231
|
|
|
* |
232
|
|
|
* @return mixed |
233
|
|
|
*/ |
234
|
556 |
|
private function normalizeAttribute($value) |
235
|
|
|
{ |
236
|
556 |
|
if ($value instanceof Proxy) { |
237
|
64 |
|
return $value->object(); |
238
|
|
|
} |
239
|
|
|
|
240
|
556 |
|
if ($value instanceof FactoryCollection) { |
241
|
56 |
|
$value = $this->normalizeCollection($value); |
242
|
|
|
} |
243
|
|
|
|
244
|
556 |
|
if (\is_array($value)) { |
245
|
|
|
// possible OneToMany/ManyToMany relationship |
246
|
80 |
|
return \array_map( |
247
|
|
|
function($value) { |
248
|
64 |
|
return $this->normalizeAttribute($value); |
249
|
80 |
|
}, |
250
|
80 |
|
$value |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
556 |
|
if (!$value instanceof self) { |
255
|
556 |
|
return $value; |
256
|
|
|
} |
257
|
|
|
|
258
|
104 |
|
if (!$this->isPersisting()) { |
259
|
|
|
// ensure attribute Factory's are also not persisted |
260
|
24 |
|
$value = $value->withoutPersisting(); |
261
|
|
|
} |
262
|
|
|
|
263
|
104 |
|
return $value->create()->object(); |
264
|
|
|
} |
265
|
|
|
|
266
|
56 |
|
private function normalizeCollection(FactoryCollection $collection): array |
267
|
|
|
{ |
268
|
56 |
|
if ($this->isPersisting() && $field = $this->inverseRelationshipField($collection->factory())) { |
269
|
|
|
$this->afterPersist[] = static function(Proxy $proxy) use ($collection, $field) { |
270
|
16 |
|
$collection->create([$field => $proxy]); |
271
|
16 |
|
$proxy->refresh(); |
272
|
16 |
|
}; |
273
|
|
|
|
274
|
|
|
// creation delegated to afterPersist event - return empty array here |
275
|
16 |
|
return []; |
276
|
|
|
} |
277
|
|
|
|
278
|
40 |
|
return $collection->all(); |
279
|
|
|
} |
280
|
|
|
|
281
|
40 |
|
private function inverseRelationshipField(self $factory): ?string |
282
|
|
|
{ |
283
|
40 |
|
$collectionClass = $factory->class; |
284
|
40 |
|
$collectionMetadata = self::configuration()->objectManagerFor($collectionClass)->getClassMetadata($collectionClass); |
285
|
|
|
|
286
|
40 |
|
foreach ($collectionMetadata->getAssociationNames() as $field) { |
287
|
|
|
// ensure 1-n and associated class matches |
288
|
40 |
|
if ($collectionMetadata->isSingleValuedAssociation($field) && $collectionMetadata->getAssociationTargetClass($field) === $this->class) { |
289
|
16 |
|
return $field; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
24 |
|
return null; // no relationship found |
294
|
|
|
} |
295
|
|
|
|
296
|
596 |
|
private function isPersisting(): bool |
297
|
|
|
{ |
298
|
596 |
|
return self::configuration()->hasManagerRegistry() ? $this->persist : false; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.