/**
* Initiate a sub request to $this->controller. Make sure to fill $this->controller
* via Dependency Injection.
* @return Response the response of this request.
* @throws Exception\InvalidControllerException
* @throws Exception\MissingControllerException
* @throws InfiniteLoopException
* @throws StopActionException
* @api
*/
protected function initiateSubRequest()
{
if ($this->controller instanceof DependencyProxy) {
$this->controller->_activateDependency();
}
if (!$this->controller instanceof AbstractWidgetController) {
throw new Exception\MissingControllerException('initiateSubRequest() can not be called if there is no controller inside $this->controller. Make sure to add the @Neos\\Flow\\Annotations\\Inject annotation in your widget class.', 1284401632);
}
/** @var $subRequest ActionRequest */
$subRequest = $this->objectManager->get(ActionRequest::class, $this->controllerContext->getRequest());
/** @var $subResponse Response */
$subResponse = $this->objectManager->get(Response::class, $this->controllerContext->getResponse());
$this->passArgumentsToSubRequest($subRequest);
$subRequest->setArgument('__widgetContext', $this->widgetContext);
$subRequest->setArgumentNamespace('--' . $this->widgetContext->getWidgetIdentifier());
$dispatchLoopCount = 0;
while (!$subRequest->isDispatched()) {
if ($dispatchLoopCount++ > 99) {
throw new InfiniteLoopException('Could not ultimately dispatch the widget request after ' . $dispatchLoopCount . ' iterations.', 1380282310);
}
$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 {
$this->controller->processRequest($subRequest, $subResponse);
} catch (StopActionException $exception) {
if ($exception instanceof ForwardException) {
$subRequest = $exception->getNextRequest();
continue;
}
/** @var $parentResponse Response */
$parentResponse = $this->controllerContext->getResponse();
$parentResponse->setStatus($subResponse->getStatusCode())->setContent($subResponse->getContent())->setHeader('Location', $subResponse->getHeader('Location'));
throw $exception;
}
}
return $subResponse;
}