public function onKernelView(GetResponseForControllerResultEvent $event)
{
$data = $event->getControllerResult();
$request = $event->getRequest();
if (!$event->getRequest()->attributes->get('_jarves_is_plugin')) {
//we accept only plugin responses.
return;
}
$content = $request->attributes->get('_content');
if (null !== $data) {
if ($data instanceof PluginResponseInterface) {
$response = $data;
} else {
$response = $this->pageResponseFactory->createPluginResponse($data);
}
//it's required to place a PluginResponseInterface as response, so
//PluginResponseListener::onKernelResponse can correctly identify it
//and set it as response for this plugin content, so ContentTypes\TypePlugin
//can place the correct response at the correct position, without executing
//the plugin twice.
$event->setResponse($response);
} else {
//we hit a plugin route, but it has responsed with NULL
//this means it is not responsible for this route/slug.
//we need now to remove this plugin route from the route collection
//and fire again a sub request until all plugins on this page
//are handled. If no plugin is responsible for this url pattern
//and the main page route is also not responsible
//no response is set in the $event and a 404 is thrown by the HttpKernel.
$foundRoute = false;
$routes = $this->frontendRouteListener->getRoutes();
foreach ($routes as $idx => $route) {
/** @var \Symfony\Component\Routing\Route $route */
if ($content === $route->getDefault('_content')) {
//remove exactly only the current plugin that was hit in this sub request
$routes->remove($idx);
$foundRoute = true;
break;
}
}
if ($foundRoute) {
//we've removed the route and fire now again a sub request
$request = clone $this->pageStack->getRequest();
$request->attributes = new ParameterBag();
$response = $this->kernel->handle($request, HttpKernelInterface::SUB_REQUEST);
$event->setResponse($response);
}
//we do not need to restore routes in the frontendRouteListener, because
//it reload all routes on every master request
}
}