Skip to content

Use default Symfony serializer service when available #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions DependencyInjection/SerializerCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the FOSJsRoutingBundle 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\JsRoutingBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Class SerializerCompilerPass
*
* @author Miguel Angel Garzón <[email protected]>
*/
class SerializerCompilerPass implements CompilerPassInterface
{
/**
* @inheritdoc
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
if ($container->hasDefinition('fos_js_routing.serializer')
|| $container->hasAlias('fos_js_routing.serializer')) {
return;
}

if ($container->hasDefinition('serializer')) {
$container->setAlias('fos_js_routing.serializer', 'serializer');
} else {
$definition = $container->register('fos_js_routing.serializer', 'Symfony\Component\Serializer\Serializer');
$normalizers = array(
$container->getDefinition('fos_js_routing.normalizer.route_collection'),
$container->getDefinition('fos_js_routing.normalizer.routes_response'),
$container->getDefinition('fos_js_routing.denormalizer.route_collection')
);
$definition->addArgument($normalizers);

$encoder = $container->register('fos_js_routing.encoder', 'Symfony\Component\Serializer\Encoder\JsonEncoder');
$encoder->setPublic(false);
$definition->addArgument(array('json' => $encoder));
}
}
}
10 changes: 10 additions & 0 deletions FOSJsRoutingBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace FOS\JsRoutingBundle;

use FOS\JsRoutingBundle\DependencyInjection\SerializerCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
Expand All @@ -20,4 +22,12 @@
*/
class FOSJsRoutingBundle extends Bundle
{
/**
* @param ContainerBuilder $container
*/
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new SerializerCompilerPass());
}
}
23 changes: 8 additions & 15 deletions Resources/config/serializer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,15 @@
</parameters>

<services>
<service id="fos_js_routing.serializer" class="Symfony\Component\Serializer\Serializer" public="true">
<argument type="collection">
<argument type="service" id="fos_js_routing.normalizer.route_collection" />
<argument type="service" id="fos_js_routing.normalizer.routes_response" />
<argument type="service" id="fos_js_routing.denormalizer.route_collection" />
</argument>
<argument type="collection">
<argument key="json" type="service" id="fos_js_routing.encoder" />
</argument>
<service id="fos_js_routing.normalizer.route_collection" class="%fos_js_routing.normalizer.route_collection.class%" public="false">
<tag name="serializer.normalizer" />
</service>
<service id="fos_js_routing.normalizer.routes_response" class="%fos_js_routing.normalizer.routes_response.class%" public="false">
<tag name="serializer.normalizer" />
</service>

<service id="fos_js_routing.normalizer.route_collection" class="%fos_js_routing.normalizer.route_collection.class%" public="false" />
<service id="fos_js_routing.normalizer.routes_response" class="%fos_js_routing.normalizer.routes_response.class%" public="false" />

<service id="fos_js_routing.denormalizer.route_collection" class="%fos_js_routing.denormalizer.route_collection.class%" public="false" />

<service id="fos_js_routing.encoder" class="Symfony\Component\Serializer\Encoder\JsonEncoder" public="false" />
<service id="fos_js_routing.denormalizer.route_collection" class="%fos_js_routing.denormalizer.route_collection.class%" public="false">
<tag name="serializer.normalizer" />
</service>
</services>
</container>
4 changes: 4 additions & 0 deletions Tests/DependencyInjection/FOSJsRoutingExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FOS\JsRoutingBundle\Tests\DependencyInjection;

use FOS\JsRoutingBundle\DependencyInjection\FOSJsRoutingExtension;
use FOS\JsRoutingBundle\DependencyInjection\SerializerCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class FOSJsRoutingExtensionTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -60,6 +61,9 @@ private function load(array $configs)
$extension = new FOSJsRoutingExtension();
$extension->load($configs, $container);

$compilerPass = new SerializerCompilerPass();
$compilerPass->process($container);

return $container;
}
}
63 changes: 63 additions & 0 deletions Tests/DependencyInjection/SerializerCompilerPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace FOS\JsRoutingBundle\Tests\DependencyInjection;

use FOS\JsRoutingBundle\DependencyInjection\FOSJsRoutingExtension;
use FOS\JsRoutingBundle\DependencyInjection\SerializerCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Class SerializerCompilerPassTest
*
* @author Miguel Angel Garzón <[email protected]>
*/
class SerializerCompilerPassTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!class_exists('Symfony\Component\DependencyInjection\ContainerBuilder')) {
$this->markTestSkipped('The DependencyInjection component is not available.');
}
}

public function testSerializerInConfig()
{
$container = $this->load(array(array('serializer' => 'test.serializer.service')));

$compilerPass = new SerializerCompilerPass();
$compilerPass->process($container);

$this->assertTrue($container->hasAlias('fos_js_routing.serializer'));
}

public function testSerializerDefined()
{
$container = $this->load(array());

$container->register('serializer', 'Symfony\Component\Serializer\Serializer');

$compilerPass = new SerializerCompilerPass();
$compilerPass->process($container);

$this->assertTrue($container->hasAlias('fos_js_routing.serializer'));
}

public function testSerializerNotDefined()
{
$container = $this->load(array());

$compilerPass = new SerializerCompilerPass();
$compilerPass->process($container);

$this->assertTrue($container->hasDefinition('fos_js_routing.serializer'));
}

private function load(array $configs)
{
$container = new ContainerBuilder();

$extension = new FOSJsRoutingExtension();
$extension->load($configs, $container);

return $container;
}
}