forked from FriendsOfSymfony/FOSRestBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControllerTrait.php
78 lines (67 loc) · 2.11 KB
/
ControllerTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\RestBundle\Controller;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Symfony\Component\HttpFoundation\Response;
/**
* Trait for Controllers using the View functionality of FOSRestBundle.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
trait ControllerTrait
{
private $viewhandler;
public function setViewHandler(ViewHandlerInterface $viewhandler)
{
$this->viewhandler = $viewhandler;
}
protected function getViewHandler()
{
if (!$this->viewhandler instanceof ViewHandlerInterface) {
throw new \RuntimeException('A "ViewHandlerInterface" instance must be set when using the FOSRestBundle "ControllerTrait".');
}
return $this->viewhandler;
}
/**
* @return View
*/
protected function view($data = null, ?int $statusCode = null, array $headers = [])
{
return View::create($data, $statusCode, $headers);
}
/**
* @return View
*/
protected function redirectView(string $url, int $statusCode = Response::HTTP_FOUND, array $headers = [])
{
return View::createRedirect($url, $statusCode, $headers);
}
/**
* @return View
*/
protected function routeRedirectView(string $route, array $parameters = [], int $statusCode = Response::HTTP_CREATED, array $headers = [])
{
return View::createRouteRedirect($route, $parameters, $statusCode, $headers);
}
/**
* Converts view into a response object.
*
* Not necessary to use, if you are using the "ViewResponseListener", which
* does this conversion automatically in kernel event "onKernelView".
*
* @return Response
*/
protected function handleView(View $view)
{
return $this->getViewHandler()->handle($view);
}
}