-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
NodeEvent.php
263 lines (238 loc) · 7.42 KB
/
NodeEvent.php
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
<?php
namespace Neos\Neos\EventLog\Domain\Model;
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\Mapping as ORM;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Utility\Arrays;
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\Domain\Service\ContentContext;
use Neos\Neos\Service\UserService;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
/**
* A specific event which is used for ContentRepository Nodes (i.e. content).
*
* @Flow\Entity
*
* The following annotation is not picked up by Doctrine migrations and thus included in the Event class as well.
* See https://github.com/doctrine/doctrine2/issues/6248
*
* @ORM\Table(
* indexes={
* @ORM\Index(name="documentnodeidentifier", columns={"documentnodeidentifier"}),
* @ORM\Index(name="workspacename_parentevent", columns={"workspacename", "parentevent"})
* }
* )
*/
class NodeEvent extends Event
{
/**
* the node identifier which was created/modified/...
*
* @var string
*/
protected $nodeIdentifier;
/**
* the document node identifier on which the action took place. is equal to NodeIdentifier if the action happened on documentNodes
*
* @var string
*/
protected $documentNodeIdentifier;
/**
* the workspace name where the action took place
*
* @var string
*/
protected $workspaceName;
/**
* the dimension values for that event
*
* @var array
*/
protected $dimension;
/**
* @Flow\Inject
* @var UserService
*/
protected $userService;
/**
* @Flow\Inject
* @var ContextFactoryInterface
*/
protected $contextFactory;
/**
* @Flow\Inject
* @var PersistenceManagerInterface
*/
protected $persistenceManager;
/**
* @Flow\Inject
* @var SiteRepository
*/
protected $siteRepository;
/**
* Return name of the workspace where the node event happened
*
* @return string
*/
public function getWorkspaceName()
{
return $this->workspaceName;
}
/**
* @return bool
*/
public function isDocumentEvent()
{
return $this->documentNodeIdentifier === $this->nodeIdentifier;
}
/**
* Return the node identifier of the closest parent document node related to this event
*
* @return string
*/
public function getDocumentNodeIdentifier()
{
return $this->documentNodeIdentifier;
}
/**
* Return the node identifier of the node this event relates to
*
* @return string
*/
public function getNodeIdentifier()
{
return $this->nodeIdentifier;
}
/**
* Set the "context node" this operation was working on.
*
* @param NodeInterface $node
* @return void
*/
public function setNode(NodeInterface $node)
{
$this->nodeIdentifier = $node->getIdentifier();
$this->workspaceName = $node->getContext()->getWorkspaceName();
$this->dimension = $node->getContext()->getDimensions();
$context = $node->getContext();
if ($context instanceof ContentContext && $context->getCurrentSite() !== null) {
$siteIdentifier = $this->persistenceManager->getIdentifierByObject($context->getCurrentSite());
} else {
$siteIdentifier = null;
}
$this->data = Arrays::arrayMergeRecursiveOverrule($this->data, [
'nodeContextPath' => $node->getContextPath(),
'nodeLabel' => $node->getLabel(),
'nodeType' => $node->getNodeType()->getName(),
'site' => $siteIdentifier
]);
$node = self::getClosestAggregateNode($node);
if ($node !== null) {
$this->documentNodeIdentifier = $node->getIdentifier();
$this->data = Arrays::arrayMergeRecursiveOverrule($this->data, [
'documentNodeContextPath' => $node->getContextPath(),
'documentNodeLabel' => $node->getLabel(),
'documentNodeType' => $node->getNodeType()->getName()
]);
}
}
/**
* Override the workspace name. *MUST* be called after setNode(), else it won't have an effect.
*
* @param string $workspaceName
* @return void
*/
public function setWorkspaceName($workspaceName)
{
$this->workspaceName = $workspaceName;
}
/**
* Returns the closest aggregate node of the given node
*
* @param NodeInterface $node
* @return NodeInterface
*/
public static function getClosestAggregateNode(NodeInterface $node)
{
while ($node !== null && !$node->getNodeType()->isAggregate()) {
$node = $node->getParent();
}
return $node;
}
/**
* Returns the closest document node, if it can be resolved.
*
* It might happen that, if this event refers to a node contained in a site which is not available anymore,
* Doctrine's proxy class of the Site domain model will fail with an EntityNotFoundException. We catch this
* case and return NULL.
*
* @return NodeInterface
*/
public function getDocumentNode()
{
try {
$context = $this->contextFactory->create([
'workspaceName' => $this->userService->getPersonalWorkspaceName(),
'dimensions' => $this->dimension,
'currentSite' => $this->getCurrentSite(),
'invisibleContentShown' => true
]);
return $context->getNodeByIdentifier($this->documentNodeIdentifier);
} catch (EntityNotFoundException $e) {
return null;
}
}
/**
* Returns the node this even refers to, if it can be resolved.
*
* It might happen that, if this event refers to a node contained in a site which is not available anymore,
* Doctrine's proxy class of the Site domain model will fail with an EntityNotFoundException. We catch this
* case and return NULL.
*
* @return NodeInterface
*/
public function getNode()
{
try {
$context = $this->contextFactory->create([
'workspaceName' => $this->userService->getPersonalWorkspaceName(),
'dimensions' => $this->dimension,
'currentSite' => $this->getCurrentSite(),
'invisibleContentShown' => true
]);
return $context->getNodeByIdentifier($this->nodeIdentifier);
} catch (EntityNotFoundException $e) {
return null;
}
}
/**
* Prevents invalid calls to the site repository in case the site data property is not available.
*
* @return null|object
*/
protected function getCurrentSite()
{
if (!isset($this->data['site']) || $this->data['site'] === null) {
return null;
}
return $this->siteRepository->findByIdentifier($this->data['site']);
}
/**
* @return string
*/
public function __toString()
{
return sprintf('NodeEvent[%s, %s]', $this->eventType, $this->nodeIdentifier);
}
}