|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager; |
|
13
|
|
|
|
|
14
|
|
|
use PHPCR\NodeInterface; |
|
15
|
|
|
use ProxyManager\Factory\LazyLoadingGhostFactory; |
|
16
|
|
|
use ProxyManager\Proxy\LazyLoadingInterface; |
|
17
|
|
|
use Sulu\Component\DocumentManager\Collection\ChildrenCollection; |
|
18
|
|
|
use Sulu\Component\DocumentManager\Collection\ReferrerCollection; |
|
19
|
|
|
use Sulu\Component\DocumentManager\Event\HydrateEvent; |
|
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Handle creation of proxies. |
|
24
|
|
|
*/ |
|
25
|
|
|
class ProxyFactory |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var LazyLoadingGhostFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
private $proxyFactory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var EventDispatcherInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $dispatcher; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var DocumentRegistry |
|
39
|
|
|
*/ |
|
40
|
|
|
private $registry; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var MetadataFactoryInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $metadataFactory; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param LazyLoadingGhostFactory $proxyFactory |
|
49
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
50
|
|
|
* @param DocumentRegistry $registry |
|
51
|
|
|
* @param MetadataFactoryInterface $metadataFactory |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct( |
|
54
|
|
|
LazyLoadingGhostFactory $proxyFactory, |
|
55
|
|
|
EventDispatcherInterface $dispatcher, |
|
56
|
|
|
DocumentRegistry $registry, |
|
57
|
|
|
MetadataFactoryInterface $metadataFactory |
|
58
|
|
|
) { |
|
59
|
|
|
$this->proxyFactory = $proxyFactory; |
|
60
|
|
|
$this->dispatcher = $dispatcher; |
|
61
|
|
|
$this->registry = $registry; |
|
62
|
|
|
$this->metadataFactory = $metadataFactory; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Create a new proxy object from the given document for |
|
67
|
|
|
* the given target node. |
|
68
|
|
|
* |
|
69
|
|
|
* TODO: We only pass the document here in order to correctly evaluate its locale |
|
70
|
|
|
* later. I wonder if it necessary. |
|
71
|
|
|
* |
|
72
|
|
|
* @param object $fromDocument |
|
73
|
|
|
* @param NodeInterface $targetNode |
|
74
|
|
|
* @param array $options |
|
75
|
|
|
* |
|
76
|
|
|
* @return \ProxyManager\Proxy\GhostObjectInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
public function createProxyForNode($fromDocument, NodeInterface $targetNode, $options = []) |
|
79
|
|
|
{ |
|
80
|
|
|
// if node is already registered then just return the registered document |
|
81
|
|
|
if ($this->registry->hasNode($targetNode)) { |
|
82
|
|
|
$document = $this->registry->getDocumentForNode($targetNode); |
|
83
|
|
|
$locale = $this->registry->getOriginalLocaleForDocument($fromDocument); |
|
84
|
|
|
|
|
85
|
|
|
// If the parent is not loaded in the correct locale, reload it in the correct locale. |
|
86
|
|
|
if ($this->registry->getOriginalLocaleForDocument($document) !== $locale) { |
|
87
|
|
|
$hydrateEvent = new HydrateEvent($targetNode, $locale); |
|
88
|
|
|
$hydrateEvent->setDocument($document); |
|
89
|
|
|
$this->dispatcher->dispatch(Events::HYDRATE, $hydrateEvent); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $document; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$initializer = function (LazyLoadingInterface $document, $method, array $parameters, &$initializer) use ( |
|
96
|
|
|
$fromDocument, |
|
97
|
|
|
$targetNode, |
|
98
|
|
|
$options |
|
99
|
|
|
) { |
|
100
|
|
|
$locale = $this->registry->getOriginalLocaleForDocument($fromDocument); |
|
101
|
|
|
|
|
102
|
|
|
$hydrateEvent = new HydrateEvent($targetNode, $locale, $options); |
|
103
|
|
|
$hydrateEvent->setDocument($document); |
|
104
|
|
|
$this->dispatcher->dispatch(Events::HYDRATE, $hydrateEvent); |
|
105
|
|
|
|
|
106
|
|
|
$initializer = null; |
|
107
|
|
|
}; |
|
108
|
|
|
|
|
109
|
|
|
$targetMetadata = $this->metadataFactory->getMetadataForPhpcrNode($targetNode); |
|
110
|
|
|
$proxy = $this->proxyFactory->createProxy($targetMetadata->getClass(), $initializer); |
|
111
|
|
|
$locale = $this->registry->getOriginalLocaleForDocument($fromDocument); |
|
112
|
|
|
$this->registry->registerDocument($proxy, $targetNode, $locale); |
|
113
|
|
|
|
|
114
|
|
|
return $proxy; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Create a new children collection for the given document. |
|
119
|
|
|
* |
|
120
|
|
|
* @param object $document |
|
121
|
|
|
* |
|
122
|
|
|
* @return ChildrenCollection |
|
123
|
|
|
*/ |
|
124
|
|
|
public function createChildrenCollection($document, array $options = []) |
|
125
|
|
|
{ |
|
126
|
|
|
$node = $this->registry->getNodeForDocument($document); |
|
127
|
|
|
$locale = $this->registry->getOriginalLocaleForDocument($document); |
|
128
|
|
|
|
|
129
|
|
|
return new ChildrenCollection( |
|
130
|
|
|
$node, |
|
131
|
|
|
$this->dispatcher, |
|
132
|
|
|
$locale, |
|
133
|
|
|
$options |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Create a new collection of referrers from a list of referencing items. |
|
139
|
|
|
* |
|
140
|
|
|
* @param $document |
|
141
|
|
|
* |
|
142
|
|
|
* @return ReferrerCollection |
|
143
|
|
|
*/ |
|
144
|
|
|
public function createReferrerCollection($document) |
|
145
|
|
|
{ |
|
146
|
|
|
$node = $this->registry->getNodeForDocument($document); |
|
147
|
|
|
$locale = $this->registry->getOriginalLocaleForDocument($document); |
|
148
|
|
|
|
|
149
|
|
|
return new ReferrerCollection( |
|
150
|
|
|
$node, |
|
151
|
|
|
$this->dispatcher, |
|
152
|
|
|
$locale |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|