vendor/friendsofsymfony/rest-bundle/Controller/ControllerTrait.php line 116

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSRestBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\RestBundle\Controller;
  11. use FOS\RestBundle\View\View;
  12. use FOS\RestBundle\View\ViewHandlerInterface;
  13. use Symfony\Component\HttpFoundation\Response;
  14. /**
  15.  * Trait for Controllers using the View functionality of FOSRestBundle.
  16.  *
  17.  * @author Benjamin Eberlei <kontakt@beberlei.de>
  18.  * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  19.  */
  20. trait ControllerTrait
  21. {
  22.     /**
  23.      * @var ViewHandlerInterface
  24.      */
  25.     private $viewhandler;
  26.     /**
  27.      * Get the ViewHandler.
  28.      *
  29.      * @param ViewHandlerInterface $viewhandler
  30.      */
  31.     public function setViewHandler(ViewHandlerInterface $viewhandler)
  32.     {
  33.         $this->viewhandler $viewhandler;
  34.     }
  35.     /**
  36.      * Get the ViewHandler.
  37.      *
  38.      * @return ViewHandlerInterface
  39.      */
  40.     protected function getViewHandler()
  41.     {
  42.         if (!$this->viewhandler instanceof ViewHandlerInterface) {
  43.             throw new \RuntimeException('A "ViewHandlerInterface" instance must be set when using the FOSRestBundle "ControllerTrait".');
  44.         }
  45.         return $this->viewhandler;
  46.     }
  47.     /**
  48.      * Creates a view.
  49.      *
  50.      * Convenience method to allow for a fluent interface.
  51.      *
  52.      * @param mixed $data
  53.      * @param int   $statusCode
  54.      * @param array $headers
  55.      *
  56.      * @return View
  57.      */
  58.     protected function view($data null$statusCode null, array $headers = [])
  59.     {
  60.         return View::create($data$statusCode$headers);
  61.     }
  62.     /**
  63.      * Creates a Redirect view.
  64.      *
  65.      * Convenience method to allow for a fluent interface.
  66.      *
  67.      * @param string $url
  68.      * @param int    $statusCode
  69.      * @param array  $headers
  70.      *
  71.      * @return View
  72.      */
  73.     protected function redirectView($url$statusCode Response::HTTP_FOUND, array $headers = [])
  74.     {
  75.         return View::createRedirect($url$statusCode$headers);
  76.     }
  77.     /**
  78.      * Creates a Route Redirect View.
  79.      *
  80.      * Convenience method to allow for a fluent interface.
  81.      *
  82.      * @param string $route
  83.      * @param mixed  $parameters
  84.      * @param int    $statusCode
  85.      * @param array  $headers
  86.      *
  87.      * @return View
  88.      */
  89.     protected function routeRedirectView($route, array $parameters = [], $statusCode Response::HTTP_CREATED, array $headers = [])
  90.     {
  91.         return View::createRouteRedirect($route$parameters$statusCode$headers);
  92.     }
  93.     /**
  94.      * Converts view into a response object.
  95.      *
  96.      * Not necessary to use, if you are using the "ViewResponseListener", which
  97.      * does this conversion automatically in kernel event "onKernelView".
  98.      *
  99.      * @param View $view
  100.      *
  101.      * @return Response
  102.      */
  103.     protected function handleView(View $view)
  104.     {
  105.         return $this->getViewHandler()->handle($view);
  106.     }
  107. }