forked from chrispenny/silverstripe-dataobject-to-fixture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixtureService.php
More file actions
642 lines (511 loc) · 24.9 KB
/
FixtureService.php
File metadata and controls
642 lines (511 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<?php
namespace ChrisPenny\DataObjectToFixture\Service;
use ChrisPenny\DataObjectToFixture\Manifest\FixtureManifest;
use ChrisPenny\DataObjectToFixture\Manifest\RelationshipManifest;
use ChrisPenny\DataObjectToFixture\ORM\Group;
use ChrisPenny\DataObjectToFixture\ORM\Record;
use Exception;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use Symfony\Component\Yaml\Yaml;
class FixtureService
{
use Injectable;
private ?FixtureManifest $fixtureManifest;
private ?RelationshipManifest $relationshipManifest;
private array $warnings = [];
private ?int $allowedDepth = null;
private ?array $toArrayCached = null;
/**
* @var DataObject[]
*/
private array $dataObjectStack = [];
public function __construct()
{
// The Fixture manifest is a simple (ish) mapping of Groups (classes) and Records (DataObjects). For example,
// the Group under the key `Page` will contain any/all Page DataObjects that were added to the fixture
$this->fixtureManifest = new FixtureManifest();
// The RelationshipManifest is a simple mapping of Class names and what other Classes they have relationships
// to. This will allow us to order (as best we can) our FixtureManifest later on
$this->relationshipManifest = new RelationshipManifest();
}
public function getWarnings(): array
{
return $this->warnings;
}
/**
* @throws Exception
*/
public function addDataObject(DataObject $dataObject): void
{
// Remove our cached array fixture, as you've just added another DataObject
$this->toArrayCached = null;
// Add this initial DataObject to the stack that we'll process
$this->dataObjectStack[] = $dataObject;
// Keep running until the stack is empty
while ($this->dataObjectStack) {
// Drop out the next item in the stack
$dataObject = array_shift($this->dataObjectStack);
// processDataObject() can itself add more DataObjects to the stack to be processed
$this->processDataObject($dataObject);
}
}
/**
* @throws Exception
*/
public function outputFixture(): string
{
return Yaml::dump($this->toArray(), 4, 2);
}
private function toArray(): array
{
// If we have a cache of this array already, then return it now
if ($this->toArrayCached !== null) {
return $this->toArrayCached;
}
$toArrayGroups = [];
// getPrioritisedOrder() uses our KahnSorter to arrange our fixture (as best we can) with dependencies at the
// top of the fixture
// Note: This isn't actually as important now that Populate supports "retries"
$prioritisedOrder = $this->relationshipManifest->getPrioritisedOrder();
$prioritisedOrderErrors = $this->relationshipManifest->getPrioritisedOrderErrors();
// There would have been some dependencies that we could not resolve (most likely they had a direct looping
// relationship)
foreach ($prioritisedOrderErrors as $prioritisedOrderError) {
$this->addWarning($prioritisedOrderError);
}
foreach ($prioritisedOrder as $className) {
$group = $this->fixtureManifest->getGroupByClassName($className);
// Sanity check, but this should always be present if it was represented in our PrioritisedOrder
if (!$group) {
continue;
}
// Grab all the records for this Group
$records = $group->toArray();
// Rather than break here, we'll add a warning, as there really should have been records
if (count($records) === 0) {
$this->addWarning(sprintf(
'Fixture output: No records were found for Group/ClassName "%s". You might need to check that you'
. ' do not have any relationships pointing to this Group/ClassName.',
$group->getClassName(),
));
continue;
}
$toArrayGroups[$group->getClassName()] = $records;
}
// Update our cache
$this->toArrayCached = $toArrayGroups;
return $this->toArrayCached;
}
/**
* @throws Exception
*/
private function processDataObject(DataObject $dataObject): ?Record
{
// Check isInDB() rather than exists(), as exists() has additional checks for (eg) Files
if (!$dataObject->isInDB()) {
throw new Exception('Your DataObject must be in the DB');
}
// Find or create a record based on the DataObject you want to add
$record = $this->findOrCreateRecordByClassNameId($dataObject->ClassName, $dataObject->ID);
// This addDataObject() method gets called many times as we try to build out the structure of related
// DataObjects. It's quite likely that we will come across the same record multiple times. We only need
// to add it once
if (!$record->isNew()) {
return $record;
}
// The Group should have been created when we found or created the Record
$group = $this->fixtureManifest->getGroupByClassName($dataObject->ClassName);
// We can't easily recover if it doesn't (mostly because it's unclear *why* it wouldn't be available)
if ($group === null) {
throw new Exception(sprintf('Group for class should have been available: %s', $dataObject->ClassName));
}
// Add this record to our relationship manifest
$this->relationshipManifest->addGroup($group);
// Add the standard DB fields for this record
$this->addDataObjectDbFields($dataObject);
// Add direct has_one relationships
$this->addDataObjectHasOneFields($dataObject);
// has_many fields may also include relationships that you've created using many_many "through" (so long as
// you also defined the has_many)
$this->addDataObjectHasManyFields($dataObject);
// Add many_many fields that do not contain through relationships
$this->addDataObjectManyManyFields($dataObject);
// Add many_many fields that contain through relationships
$this->addDataObjectManyManyThroughFields($dataObject);
return $record;
}
/**
* @throws Exception
*/
private function addDataObjectDbFields(DataObject $dataObject): void
{
$record = $this->fixtureManifest->getRecordByClassNameId($dataObject->ClassName, $dataObject->ID);
// The Record should already exist at this point (as we only call this method processDataObject()
if ($record === null) {
// We should just bail out
throw new Exception(
sprintf('Unable to find Record "%s" in Group "%s"', $dataObject->ID, $dataObject->ClassName)
);
}
// Find all the DB fields that have been configured for this DataObject
$dbFields = $dataObject->config()->get('db');
if (!is_array($dbFields)) {
return;
}
foreach (array_keys($dbFields) as $fieldName) {
// DB fields are pretty simple key => value. Using relField means that we follow Silverstripe convention
// for if a developer has created any override methods/etc
$value = $dataObject->relField($fieldName);
$record->addFieldValue($fieldName, $value);
}
}
/**
* @throws Exception
*/
private function addDataObjectHasOneFields(DataObject $dataObject): void
{
// The Record should already exist at this point
$record = $this->fixtureManifest->getRecordByClassNameId($dataObject->ClassName, $dataObject->ID);
// We can't easily recover if it doesn't (mostly because it's unclear *why* it wouldn't be available)
if ($record === null) {
throw new Exception(
sprintf('Unable to find Record "%s" in Group "%s"', $dataObject->ID, $dataObject->ClassName)
);
}
/** @var array $hasOneRelationships */
$hasOneRelationships = $dataObject->config()->get('has_one');
// Nothing for us to do here if there are no defined has_one
if (!is_array($hasOneRelationships)) {
return;
}
foreach ($hasOneRelationships as $relationName => $relationClassName) {
// Polymorphic has_one relationships can be defined as arrays, eg:
// ['class' => DataObject::class, 'type' => 'polymorphic']
// Extract the class name so the rest of the method can treat it as a string
if (is_array($relationClassName)) {
$relationClassName = $relationClassName['class'] ?? null;
if ($relationClassName === null) {
$this->addWarning(sprintf(
'Polymorphic relationship "%s" in class "%s" has no "class" key defined',
$relationName,
$dataObject->ClassName
));
continue;
}
}
// Relationship field names (as represented in the Database) are always appended with `ID`
$relationFieldName = sprintf('%sID', $relationName);
// field_classname_map provides devs with the opportunity to describe polymorphic relationships (see the
// README for details)
$fieldClassNameMap = $dataObject->config()->get('field_classname_map');
// Apply the map that has been specified
if ($fieldClassNameMap !== null && array_key_exists($relationFieldName, $fieldClassNameMap)) {
$relationClassName = $dataObject->relField($fieldClassNameMap[$relationFieldName]);
if (!is_string($relationClassName) || $relationClassName === '') {
$this->addWarning(sprintf(
'field_classname_map for "%s" in "%s" did not resolve to a valid class name',
$relationFieldName,
$dataObject->ClassName
));
continue;
}
}
// Check to see if class has requested that it not be included in relationship maps
$excludeClass = Config::inst()->get($relationClassName, 'exclude_from_fixture_relationships');
// Yup, exclude this class
if ($excludeClass) {
continue;
}
// Check to see if this particular relationship wants to be excluded
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
$dataObject->ClassName,
$relationName
);
// Yup, exclude this relationship
if ($excludeRelationship) {
continue;
}
// If there is no value, then, we have a relationship field, but no relationship active
if (!$dataObject->hasValue($relationFieldName)) {
continue;
}
// We expect this value to be an ID for a related object, if it's not an INT, then that's invalid
if (!is_numeric($dataObject->{$relationFieldName})) {
continue;
}
$relatedObjectId = (int) $dataObject->{$relationFieldName};
// We cannot query a DataObject. This relationship needs to be described in field_classname_map (see the
// README for details)
if ($relationClassName === DataObject::class) {
$this->addWarning(sprintf(
'Relationship "%s" found in "%s" has only been defined as DataObject. Polymorphic relationships'
. ' need to be described in field_classname_map (see the README for details)',
$relationFieldName,
$dataObject->ClassName
));
continue;
}
// Fetch the related DataObject
$relatedObject = DataObject::get($relationClassName)->byID($relatedObjectId);
// We expect the relationship to be a DataObject
if (!$relatedObject instanceof DataObject) {
$this->addWarning(sprintf(
'Related Object "%s" found on "%s" was not a DataObject',
$relationFieldName,
$dataObject->ClassName
));
continue;
}
// Build out the value that we want to use in our Fixture. This should be a "relationship" value
$relationshipValue = sprintf('=>%s.%s', $relatedObject->ClassName, $relatedObject->ID);
// Add the relationship field to our current Record
$record->addFieldValue($relationName, $relationshipValue);
// Add a relationship map for these Groups. That being, our origin DataObject class relies on the related
// DataObject class (EG: Page has ElementalArea)
$this->relationshipManifest->addRelationship($dataObject->ClassName, $relatedObject->ClassName);
// Add the related DataObject to the stack to be processed
$this->dataObjectStack[] = $relatedObject;
}
}
/**
* @throws Exception
*/
private function addDataObjectHasManyFields(DataObject $dataObject): void
{
/** @var array $hasManyRelationships */
$hasManyRelationships = $dataObject->config()->get('has_many');
if (!is_array($hasManyRelationships)) {
return;
}
$schema = $dataObject->getSchema();
foreach ($hasManyRelationships as $relationFieldName => $relationClassName) {
// Relationships are sometimes defined as ClassName.FieldName. Drop the .FieldName
$cleanRelationshipClassName = strtok($relationClassName, '.');
// Use Schema to make sure that this relationship has a reverse has_one created. This will throw an
// Exception if there isn't (SilverStripe always expects you to have it)
$schema->getRemoteJoinField($dataObject->ClassName, $relationFieldName, 'has_many');
// Check to see if this class has requested that it not be included in relationship maps
$excludeClass = Config::inst()->get($cleanRelationshipClassName, 'exclude_from_fixture_relationships');
// Yup, exclude this class
if ($excludeClass) {
continue;
}
// Check to see if this particular relationship wants to be excluded
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
$dataObject->ClassName,
$relationFieldName
);
// Yup, exclude this relationship
if ($excludeRelationship) {
continue;
}
// If we have the correct relationship mapping (a "has_one" relationship on the object in the "has_many"),
// then we can simply add each of these records and let the "has_one" be added by addRecordHasOneFields()
foreach ($dataObject->relField($relationFieldName) as $relatedObject) {
// Add the related DataObject to the stack to be processed
$this->dataObjectStack[] = $relatedObject;
}
}
}
/**
* @throws Exception
*/
private function addDataObjectManyManyFields(DataObject $dataObject): void
{
// The Record should already exist at this point
$record = $this->fixtureManifest->getRecordByClassNameId($dataObject->ClassName, $dataObject->ID);
// We can't easily recover if it doesn't (mostly because it's unclear *why* it wouldn't be available)
if ($record === null) {
throw new Exception(
sprintf('Unable to find Record "%s" in Group "%s"', $dataObject->ID, $dataObject->ClassName)
);
}
$manyManyRelationships = $dataObject->config()->get('many_many');
if (!is_array($manyManyRelationships)) {
return;
}
foreach ($manyManyRelationships as $relationFieldName => $relationClassName) {
// many_many relationships can also be defined with a through object. These are handled by the
// addDataObjectManyManyThroughFields() method
if (is_array($relationClassName)) {
continue;
}
// Relationships are sometimes defined as ClassName.FieldName. Drop the .FieldName
$cleanRelationshipClassName = strtok($relationClassName, '.');
// Check to see if this class has requested that it not be included in relationship maps
$excludeClass = Config::inst()->get($cleanRelationshipClassName, 'exclude_from_fixture_relationships');
// Yup, exclude this class
if ($excludeClass) {
continue;
}
// Check to see if this particular relationship wants to be excluded
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
$dataObject->ClassName,
$relationFieldName
);
// Yup, exclude this relationship
if ($excludeRelationship) {
continue;
}
// TL;DR: many_many is really tough. Developers could choose to define it only in one direction, or in
// both directions, and they could choose to define it either with, or without dot notation in either
// direction
$hasManyManyRelationship = $this->relationshipManifest->hasManyManyRelationship(
$dataObject->ClassName,
$relationFieldName,
$relationClassName
);
// This many_many relationship has already been represented, so we don't want to add it again
// Note: many_many relationship can be defined on one, or both sides of the relationship, but it can only
// be represented once in our fixture
if ($hasManyManyRelationship) {
continue;
}
// Track that this many_many relationship has been represented already, so that when we addDataObject()
// below we don't cause infinite recursion
$this->relationshipManifest->addManyManyRelationship(
$dataObject->ClassName,
$relationFieldName,
$relationClassName
);
// We're going to add these many_many relationships as an array
$resolvedRelationships = [];
/** @var DataList|DataObject[] $relatedObjects */
$relatedObjects = $dataObject->relField($relationFieldName);
foreach ($relatedObjects as $relatedObject) {
// Add the related DataObject as one of our resolved relationships
$resolvedRelationships[] = sprintf('=>%s.%s', $relatedObject->ClassName, $relatedObject->ID);
// Add the related DataObject to our stack to be processed
$this->dataObjectStack[] = $relatedObject;
}
// There are no relationships for us to track
if (!$resolvedRelationships) {
continue;
}
// Add all of these relationships to our Record
$record->addFieldValue($relationFieldName, $resolvedRelationships);
}
}
/**
* @throws Exception
*/
private function addDataObjectManyManyThroughFields(DataObject $dataObject): void
{
// The Record should already exist at this point
$record = $this->fixtureManifest->getRecordByClassNameId($dataObject->ClassName, $dataObject->ID);
// We can't easily recover if it doesn't (mostly because it's unclear *why* it wouldn't be available)
if ($record === null) {
throw new Exception(
sprintf('Unable to find Record "%s" in Group "%s"', $dataObject->ID, $dataObject->ClassName)
);
}
$manyManyRelationships = $dataObject->config()->get('many_many');
// many_many with through information can also be tracked in has_many, so we won't want to duplicate our
// efforts
$hasManyRelationships = $dataObject->config()->get('has_many');
if (!is_array($manyManyRelationships)) {
return;
}
foreach ($manyManyRelationships as $relationFieldName => $relationshipValue) {
// This many_many relationship does not contain "through" information, so we don't want to process this here
if (!is_array($relationshipValue) || !array_key_exists('through', $relationshipValue)) {
continue;
}
// Check to see if this particular relationship wants to be excluded
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
$dataObject->ClassName,
$relationFieldName
);
// Yup, exclude this relationship
if ($excludeRelationship) {
continue;
}
// This should always simply be defined as the class name (no dot notation)
$throughClass = $relationshipValue['through'];
$represented = false;
// First we'll check if we've already represented this relationship
if ($this->relationshipManifest->hasManyManyThroughRelationship($throughClass)) {
continue;
}
// Checking to see if this through relationship was also represented in a has_many is a bit of a mission,
// as we can't know what the relationship name is, and the class definition could be provided with (or
// without) dot notation
foreach ($hasManyRelationships as $hasManyRelationship) {
// Grab the first array item after the explode(). Doesn't matter if this is dot notation or not, it
// should always equal the class name portion of that string
[$hasManyClass] = explode('.', $hasManyRelationship);
// Yup! There is a has_many that already explains this relationship
if ($hasManyClass === $throughClass) {
$represented = true;
break;
}
}
// Skip this relationship, as it's already described by a has_many
if ($represented) {
continue;
}
// Track that this many_many relationship has been represented already, so that when we addDataObject()
// below we don't cause infinite recursion
$this->relationshipManifest->addManyManyThroughRelationship($throughClass);
// We're going to add these many_many relationships as an array
$resolvedRelationships = [];
/** @var DataList|DataObject[] $relatedObjects */
$relatedObjects = $dataObject->relField($relationFieldName);
foreach ($relatedObjects as $relatedObject) {
// Add the related DataObject as one of our resolved relationships
$resolvedRelationships[] = sprintf('=>%s.%s', $relatedObject->ClassName, $relatedObject->ID);
// Add the related DataObject to the stack to be processed
$this->dataObjectStack[] = $relatedObject;
}
// There are no relationships for us to track
if (!$resolvedRelationships) {
continue;
}
// Add all of these relationships to our Record
$record->addFieldValue($relationFieldName, $resolvedRelationships);
}
}
/**
* @throws Exception
*/
private function findOrCreateGroupByClassName(string $className): Group
{
$group = $this->fixtureManifest->getGroupByClassName($className);
if ($group !== null) {
return $group;
}
$group = Group::create($className);
$this->fixtureManifest->addGroup($group);
return $group;
}
/**
* @throws Exception
*/
private function findOrCreateRecordByClassNameId(string $className, string|int $id): Record
{
$group = $this->findOrCreateGroupByClassName($className);
// The Group should have been available. If it isn't, that's a paddlin
if ($group === null) {
throw new Exception(sprintf('Group "%s" should have been available', $className));
}
$record = $group->getRecordById($id);
// If the Record already exists, then we can just return it
if ($record !== null) {
return $record;
}
// Create and add the new Record, and then return it
$record = Record::create($id);
$group->addRecord($record);
return $record;
}
private function addWarning(string $message): void
{
if (in_array($message, $this->warnings, true)) {
return;
}
$this->warnings[] = $message;
}
}