Skip to content

Commit

Permalink
Adjust WidgetViewHelper to removed dispatched
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsunet authored and mhsdesign committed Mar 3, 2024
1 parent 316c2ed commit c1e5375
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions Neos.FluidAdaptor/Classes/Core/Widget/AbstractWidgetViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,30 @@ protected function initiateSubRequest()
$subRequest->setArgumentNamespace('--' . $this->widgetContext->getWidgetIdentifier());

$dispatchLoopCount = 0;
$content = '';
while (!$subRequest->isDispatched()) {
if ($dispatchLoopCount++ > 99) {
throw new InfiniteLoopException('Could not ultimately dispatch the widget request after ' . $dispatchLoopCount . ' iterations.', 1380282310);
}

do {
$widgetControllerObjectName = $this->widgetContext->getControllerObjectName();
if ($subRequest->getControllerObjectName() !== '' && $subRequest->getControllerObjectName() !== $widgetControllerObjectName) {
throw new Exception\InvalidControllerException(sprintf('You are not allowed to initiate requests to different controllers from a widget.' . chr(10) . 'widget controller: "%s", requested controller: "%s".', $widgetControllerObjectName, $subRequest->getControllerObjectName()), 1380284579);
}
$subRequest->setControllerObjectName($this->widgetContext->getControllerObjectName());
try {
$subResponse = $this->controller->processRequest($subRequest);

// We need to make sure to not merge content up into the parent ActionResponse because that _could_ break the parent response.
$content = $subResponse->getBody()->getContents();

// hacky, but part of the deal. Legacy behaviour of "mergeIntoParentResponse":
// we have to manipulate the global response to redirect for example.
// transfer possible headers that have been set dynamically
foreach ($subResponse->getHeaders() as $name => $values) {
$this->controllerContext->getResponse()->setHttpHeader($name, $values);
}
// if the status code is 200 we assume it's the default and will not overrule it
if ($subResponse->getStatusCode() !== 200) {
$this->controllerContext->getResponse()->setStatusCode($subResponse->getStatusCode());
}

return $content;
} catch (StopActionException $exception) {
$subResponse = $exception->response;
$parentResponse = $this->controllerContext->getResponse()->buildHttpResponse();
Expand All @@ -251,6 +262,7 @@ protected function initiateSubRequest()
$parentResponse = $parentResponse->withStatus($subResponse->getStatusCode());
}
// if the known body size is not empty replace the body
// we keep the contents of $subResponse as it might contain <meta http-equiv="refresh" for redirects
if ($subResponse->getBody()->getSize() !== 0) {
$parentResponse = $parentResponse->withBody($subResponse->getBody());
}
Expand All @@ -260,23 +272,8 @@ protected function initiateSubRequest()
$subRequest = $exception->nextRequest;
continue;
}

// We need to make sure to not merge content up into the parent ActionResponse because that _could_ break the parent response.
$content = $subResponse->getBody()->getContents();

// hacky, but part of the deal. Legacy behaviour of "mergeIntoParentResponse":
// we have to manipulate the global response to redirect for example.
// transfer possible headers that have been set dynamically
foreach ($subResponse->getHeaders() as $name => $values) {
$this->controllerContext->getResponse()->setHttpHeader($name, $values);
}
// if the status code is 200 we assume it's the default and will not overrule it
if ($subResponse->getStatusCode() !== 200) {
$this->controllerContext->getResponse()->setStatusCode($subResponse->getStatusCode());
}
}

return $content;
} while ($dispatchLoopCount++ < 99);
throw new InfiniteLoopException('Could not ultimately dispatch the widget request after ' . $dispatchLoopCount . ' iterations.', 1380282310);
}

/**
Expand Down

0 comments on commit c1e5375

Please sign in to comment.