1 | <?php |
||
32 | class ViewResponseListener implements EventSubscriberInterface |
||
33 | { |
||
34 | private $viewHandler; |
||
35 | private $forceView; |
||
36 | |||
37 | /** |
||
38 | * Constructor. |
||
39 | * |
||
40 | * @param ViewHandlerInterface $viewHandler |
||
41 | * @param bool $forceView |
||
42 | */ |
||
43 | 19 | public function __construct(ViewHandlerInterface $viewHandler, $forceView) |
|
48 | |||
49 | /** |
||
50 | * Renders the parameters and template and initializes a new response object with the |
||
51 | * rendered content. |
||
52 | * |
||
53 | * @param GetResponseForControllerResultEvent $event |
||
54 | */ |
||
55 | 19 | public function onKernelView(GetResponseForControllerResultEvent $event) |
|
56 | { |
||
57 | 19 | $request = $event->getRequest(); |
|
58 | |||
59 | 19 | if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) { |
|
60 | return false; |
||
61 | } |
||
62 | |||
63 | 19 | $configuration = $request->attributes->get('_template'); |
|
64 | |||
65 | 19 | $view = $event->getControllerResult(); |
|
66 | 19 | if (!$view instanceof View) { |
|
67 | 9 | if (!$configuration instanceof ViewAnnotation && !$this->forceView) { |
|
68 | 1 | return; |
|
69 | } |
||
70 | |||
71 | 8 | $view = new View($view); |
|
72 | 8 | } |
|
73 | |||
74 | 18 | if ($configuration instanceof ViewAnnotation) { |
|
75 | 12 | if ($configuration->getTemplateVar()) { |
|
76 | $view->setTemplateVar($configuration->getTemplateVar()); |
||
77 | } |
||
78 | 12 | if (null !== $configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) { |
|
79 | 1 | $view->setStatusCode($configuration->getStatusCode()); |
|
80 | 1 | } |
|
81 | |||
82 | 12 | $context = $view->getContext(); |
|
83 | 12 | if ($configuration->getSerializerGroups()) { |
|
84 | $context->addGroups($configuration->getSerializerGroups()); |
||
85 | } |
||
86 | 12 | if ($configuration->getSerializerEnableMaxDepthChecks()) { |
|
87 | $context->setMaxDepth(0); |
||
88 | } |
||
89 | |||
90 | 12 | list($controller, $action) = $configuration->getOwner(); |
|
91 | 12 | $vars = $this->getDefaultVars($configuration, $controller, $action); |
|
92 | 12 | } else { |
|
93 | 6 | $vars = null; |
|
94 | } |
||
95 | |||
96 | 18 | if (null === $view->getFormat()) { |
|
97 | 18 | $view->setFormat($request->getRequestFormat()); |
|
98 | 18 | } |
|
99 | |||
100 | 18 | if ($this->viewHandler->isFormatTemplating($view->getFormat()) |
|
101 | 18 | && !$view->getRoute() |
|
|
|||
102 | 18 | && !$view->getLocation() |
|
103 | 18 | ) { |
|
104 | 10 | if (null !== $vars && 0 !== count($vars)) { |
|
105 | 4 | $parameters = (array) $this->viewHandler->prepareTemplateParameters($view); |
|
106 | 4 | foreach ($vars as $var) { |
|
107 | 4 | if (!array_key_exists($var, $parameters)) { |
|
108 | 3 | $parameters[$var] = $request->attributes->get($var); |
|
109 | 3 | } |
|
110 | 4 | } |
|
111 | 4 | $view->setData($parameters); |
|
112 | 4 | } |
|
113 | |||
114 | 10 | if ($configuration && ($template = $configuration->getTemplate()) && !$view->getTemplate()) { |
|
115 | 2 | if ($template instanceof TemplateReferenceInterface) { |
|
116 | 1 | $template->set('format', null); |
|
117 | 1 | } |
|
118 | |||
119 | 2 | $view->setTemplate($template); |
|
120 | 2 | } |
|
121 | 10 | } |
|
122 | |||
123 | 18 | $response = $this->viewHandler->handle($view, $request); |
|
124 | |||
125 | 18 | $event->setResponse($response); |
|
126 | 18 | } |
|
127 | |||
128 | 14 | public static function getSubscribedEvents() |
|
135 | |||
136 | /** |
||
137 | * @param Request $request |
||
138 | * @param Template $template |
||
139 | * @param object $controller |
||
140 | * @param string $action |
||
141 | * |
||
142 | * @return array |
||
143 | * |
||
144 | * @see \Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::resolveDefaultParameters() |
||
145 | */ |
||
146 | 12 | private function getDefaultVars(Template $template = null, $controller, $action) |
|
163 | } |
||
164 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: