Completed
Push — master ( 933a2a...d1c0c8 )
by Lukas Kahwe
05:29
created

ViewResponseListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\EventListener;
13
14
use FOS\RestBundle\FOSRestBundle;
15
use FOS\RestBundle\View\View;
16
use FOS\RestBundle\View\ViewHandlerInterface;
17
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
20
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\KernelEvents;
23
24
/**
25
 * The ViewResponseListener class handles the View core event as well as the "@extra:Template" annotation.
26
 *
27
 * @author Lukas Kahwe Smith <[email protected]>
28
 *
29
 * @internal
30
 */
31
class ViewResponseListener implements EventSubscriberInterface
32
{
33
    private $viewHandler;
34
    private $forceView;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param ViewHandlerInterface $viewHandler
40
     * @param bool                 $forceView
41
     */
42 21
    public function __construct(ViewHandlerInterface $viewHandler, $forceView)
43
    {
44 21
        $this->viewHandler = $viewHandler;
45 21
        $this->forceView = $forceView;
46 21
    }
47
48
    /**
49
     * Guesses the template name to render and its variables and adds them to
50
     * the request object.
51
     *
52
     * @param FilterControllerEvent $event
53
     */
54 11
    public function onKernelController(FilterControllerEvent $event)
55
    {
56 11
        $request = $event->getRequest();
57
58 11
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
59 1
            return;
60
        }
61
62 10
        if ($configuration = $request->attributes->get('_view')) {
63 1
            $request->attributes->set('_template', $configuration);
64 1
        }
65 10
    }
66
67
    /**
68
     * Renders the parameters and template and initializes a new response object with the
69
     * rendered content.
70
     *
71
     * @param GetResponseForControllerResultEvent $event
72
     */
73 14
    public function onKernelView(GetResponseForControllerResultEvent $event)
74
    {
75 14
        $request = $event->getRequest();
76
77 14
        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
78
            return false;
79
        }
80
81
        /** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
82 14
        $configuration = $request->attributes->get('_view');
83
84 14
        $view = $event->getControllerResult();
85 14
        $customViewDefined = true;
86 14
        if (!$view instanceof View) {
87 5
            if (!$configuration && !$this->forceView) {
88 1
                return;
89
            }
90
91 4
            $view = new View($view);
92 4
            $customViewDefined = false;
93 4
        }
94
95 13
        if ($configuration) {
96 7
            $context = $view->getSerializationContext();
97 7
            if ($configuration->getTemplateVar()) {
98
                $view->setTemplateVar($configuration->getTemplateVar());
99
            }
100 7
            if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
101 1
                $view->setStatusCode($configuration->getStatusCode());
102 1
            }
103 7
            if ($configuration->getSerializerGroups() && !$customViewDefined) {
104
                $context->addGroups($configuration->getSerializerGroups());
105
            }
106 7
            if ($configuration->getSerializerEnableMaxDepthChecks()) {
107 1
                $context->setMaxDepth(0);
108 1
            }
109 7
            $populateDefaultVars = $configuration->isPopulateDefaultVars();
110 7
        } else {
111 6
            $populateDefaultVars = true;
112
        }
113
114 13
        if (null === $view->getFormat()) {
115 13
            $view->setFormat($request->getRequestFormat());
116 13
        }
117
118 13
        $vars = $request->attributes->get('_template_vars');
119 13
        if (!$vars && $populateDefaultVars) {
120 12
            $vars = $request->attributes->get('_template_default_vars');
121 12
        }
122
123 13
        if ($this->viewHandler->isFormatTemplating($view->getFormat())) {
124 9
            if (!empty($vars)) {
125 3
                $parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
126 3
                foreach ($vars as $var) {
127 3
                    if (!array_key_exists($var, $parameters)) {
128 3
                        $parameters[$var] = $request->attributes->get($var);
129 3
                    }
130 3
                }
131 3
                $view->setData($parameters);
132 3
            }
133
134 9
            $template = $request->attributes->get('_template');
135 9
            if ($template) {
136 1
                if ($template instanceof TemplateReference) {
137 1
                    $template->set('format', null);
138 1
                }
139
140 1
                $view->setTemplate($template);
141 1
            }
142 9
        }
143
144 13
        $response = $this->viewHandler->handle($view, $request);
145
146 13
        $event->setResponse($response);
147 13
    }
148
149
    public static function getSubscribedEvents()
150
    {
151
        return array(
152
            KernelEvents::CONTROLLER => array('onKernelController', -100),
153
            KernelEvents::VIEW => 'onKernelView',
154
        );
155
    }
156
}
157