1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Bridge\ContentType\Doctrine\PhpcrOdm; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\ClassUtils; |
6
|
|
|
use Doctrine\ODM\PHPCR\DocumentManagerInterface; |
7
|
|
|
use Doctrine\ODM\PHPCR\Event; |
8
|
|
|
use Metadata\MetadataFactory; |
9
|
|
|
use Metadata\MetadataFactoryInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The collection identifier updater updates the IDs (paths) of any documents |
13
|
|
|
* mapped under a "CollectionField" to conform with the expected filter. |
14
|
|
|
* |
15
|
|
|
* This is necessary as it is not currently possible to achieve this in an |
16
|
|
|
* event subscriber within the PHPCR-ODM itself, see: |
17
|
|
|
* |
18
|
|
|
* https://github.com/doctrine/phpcr-odm/issues/726 |
19
|
|
|
* |
20
|
|
|
* Instead it is necessary to invoke this on any documents (at least those |
21
|
|
|
* which have a CollectionField mapping) before the DocumentManager#flush() |
22
|
|
|
* method is called. This could easily be done from the controller / |
23
|
|
|
* persistence agent of the consuming library. |
24
|
|
|
*/ |
25
|
|
|
class CollectionIdentifierUpdater |
26
|
|
|
{ |
27
|
|
|
private $metadataFactory; |
28
|
|
|
private $encoder; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
MetadataFactoryInterface $metadataFactory, |
32
|
|
|
PropertyEncoder $encoder |
33
|
|
|
) { |
34
|
|
|
$this->metadataFactory = $metadataFactory; |
35
|
|
|
$this->encoder = $encoder; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function update(DocumentManagerInterface $documentManager, $document) |
39
|
|
|
{ |
40
|
|
|
$metadataFactory = $documentManager->getMetadataFactory(); |
41
|
|
|
$classFqn = ClassUtils::getRealClass(get_class($document)); |
42
|
|
|
|
43
|
|
|
// PHPCR-ODM will throw an exception if the document is not mapped. |
44
|
|
|
$odmMetadata = $metadataFactory->getMetadataFor($classFqn); |
45
|
|
|
|
46
|
|
|
if (null === $ctMetadata = $this->metadataFactory->getMetadataForClass($classFqn)) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$documentId = $odmMetadata->getIdentifierValue($document); |
|
|
|
|
51
|
|
|
|
52
|
|
|
foreach ($odmMetadata->childrenMappings as $childrenField) { |
|
|
|
|
53
|
|
|
|
54
|
|
|
// if the children field is not managed by the CT component, |
55
|
|
|
// continue |
56
|
|
|
if (!isset($ctMetadata->propertyMetadata[$childrenField])) { |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$children = $odmMetadata->getFieldValue($document, $childrenField); |
61
|
|
|
|
62
|
|
|
// note that we do not preserve array keys. PHPCR ODM will return a |
63
|
|
|
// children collection using the PHPCR property names as keys, so |
64
|
|
|
// we currently have no control over how these keys populated. |
65
|
|
|
$index = 0; |
66
|
|
|
foreach ($children as $child) { |
67
|
|
|
$childMetadata = $metadataFactory->getMetadataFor(ClassUtils::getRealClass(get_class($child))); |
68
|
|
|
$newId = sprintf( |
69
|
|
|
'%s/%s', |
70
|
|
|
$documentId, |
71
|
|
|
$this->encoder->encode($childrenField, $index++) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$childMetadata->setIdentifierValue($child, $newId); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.