Skip to content
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

Use default Symfony serializer service when available #276

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions DependencyInjection/FOSJsRoutingExtension.php
Expand Up @@ -15,8 +15,10 @@
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Serializer\Serializer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why adding such use statement ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A rest from some refactoring, probably


/**
* FOSJsRoutingExtension
Expand Down
55 changes: 55 additions & 0 deletions DependencyInjection/SerializerCompilerPass.php
@@ -0,0 +1,55 @@
<?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;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;

/**
* Class SerializerCompilerPass
*
* @author Miguel Angel Garzón <magarzon@gmail.com>
*/
class SerializerCompilerPass implements CompilerPassInterface
{
const SERIALIZER_SERVICE_ID = 'fos_js_routing.serializer';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should not add such constant. There is no reason to make this public (and private constants are not available in the targeted PHP versions)


/**
* @inheritdoc
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
if ($container->hasDefinition(self::SERIALIZER_SERVICE_ID)
|| $container->hasAlias(self::SERIALIZER_SERVICE_ID)) {
return;
}

if ($container->hasDefinition('serializer')) {
$container->setAlias(self::SERIALIZER_SERVICE_ID, 'serializer');
} else {
$definition = $container->register(self::SERIALIZER_SERVICE_ID, Serializer::class);
$normalizers = [
$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', JsonEncoder::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::class is not OK either

$encoder->setPublic(false);
$definition->addArgument(['json' => $encoder]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the composer.json requires php >=5.3.2, so the short array syntax is not OK.

}
}
}
10 changes: 10 additions & 0 deletions FOSJsRoutingBundle.php
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
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
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;
}
}
64 changes: 64 additions & 0 deletions Tests/DependencyInjection/SerializerCompilerPassTest.php
@@ -0,0 +1,64 @@
<?php
namespace FOS\JsRoutingBundle\Tests\DependencyInjection;

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

/**
* Class SerializerCompilerPassTest
*
* @author Miguel Angel Garzón <magarzon@gmail.com>
*/
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([['serializer' => 'test.serializer.service']]);

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

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

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

$container->register('serializer', Serializer::class);

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

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

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

$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;
}
}