-
Notifications
You must be signed in to change notification settings - Fork 261
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
$encoder->setPublic(false); | ||
$definition->addArgument(['json' => $encoder]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the composer.json requires |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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