Skip to content

Commit 4a857f5

Browse files
committed
Add support for payload mapping
1 parent cffe74b commit 4a857f5

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

EventListener/RestListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function addNoCacheHeaders(Response $response)
7474
* @param Request $request
7575
* @param Array $trigger
7676
*
77-
* @return Mixed false or content accepted
77+
* @return Mixed false or content accepted
7878
*/
7979
public function checkAcceptedContent(Request $request, $acceptedContent)
8080
{
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
1717
use Symfony\Component\HttpKernel\HttpKernelInterface;
18-
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
1918
use Symfony\Component\HttpKernel\KernelEvents;
19+
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
20+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
21+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
2022
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2123

22-
class RestKernelSuscriber implements EventSubscriberInterface
24+
class RestSubscriber implements EventSubscriberInterface
2325
{
2426
/**
2527
*
2628
* @param Request $request
2729
* @param Array $trigger
2830
*
29-
* @return Mixed false or content accepted
31+
* @return Mixed false or content accepted
3032
*/
3133
public function checkAcceptedContent(Request $request, $acceptedContent)
3234
{
@@ -46,6 +48,24 @@ public function checkAcceptedContent(Request $request, $acceptedContent)
4648
return false;
4749
}
4850

51+
public function postAnnotations(FilterControllerEvent $event)
52+
{
53+
$request = $event->getRequest();
54+
$restConfig = $request->attributes->get('_rest');
55+
56+
if ($restConfig == false) {
57+
return;
58+
}
59+
60+
if ($restConfig->getPayloadMapping()) {
61+
$content = $request->getContent();
62+
if (empty($content) == false) {
63+
$payload = json_decode($content, true);
64+
$request->request->add(array($restConfig->getPayloadMapping() => $payload));
65+
}
66+
}
67+
}
68+
4969
public function onKernelResponse(FilterResponseEvent $event)
5070
{
5171
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
@@ -127,7 +147,8 @@ public function onKernelResponse(FilterResponseEvent $event)
127147
public static function getSubscribedEvents()
128148
{
129149
return array(
130-
KernelEvents::RESPONSE => array('onKernelResponse', 0),
150+
KernelEvents::CONTROLLER => array('postAnnotations', 0),
151+
KernelEvents::RESPONSE => array('onKernelResponse', 0)
131152
);
132153
}
133154
}

Resources/config/services.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ services:
1919
tags:
2020
- { name: kernel.event_listener, event: kernel.view, method: onKernelView }
2121

22-
wobblecode.restbundle.kernel.suscriber:
23-
class: WobbleCode\RestBundle\EventListener\RestKernelSuscriber
22+
wobblecode.restbundle.rest_suscriber:
23+
class: WobbleCode\RestBundle\EventListener\RestSubscriber
2424
tags:
2525
- { name: kernel.event_subscriber }

0 commit comments

Comments
 (0)