Skip to content

Commit

Permalink
Merge pull request #1057 from xabbuh/issue-1045
Browse files Browse the repository at this point in the history
SensioFrameworkExtraBundle 2.0.x compatibility
  • Loading branch information
lsmith77 committed Jun 4, 2015
2 parents ea4f6f6 + 75a46a5 commit 46a1870
Show file tree
Hide file tree
Showing 11 changed files with 487 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ matrix:
env: deps="low"
- php: 5.5
env: SYMFONY_VERSION='2.3.* symfony/expression-language:2.4.*'
- php: 5.5
env: SYMFONY_VERSION='2.3.* sensio/framework-extra-bundle:2.*'
- php: 5.5
env: SYMFONY_VERSION=2.4.*
- php: 5.5
Expand Down
20 changes: 20 additions & 0 deletions DependencyInjection/FOSRestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ private function loadBodyConverter(array $config, $validator, XmlFileLoader $loa
{
if (!empty($config['body_converter'])) {
if (!empty($config['body_converter']['enabled'])) {
$parameter = new \ReflectionParameter(
array(
'Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface',
'supports',
),
'configuration'
);

if ('Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter' === $parameter->getClass()->getName()) {
$container->setParameter(
'fos_rest.converter.request_body.class',
'FOS\RestBundle\Request\RequestBodyParamConverter'
);
} else {
$container->setParameter(
'fos_rest.converter.request_body.class',
'FOS\RestBundle\Request\RequestBodyParamConverter20'
);
}

$loader->load('request_body_param_converter.xml');
}

Expand Down
40 changes: 40 additions & 0 deletions Request/RequestBodyParamConverter20.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Request;

use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;

/**
* This code is needed for SensioFrameworkExtraBundle 2.x compatibility
* https://github.com/FriendsOfSymfony/FOSRestBundle/issues/622
*
* @author Tyler Stroud <[email protected]>
*/
class RequestBodyParamConverter20 extends AbstractRequestBodyParamConverter
{
/**
* {@inheritDoc}
*/
public function apply(Request $request, ConfigurationInterface $configuration)
{
return $this->execute($request, $configuration);
}

/**
* {@inheritDoc}
*/
public function supports(ConfigurationInterface $configuration)
{
return null !== $configuration->getClass();
}
}
4 changes: 0 additions & 4 deletions Resources/config/request_body_param_converter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="fos_rest.converter.request_body.class">FOS\RestBundle\Request\RequestBodyParamConverter</parameter>
</parameters>

<services>

<service id="fos_rest.converter.request_body" class="%fos_rest.converter.request_body.class%">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class RequestBodyParamConverterController extends Controller
{
public function putPostAction(Post $post)
{
return new Response($post->getName());
}
}

class Post
{
private $name;
private $body;

public function getName()
{
return $this->name;
}

public function setName($name)
{
$this->name = $name;
}

public function getBody()
{
return $this->body;
}

public function setBody($body)
{
$this->body = $body;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
request_body_param_converter:
path: /body-converter
defaults: { _controller: TestBundle:RequestBodyParamConverter:putPost }

test_serializer_error_exception:
path: /serializer-error/exception.{_format}
defaults: { _controller: TestBundle:SerializerError:exception }
Expand Down
31 changes: 31 additions & 0 deletions Tests/Functional/RequestBodyParamConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Tests\Functional;

class RequestBodyParamConverterTest extends WebTestCase
{
public function testRequestBodyIsDeserialized()
{
$client = $this->createClient(array('test_case' => 'RequestBodyParamConverter'));
$client->request(
'POST',
'/body-converter',
array(),
array(),
array('CONTENT_TYPE' => 'application/json'),
'{"name": "Post 1", "body": "This is a blog post"}'
);

$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertSame('Post 1', $client->getResponse()->getContent());
}
}
8 changes: 8 additions & 0 deletions Tests/Functional/app/RequestBodyParamConverter/bundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return array(
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FOS\RestBundle\FOSRestBundle(),
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(),
new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
);
20 changes: 20 additions & 0 deletions Tests/Functional/app/RequestBodyParamConverter/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
imports:
- { resource: ../config/default.yml }

framework:
serializer:
enabled: true

fos_rest:
body_converter:
enabled: true

sensio_framework_extra:
request:
converters: true

services:
get_set_method_normalizer:
class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
tags:
- { name: serializer.normalizer }
Loading

0 comments on commit 46a1870

Please sign in to comment.