Пример #1
0
 /**
  * Processes a successful controller into an HTTP 200 response.
  *
  * Some controllers may not return a response object but simply the body of
  * one.  The VIEW event is called in that case, to allow us to mutate that
  * body into a Response object.  In particular we assume that the return
  * from an JSON-type response is a JSON string, so just wrap it into a
  * Response object.
  *
  * @param Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
  *   The Event to process.
  */
 public function onView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     // For a master request, we process the result and wrap it as needed.
     // For a subrequest, all we want is the string value.  We assume that
     // is just an HTML string from a controller, so wrap that into a response
     // object.  The subrequest's response will get dissected and placed into
     // the larger page as needed.
     if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
         $method = 'on' . $this->negotiation->getContentType($request);
         if (method_exists($this, $method)) {
             $event->setResponse($this->{$method}($event));
         } else {
             $event->setResponse(new Response('Not Acceptable', 406));
         }
     } else {
         // This is a new-style Symfony-esque subrequest, which means we assume
         // the body is not supposed to be a complete page but just a page
         // fragment.
         $page_result = $event->getControllerResult();
         if ($page_result instanceof HtmlPage || $page_result instanceof Response) {
             return $page_result;
         }
         if (!is_array($page_result)) {
             $page_result = array('#markup' => $page_result);
         }
         // If no title was returned fall back to one defined in the route.
         if (!isset($page_result['#title'])) {
             $page_result['#title'] = $this->titleResolver->getTitle($request, $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT));
         }
         $event->setResponse(new Response(drupal_render_root($page_result)));
     }
 }
 /**
  * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
  *
  * @throws \Exception
  */
 public function onKernelResultView(GetResponseForControllerResultEvent $event)
 {
     if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
         return;
     }
     if (!$this->isRestRequest($event->getRequest())) {
         return;
     }
     $result = $event->getControllerResult();
     $event->setResponse($this->visitResult($result));
     $event->stopPropagation();
 }
Пример #3
0
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
         return;
     }
     $result = $event->getControllerResult();
     if (!is_array($result)) {
         return;
     }
     $result = $this->factory->create($this->resolver->resolve($event->getRequest(), $result), $result);
     $event->setResponse(new Response($result));
 }
Пример #4
0
 /**
  * Processes a successful controller into an HTTP 200 response.
  *
  * Some controllers may not return a response object but simply the body of
  * one.  The VIEW event is called in that case, to allow us to mutate that
  * body into a Response object.  In particular we assume that the return
  * from an JSON-type response is a JSON string, so just wrap it into a
  * Response object.
  *
  * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
  *   The Event to process.
  */
 public function onView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
         $method = 'on' . $request->getRequestFormat();
         if (method_exists($this, $method)) {
             $event->setResponse($this->{$method}($event));
         } else {
             $event->setResponse(new Response('Not Acceptable', 406));
         }
     }
 }
Пример #5
0
 /**
  * This event is called when the controller action returns something else than a response object (e.g. an array).
  * This function handles the rendering of proper template according to the requested partial.
  * 
  * @param GetResponseForControllerResultEvent $event Event called.
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     // only work with master request
     if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
         return;
     }
     // only on NJAX requests
     $njax = $this->container->get('njax');
     if (!$njax->isNjax()) {
         return;
     }
     $request = $event->getRequest();
     $parameters = $event->getControllerResult();
     if (!($njaxConfiguration = $request->attributes->get('_njax'))) {
         return;
     }
     // this behavior is copied from Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelView()
     if (null === $parameters) {
         if (!($vars = $request->attributes->get('_template_vars'))) {
             if (!($vars = $request->attributes->get('_template_default_vars'))) {
                 return;
             }
         }
         $parameters = array();
         foreach ($vars as $var) {
             $parameters[$var] = $request->attributes->get($var);
         }
     }
     if (!is_array($parameters)) {
         return $parameters;
     }
     // get the templating engine
     $templating = $this->container->get('templating');
     // now figure out which template to use
     $partial = $njax->getPartial();
     $template = $njaxConfiguration->getPartialTemplate($partial);
     $usedPartial = $partial;
     // if no specified template then try to guess the name of a partial template
     if (!$template) {
         $template = $njaxConfiguration->guessPartialTemplate($partial);
         // if couldn't guess the name then just use the default one
         if (!$template || !$templating->exists($template)) {
             $template = $njaxConfiguration->getDefaultTemplate();
             $usedPartial = null;
         }
     }
     $content = $templating->render($template, $parameters);
     $event->setResponse(new Response($content));
 }