Skip to content

Commit 75a46a5

Browse files
committed
SensioFrameworkExtraBundle 2.0.x compatibility
1 parent ea4f6f6 commit 75a46a5

File tree

11 files changed

+487
-4
lines changed

11 files changed

+487
-4
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ matrix:
2121
env: deps="low"
2222
- php: 5.5
2323
env: SYMFONY_VERSION='2.3.* symfony/expression-language:2.4.*'
24+
- php: 5.5
25+
env: SYMFONY_VERSION='2.3.* sensio/framework-extra-bundle:2.*'
2426
- php: 5.5
2527
env: SYMFONY_VERSION=2.4.*
2628
- php: 5.5

DependencyInjection/FOSRestExtension.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,26 @@ private function loadBodyConverter(array $config, $validator, XmlFileLoader $loa
202202
{
203203
if (!empty($config['body_converter'])) {
204204
if (!empty($config['body_converter']['enabled'])) {
205+
$parameter = new \ReflectionParameter(
206+
array(
207+
'Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface',
208+
'supports',
209+
),
210+
'configuration'
211+
);
212+
213+
if ('Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter' === $parameter->getClass()->getName()) {
214+
$container->setParameter(
215+
'fos_rest.converter.request_body.class',
216+
'FOS\RestBundle\Request\RequestBodyParamConverter'
217+
);
218+
} else {
219+
$container->setParameter(
220+
'fos_rest.converter.request_body.class',
221+
'FOS\RestBundle\Request\RequestBodyParamConverter20'
222+
);
223+
}
224+
205225
$loader->load('request_body_param_converter.xml');
206226
}
207227

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Request;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
16+
17+
/**
18+
* This code is needed for SensioFrameworkExtraBundle 2.x compatibility
19+
* https://github.com/FriendsOfSymfony/FOSRestBundle/issues/622
20+
*
21+
* @author Tyler Stroud <[email protected]>
22+
*/
23+
class RequestBodyParamConverter20 extends AbstractRequestBodyParamConverter
24+
{
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function apply(Request $request, ConfigurationInterface $configuration)
29+
{
30+
return $this->execute($request, $configuration);
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public function supports(ConfigurationInterface $configuration)
37+
{
38+
return null !== $configuration->getClass();
39+
}
40+
}

Resources/config/request_body_param_converter.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

7-
<parameters>
8-
<parameter key="fos_rest.converter.request_body.class">FOS\RestBundle\Request\RequestBodyParamConverter</parameter>
9-
</parameters>
10-
117
<services>
128

139
<service id="fos_rest.converter.request_body" class="%fos_rest.converter.request_body.class%">
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15+
use Symfony\Component\HttpFoundation\Response;
16+
17+
class RequestBodyParamConverterController extends Controller
18+
{
19+
public function putPostAction(Post $post)
20+
{
21+
return new Response($post->getName());
22+
}
23+
}
24+
25+
class Post
26+
{
27+
private $name;
28+
private $body;
29+
30+
public function getName()
31+
{
32+
return $this->name;
33+
}
34+
35+
public function setName($name)
36+
{
37+
$this->name = $name;
38+
}
39+
40+
public function getBody()
41+
{
42+
return $this->body;
43+
}
44+
45+
public function setBody($body)
46+
{
47+
$this->body = $body;
48+
}
49+
}

Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
request_body_param_converter:
2+
path: /body-converter
3+
defaults: { _controller: TestBundle:RequestBodyParamConverter:putPost }
4+
15
test_serializer_error_exception:
26
path: /serializer-error/exception.{_format}
37
defaults: { _controller: TestBundle:SerializerError:exception }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Tests\Functional;
13+
14+
class RequestBodyParamConverterTest extends WebTestCase
15+
{
16+
public function testRequestBodyIsDeserialized()
17+
{
18+
$client = $this->createClient(array('test_case' => 'RequestBodyParamConverter'));
19+
$client->request(
20+
'POST',
21+
'/body-converter',
22+
array(),
23+
array(),
24+
array('CONTENT_TYPE' => 'application/json'),
25+
'{"name": "Post 1", "body": "This is a blog post"}'
26+
);
27+
28+
$this->assertSame(200, $client->getResponse()->getStatusCode());
29+
$this->assertSame('Post 1', $client->getResponse()->getContent());
30+
}
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return array(
4+
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
5+
new \FOS\RestBundle\FOSRestBundle(),
6+
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(),
7+
new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
8+
);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
framework:
5+
serializer:
6+
enabled: true
7+
8+
fos_rest:
9+
body_converter:
10+
enabled: true
11+
12+
sensio_framework_extra:
13+
request:
14+
converters: true
15+
16+
services:
17+
get_set_method_normalizer:
18+
class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
19+
tags:
20+
- { name: serializer.normalizer }

0 commit comments

Comments
 (0)