-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
ReloadContentOutOfBand.php
173 lines (148 loc) · 5.32 KB
/
ReloadContentOutOfBand.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
<?php
namespace Neos\Neos\Ui\Domain\Model\Feedback\Operations;
/*
* This file is part of the Neos.Neos.Ui 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 Neos\Neos\FrontendRouting\NodeAddressFactory;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Fusion\Core\Cache\ContentCache;
use Neos\Fusion\Exception as FusionException;
use Neos\Neos\Fusion\Helper\CachingHelper;
use Neos\Neos\Ui\Domain\Model\AbstractFeedback;
use Neos\Neos\Ui\Domain\Model\FeedbackInterface;
use Neos\Neos\Ui\Domain\Model\RenderedNodeDomAddress;
use Neos\Neos\View\FusionView;
use Psr\Http\Message\ResponseInterface;
class ReloadContentOutOfBand extends AbstractFeedback
{
protected ?Node $node;
protected ?RenderedNodeDomAddress $nodeDomAddress;
/**
* @Flow\Inject
* @var ContentCache
*/
protected $contentCache;
/**
* @Flow\Inject
* @var CachingHelper
*/
protected $cachingHelper;
/**
* @Flow\Inject
* @var ContentRepositoryRegistry
*/
protected $contentRepositoryRegistry;
public function setNode(Node $node): void
{
$this->node = $node;
}
public function getNode(): ?Node
{
return $this->node;
}
public function setNodeDomAddress(RenderedNodeDomAddress $nodeDomAddress = null): void
{
$this->nodeDomAddress = $nodeDomAddress;
}
public function getNodeDomAddress(): ?RenderedNodeDomAddress
{
return $this->nodeDomAddress;
}
public function getType(): string
{
return 'Neos.Neos.Ui:ReloadContentOutOfBand';
}
public function getDescription(): string
{
return sprintf('Rendering of node "%s" required.', $this->node?->nodeAggregateId->value);
}
/**
* Checks whether this feedback is similar to another
*/
public function isSimilarTo(FeedbackInterface $feedback): bool
{
if (!$feedback instanceof ReloadContentOutOfBand) {
return false;
}
$feedbackNode = $feedback->getNode();
return (
$this->node instanceof Node &&
$feedbackNode instanceof Node &&
$this->node->subgraphIdentity->equals($feedbackNode->subgraphIdentity) &&
$this->node->nodeAggregateId->equals(
$feedbackNode->nodeAggregateId
) &&
$this->getNodeDomAddress() == $feedback->getNodeDomAddress()
);
}
/**
* Serialize the payload for this feedback
*
* @return array<string,mixed>
*/
public function serializePayload(ControllerContext $controllerContext): array
{
if (!is_null($this->node) && !is_null($this->nodeDomAddress)) {
$contentRepository = $this->contentRepositoryRegistry->get($this->node->subgraphIdentity->contentRepositoryId);
$nodeAddressFactory = NodeAddressFactory::create($contentRepository);
return [
'contextPath' => $nodeAddressFactory->createFromNode($this->node)->serializeForUri(),
'nodeDomAddress' => $this->nodeDomAddress,
'renderedContent' => $this->renderContent($controllerContext)
];
}
return [];
}
/**
* Render the node
*/
protected function renderContent(ControllerContext $controllerContext): string|ResponseInterface
{
if (!is_null($this->node)) {
$cacheTags = $this->cachingHelper->nodeTag($this->getNode());
foreach ($cacheTags as $tag) {
$this->contentCache->flushByTag($tag);
}
if ($this->nodeDomAddress) {
$fusionView = new FusionView();
$fakeActionRequest = clone $controllerContext->getRequest();
$fakeActionRequest->setControllerPackageKey('Neos.Neos');
$fakeActionRequest->setControllerName('Frontend\\Node');
$fakeActionRequest->setControllerActionName('edit');
$fakeControllerContext = new ControllerContext(
$fakeActionRequest,
$controllerContext->getResponse(),
$controllerContext->getArguments(),
$controllerContext->getUriBuilder(),
);
$fusionView->setControllerContext($fakeControllerContext);
$fusionView->assign('value', $this->node);
$fusionView->setFusionPath($this->nodeDomAddress->getFusionPathForContentRendering());
return $fusionView->render();
}
}
return '';
}
/**
* @return array<string,mixed>
*/
public function serialize(ControllerContext $controllerContext)
{
try {
return parent::serialize($controllerContext);
} catch (FusionException $e) {
// in case there was a rendering error, we just try to reload the document as fallback. Needed
// e.g. when adding validators to Neos.FormBuilder
return (new ReloadDocument())->serialize($controllerContext);
}
}
}