Skip to content

Commit

Permalink
Allow overriding serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Marechal committed Jun 30, 2014
1 parent 2776be3 commit 03e80ca
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
9 changes: 9 additions & 0 deletions DependencyInjection/BazingaHateoasExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Config\FileLocator;

/**
Expand Down Expand Up @@ -51,5 +52,13 @@ public function load(array $configs, ContainerBuilder $container)
new Alias($config['metadata']['cache'], false)
);
}

$container
->getDefinition('hateoas.event_subscriber.json')
->replaceArgument(0, new Reference($config['serializer']['json']));

$container
->getDefinition('hateoas.event_subscriber.xml')
->replaceArgument(0, new Reference($config['serializer']['xml']));
}
}
7 changes: 7 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('serializer')
->addDefaultsIfNotSet()
->children()
->scalarNode('json')->defaultValue('hateoas.serializer.json_hal')->end()
->scalarNode('xml')->defaultValue('hateoas.serializer.xml')->end()
->end()
->end()
;

return $treeBuilder;
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/serializer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@
<!-- Subscribers -->
<service id="hateoas.event_subscriber.xml" class="%hateoas.event_subscriber.xml.class%">
<tag name="jms_serializer.event_subscriber" />
<argument type="service" id="hateoas.serializer.xml" />
<argument /> <!-- xml serializer -->
<argument type="service" id="hateoas.links_factory" />
<argument type="service" id="hateoas.embeds_factory" />
</service>

<service id="hateoas.event_subscriber.json" class="%hateoas.event_subscriber.json.class%">
<tag name="jms_serializer.event_subscriber" />
<argument type="service" id="hateoas.serializer.json_hal" />
<argument /> <!-- json serializer -->
<argument type="service" id="hateoas.links_factory" />
<argument type="service" id="hateoas.embeds_factory" />
<argument type="service" id="hateoas.inline_deferrer.embeds" />
Expand Down
28 changes: 27 additions & 1 deletion Tests/DependencyInjection/BazingaHateoasExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ public function testLoadUrlGenerator()
$this->assertInstanceOf($urlGeneratorClass, $urlGenerator2);
}

public function testLoadSerializer()
{
$class = 'Bazinga\Bundle\HateoasBundle\Tests\Fixtures\JsonSerializer';
$container = $this->getContainerForConfig(array('bazinga_hateoas' => array('serializer' => array('json' => 'custom_serializer'))));
$container->setDefinition('custom_serializer', new Definition($class));
$container->compile();

$jsonListener = $container->get('hateoas.event_subscriber.json');

$reflClass = new \ReflectionClass($jsonListener);
$reflProp = $reflClass->getProperty('jsonSerializer');
$reflProp->setAccessible(true);

$this->assertInstanceOf($class, $reflProp->getValue($jsonListener));

$xmlListener = $container->get('hateoas.event_subscriber.xml');

$reflClass = new \ReflectionClass($xmlListener);
$reflProp = $reflClass->getProperty('xmlSerializer');
$reflProp->setAccessible(true);

$this->assertInstanceOf('Hateoas\Serializer\XmlSerializer', $reflProp->getValue($xmlListener));
}

private function clearTempDir()
{
// clear temporary directory
Expand Down Expand Up @@ -127,8 +151,10 @@ private function getContainerForConfig(array $configs, KernelInterface $kernel =
$container->setParameter('foo', 'bar');

foreach ($extensions as $extension) {
$extensionConfig = isset($configs[$extension->getAlias()]) ? $configs[$extension->getAlias()] : array();

$container->registerExtension($extension);
$extension->load($configs, $container);
$extension->load(array($extension->getAlias() => $extensionConfig), $container);
}

foreach ($bundles as $bundle) {
Expand Down
18 changes: 18 additions & 0 deletions Tests/Fixtures/JsonSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Bazinga\Bundle\HateoasBundle\Tests\Fixtures;

use Hateoas\Serializer\JsonSerializerInterface;
use JMS\Serializer\JsonSerializationVisitor;
use JMS\Serializer\SerializationContext;

class JsonSerializer implements JsonSerializerInterface
{
public function serializeLinks(array $links, JsonSerializationVisitor $visitor, SerializationContext $context)
{
}

public function serializeEmbeddeds(array $embeddeds, JsonSerializationVisitor $visitor, SerializationContext $context)
{
}
}

0 comments on commit 03e80ca

Please sign in to comment.