PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 187 lines | 78 code | 26 blank | 83 comment | 11 complexity | d0e916693220970c9ed3a3c8afdd0a6a MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\Core\EventSubscriber\ViewSubscriber.
  5. */
  6. namespace Drupal\Core\EventSubscriber;
  7. use Drupal\Core\Ajax\AjaxResponseRenderer;
  8. use Drupal\Core\Controller\TitleResolverInterface;
  9. use Drupal\Core\Page\HtmlPage;
  10. use Symfony\Cmf\Component\Routing\RouteObjectInterface;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\HttpKernel\HttpKernelInterface;
  15. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Drupal\Core\ContentNegotiation;
  18. /**
  19. * Main subscriber for VIEW HTTP responses.
  20. *
  21. * @todo This needs to get refactored to be extensible so that we can handle
  22. * more than just Html and Drupal-specific JSON requests. See
  23. * http://drupal.org/node/1594870
  24. */
  25. class ViewSubscriber implements EventSubscriberInterface {
  26. /**
  27. * The content negotiation.
  28. *
  29. * @var \Drupal\Core\ContentNegotiation
  30. */
  31. protected $negotiation;
  32. /**
  33. * The title resolver.
  34. *
  35. * @var \Drupal\Core\Controller\TitleResolverInterface
  36. */
  37. protected $titleResolver;
  38. /**
  39. * The Ajax response renderer.
  40. *
  41. * @var \Drupal\Core\Ajax\AjaxResponseRenderer
  42. */
  43. protected $ajaxRenderer;
  44. /**
  45. * Constructs a new ViewSubscriber.
  46. *
  47. * @param \Drupal\Core\ContentNegotiation $negotiation
  48. * The content negotiation.
  49. * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
  50. * The title resolver.
  51. * @param \Drupal\Core\Ajax\AjaxResponseRenderer $ajax_renderer
  52. * The ajax response renderer.
  53. */
  54. public function __construct(ContentNegotiation $negotiation, TitleResolverInterface $title_resolver, AjaxResponseRenderer $ajax_renderer) {
  55. $this->negotiation = $negotiation;
  56. $this->titleResolver = $title_resolver;
  57. $this->ajaxRenderer = $ajax_renderer;
  58. }
  59. /**
  60. * Processes a successful controller into an HTTP 200 response.
  61. *
  62. * Some controllers may not return a response object but simply the body of
  63. * one. The VIEW event is called in that case, to allow us to mutate that
  64. * body into a Response object. In particular we assume that the return
  65. * from an JSON-type response is a JSON string, so just wrap it into a
  66. * Response object.
  67. *
  68. * @param Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
  69. * The Event to process.
  70. */
  71. public function onView(GetResponseForControllerResultEvent $event) {
  72. $request = $event->getRequest();
  73. // For a master request, we process the result and wrap it as needed.
  74. // For a subrequest, all we want is the string value. We assume that
  75. // is just an HTML string from a controller, so wrap that into a response
  76. // object. The subrequest's response will get dissected and placed into
  77. // the larger page as needed.
  78. if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
  79. $method = 'on' . $this->negotiation->getContentType($request);
  80. if (method_exists($this, $method)) {
  81. $event->setResponse($this->$method($event));
  82. }
  83. else {
  84. $event->setResponse(new Response('Not Acceptable', 406));
  85. }
  86. }
  87. else {
  88. // This is a new-style Symfony-esque subrequest, which means we assume
  89. // the body is not supposed to be a complete page but just a page
  90. // fragment.
  91. $page_result = $event->getControllerResult();
  92. if ($page_result instanceof HtmlPage || $page_result instanceof Response) {
  93. return $page_result;
  94. }
  95. if (!is_array($page_result)) {
  96. $page_result = array(
  97. '#markup' => $page_result,
  98. );
  99. }
  100. // If no title was returned fall back to one defined in the route.
  101. if (!isset($page_result['#title'])) {
  102. $page_result['#title'] = $this->titleResolver->getTitle($request, $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT));
  103. }
  104. $event->setResponse(new Response(drupal_render($page_result)));
  105. }
  106. }
  107. public function onJson(GetResponseForControllerResultEvent $event) {
  108. $page_callback_result = $event->getControllerResult();
  109. $response = new JsonResponse();
  110. $response->setData($page_callback_result);
  111. return $response;
  112. }
  113. public function onIframeUpload(GetResponseForControllerResultEvent $event) {
  114. $response = $event->getResponse();
  115. // Browser IFRAMEs expect HTML. Browser extensions, such as Linkification
  116. // and Skype's Browser Highlighter, convert URLs, phone numbers, etc. into
  117. // links. This corrupts the JSON response. Protect the integrity of the
  118. // JSON data by making it the value of a textarea.
  119. // @see http://malsup.com/jquery/form/#file-upload
  120. // @see http://drupal.org/node/1009382
  121. $html = '<textarea>' . $response->getContent() . '</textarea>';
  122. return new Response($html);
  123. }
  124. /**
  125. * Processes a successful controller into an HTTP 200 response.
  126. *
  127. * Some controllers may not return a response object but simply the body of
  128. * one. The VIEW event is called in that case, to allow us to mutate that
  129. * body into a Response object. In particular we assume that the return from
  130. * an HTML-type response is a render array from a legacy page callback and
  131. * render it.
  132. *
  133. * @param Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
  134. * The Event to process.
  135. */
  136. public function onHtml(GetResponseForControllerResultEvent $event) {
  137. $page_callback_result = $event->getControllerResult();
  138. $request = $event->getRequest();
  139. // Convert string content to a renderable array, so we can set a title.
  140. if (!is_array($page_callback_result)) {
  141. $page_callback_result = array(
  142. '#markup' => $page_callback_result,
  143. );
  144. }
  145. // If no title was returned fall back to one defined in the route.
  146. if (!isset($page_callback_result['#title'])) {
  147. $page_callback_result['#title'] = $this->titleResolver->getTitle($request, $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT));
  148. }
  149. return new Response(drupal_render_page($page_callback_result));
  150. }
  151. /**
  152. * Registers the methods in this class that should be listeners.
  153. *
  154. * @return array
  155. * An array of event listener definitions.
  156. */
  157. static function getSubscribedEvents() {
  158. $events[KernelEvents::VIEW][] = array('onView');
  159. return $events;
  160. }
  161. }