vendor/ibexa/core/src/lib/MVC/Symfony/Controller/Content/ViewController.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. namespace Ibexa\Core\MVC\Symfony\Controller\Content;
  7. use DateTime;
  8. use Exception;
  9. use Ibexa\Contracts\Core\Repository\Values\Content\Content;
  10. use Ibexa\Contracts\Core\Repository\Values\Content\Location;
  11. use Ibexa\Core\MVC\Symfony\Controller\Controller;
  12. use Ibexa\Core\MVC\Symfony\Event\APIContentExceptionEvent;
  13. use Ibexa\Core\MVC\Symfony\MVCEvents;
  14. use Ibexa\Core\MVC\Symfony\Security\Authorization\Attribute as AuthorizationAttribute;
  15. use Ibexa\Core\MVC\Symfony\View\ContentView;
  16. use Ibexa\Core\MVC\Symfony\View\ViewManagerInterface;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  19. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  20. /**
  21. * This controller provides the content view feature.
  22. *
  23. * @since 6.0.0 All methods except `view()` are deprecated and will be removed in the future.
  24. */
  25. class ViewController extends Controller
  26. {
  27. /** @var \Ibexa\Core\MVC\Symfony\View\ViewManagerInterface */
  28. protected $viewManager;
  29. /** @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface */
  30. private $authorizationChecker;
  31. public function __construct(ViewManagerInterface $viewManager, AuthorizationCheckerInterface $authorizationChecker)
  32. {
  33. $this->viewManager = $viewManager;
  34. $this->authorizationChecker = $authorizationChecker;
  35. }
  36. /**
  37. * This is the default view action or a ContentView object.
  38. *
  39. * It doesn't do anything by itself: the returned View object is rendered by the ViewRendererListener
  40. * into an HttpFoundation Response.
  41. *
  42. * This action can be selectively replaced by a custom action by means of content_view
  43. * configuration. Custom actions can add parameters to the view and customize the Response the View will be
  44. * converted to. They may also bypass the ViewRenderer by returning an HttpFoundation Response.
  45. *
  46. * Cache is in both cases handled by the CacheViewResponseListener.
  47. *
  48. * @param \Ibexa\Core\MVC\Symfony\View\ContentView $view
  49. *
  50. * @return \Ibexa\Core\MVC\Symfony\View\ContentView
  51. */
  52. public function viewAction(ContentView $view)
  53. {
  54. return $view;
  55. }
  56. /**
  57. * Embed a content.
  58. * Behaves mostly like viewAction(), but with specific content load permission handling.
  59. *
  60. * @param \Ibexa\Core\MVC\Symfony\View\ContentView $view
  61. *
  62. * @return \Ibexa\Core\MVC\Symfony\View\ContentView
  63. */
  64. public function embedAction(ContentView $view)
  65. {
  66. return $view;
  67. }
  68. /**
  69. * Build the response so that depending on settings it's cacheable.
  70. *
  71. * @param string|null $etag
  72. * @param \DateTime|null $lastModified
  73. *
  74. * @return \Symfony\Component\HttpFoundation\Response
  75. */
  76. protected function buildResponse($etag = null, DateTime $lastModified = null)
  77. {
  78. $request = $this->getRequest();
  79. $response = new Response();
  80. if ($this->getParameter('content.view_cache') === true) {
  81. $response->setPublic();
  82. if ($etag !== null) {
  83. $response->setEtag($etag);
  84. }
  85. if ($this->getParameter('content.ttl_cache') === true) {
  86. $response->setSharedMaxAge(
  87. $this->getParameter('content.default_ttl')
  88. );
  89. }
  90. // Make the response vary against X-User-Context-Hash header ensures that an HTTP
  91. // reverse proxy caches the different possible variations of the
  92. // response as it can depend on user role for instance.
  93. if ($request->headers->has('X-User-Context-Hash')) {
  94. $response->setVary('X-User-Context-Hash');
  95. }
  96. if ($lastModified != null) {
  97. $response->setLastModified($lastModified);
  98. }
  99. }
  100. return $response;
  101. }
  102. protected function handleViewException(Response $response, $params, Exception $e, $viewType, $contentId = null, $locationId = null)
  103. {
  104. $event = new APIContentExceptionEvent(
  105. $e,
  106. [
  107. 'contentId' => $contentId,
  108. 'locationId' => $locationId,
  109. 'viewType' => $viewType,
  110. ]
  111. );
  112. $this->getEventDispatcher()->dispatch($event, MVCEvents::API_CONTENT_EXCEPTION);
  113. if ($event->hasContentView()) {
  114. $response->setContent(
  115. $this->viewManager->renderContentView(
  116. $event->getContentView(),
  117. $params
  118. )
  119. );
  120. return $response;
  121. }
  122. throw $e;
  123. }
  124. /**
  125. * Creates the content to be returned when viewing a Location.
  126. *
  127. * @param \Ibexa\Contracts\Core\Repository\Values\Content\Location $location
  128. * @param string $viewType
  129. * @param bool $layout
  130. * @param array $params
  131. *
  132. * @return string
  133. */
  134. protected function renderLocation(Location $location, $viewType, $layout = false, array $params = [])
  135. {
  136. return $this->viewManager->renderLocation($location, $viewType, $params + ['no_layout' => !$layout]);
  137. }
  138. /**
  139. * Creates the content to be returned when viewing a Content.
  140. *
  141. * @param \Ibexa\Contracts\Core\Repository\Values\Content\Content $content
  142. * @param string $viewType
  143. * @param bool $layout
  144. * @param array $params
  145. *
  146. * @return string
  147. */
  148. protected function renderContent(Content $content, $viewType, $layout = false, array $params = [])
  149. {
  150. return $this->viewManager->renderContent($content, $viewType, $params + ['no_layout' => !$layout]);
  151. }
  152. /**
  153. * Performs the access checks.
  154. */
  155. protected function performAccessChecks()
  156. {
  157. if (!$this->isGranted(new AuthorizationAttribute('content', 'read'))) {
  158. throw new AccessDeniedException();
  159. }
  160. }
  161. }
  162. class_alias(ViewController::class, 'eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController');