1111
1212namespace FOS \RestBundle \EventListener ;
1313
14+ use FOS \RestBundle \Controller \Annotations \View as ViewAnnotation ;
1415use FOS \RestBundle \FOSRestBundle ;
1516use FOS \RestBundle \View \View ;
1617use FOS \RestBundle \View \ViewHandlerInterface ;
18+ use Sensio \Bundle \FrameworkExtraBundle \Configuration \Template ;
1719use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
1820use Symfony \Component \HttpFoundation \Response ;
1921use Symfony \Component \HttpKernel \Event \GetResponseForControllerResultEvent ;
@@ -58,55 +60,48 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
5860 return false ;
5961 }
6062
61- /** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
62- $ configuration = $ request ->attributes ->get ('_view ' );
63+ $ configuration = $ request ->attributes ->get ('_template ' );
6364
6465 $ view = $ event ->getControllerResult ();
65- $ customViewDefined = true ;
6666 if (!$ view instanceof View) {
67- if (!$ configuration && !$ this ->forceView ) {
67+ if (!$ configuration instanceof ViewAnnotation && !$ this ->forceView ) {
6868 return ;
6969 }
7070
7171 $ view = new View ($ view );
72- $ customViewDefined = false ;
7372 }
7473
75- if ($ configuration ) {
74+ if ($ configuration instanceof ViewAnnotation ) {
7675 if ($ configuration ->getTemplateVar ()) {
7776 $ view ->setTemplateVar ($ configuration ->getTemplateVar ());
7877 }
79- if ($ configuration ->getStatusCode () && (null === $ view ->getStatusCode () || Response::HTTP_OK === $ view ->getStatusCode ())) {
78+ if (null !== $ configuration ->getStatusCode () && (null === $ view ->getStatusCode () || Response::HTTP_OK === $ view ->getStatusCode ())) {
8079 $ view ->setStatusCode ($ configuration ->getStatusCode ());
8180 }
8281
8382 $ context = $ view ->getContext ();
84- if ($ configuration ->getSerializerGroups () && ! $ customViewDefined ) {
83+ if ($ configuration ->getSerializerGroups ()) {
8584 $ context ->addGroups ($ configuration ->getSerializerGroups ());
8685 }
8786 if ($ configuration ->getSerializerEnableMaxDepthChecks ()) {
8887 $ context ->setMaxDepth (0 );
8988 }
9089
91- $ populateDefaultVars = $ configuration ->isPopulateDefaultVars ();
90+ list ($ controller , $ action ) = $ configuration ->getOwner ();
91+ $ vars = $ this ->getDefaultVars ($ configuration , $ controller , $ action );
9292 } else {
93- $ populateDefaultVars = true ;
93+ $ vars = null ;
9494 }
9595
9696 if (null === $ view ->getFormat ()) {
9797 $ view ->setFormat ($ request ->getRequestFormat ());
9898 }
9999
100- $ vars = $ request ->attributes ->get ('_template_vars ' );
101- if (!$ vars && $ populateDefaultVars ) {
102- $ vars = $ request ->attributes ->get ('_template_default_vars ' );
103- }
104-
105100 if ($ this ->viewHandler ->isFormatTemplating ($ view ->getFormat ())
106101 && !$ view ->getRoute ()
107102 && !$ view ->getLocation ()
108103 ) {
109- if (! empty ($ vars )) {
104+ if (null !== $ vars && 0 !== count ($ vars )) {
110105 $ parameters = (array ) $ this ->viewHandler ->prepareTemplateParameters ($ view );
111106 foreach ($ vars as $ var ) {
112107 if (!array_key_exists ($ var , $ parameters )) {
@@ -116,10 +111,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
116111 $ view ->setData ($ parameters );
117112 }
118113
119- $ template = null !== $ configuration && $ configuration ->getTemplate ()
120- ? $ configuration ->getTemplate ()
121- : $ request ->attributes ->get ('_template ' );
122- if ($ template && !$ view ->getTemplate ()) {
114+ if ($ configuration && ($ template = $ configuration ->getTemplate ()) && !$ view ->getTemplate ()) {
123115 if ($ template instanceof TemplateReferenceInterface) {
124116 $ template ->set ('format ' , null );
125117 }
@@ -135,8 +127,37 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
135127
136128 public static function getSubscribedEvents ()
137129 {
130+ // Must be executed before SensioFrameworkExtraBundle's listener
138131 return array (
139- KernelEvents::VIEW => 'onKernelView ' ,
132+ KernelEvents::VIEW => array ( 'onKernelView ' , 30 ) ,
140133 );
141134 }
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+ private function getDefaultVars (Template $ template = null , $ controller , $ action )
147+ {
148+ if (0 !== count ($ arguments = $ template ->getVars ())) {
149+ return $ arguments ;
150+ }
151+
152+ if (!$ template instanceof ViewAnnotation || $ template ->isPopulateDefaultVars ()) {
153+ $ r = new \ReflectionObject ($ controller );
154+
155+ $ arguments = array ();
156+ foreach ($ r ->getMethod ($ action )->getParameters () as $ param ) {
157+ $ arguments [] = $ param ->getName ();
158+ }
159+
160+ return $ arguments ;
161+ }
162+ }
142163}
0 commit comments