diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 5797f3f19f892..3ef2349a86aca 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -17,6 +17,7 @@ CHANGELOG * replaced Symfony\Bundle\FrameworkBundle\Controller\TraceableControllerResolver by Symfony\Component\HttpKernel\Controller\TraceableControllerResolver * replaced Symfony\Component\HttpKernel\Debug\ContainerAwareTraceableEventDispatcher by Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher * added Client::enableProfiler() + * added posibility to load the serializer component in the service container * A new parameter has been added to the DIC: `router.request_context.base_url` You can customize it for your functional tests or for generating urls with the right base url when your are in the cli context. diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php new file mode 100644 index 0000000000000..1a697c4bf0294 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\Reference; + +/** + * Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as + * encoders and normalizers to the Serializer service. + * + * @author Javier Lopez + */ +class SerializerPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition('serializer')) { + return; + } + + // Looks for all the services tagged "serializer.normalizer" and adds them to the Serializer service + $normalizers = $this->findAndSortTaggedServices('serializer.normalizer', $container); + $container->getDefinition('serializer')->replaceArgument(0, $normalizers); + + // Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service + $encoders = $this->findAndSortTaggedServices('serializer.encoder', $container); + $container->getDefinition('serializer')->replaceArgument(1, $encoders); + } + + private function findAndSortTaggedServices($tagName, ContainerBuilder $container) + { + $services = $container->findTaggedServiceIds($tagName); + + if (empty($services)) { + throw new \RuntimeException(sprintf('You must tag at least one service as "%s" to use the Serializer service', $tagName)); + } + + $sortedServices = array(); + foreach ($services as $serviceId => $tags) { + foreach ($tags as $tag) { + $priority = isset($tag['priority']) ? $tag['priority'] : 0; + $sortedServices[$priority][] = new Reference($serviceId); + } + } + + krsort($sortedServices); + + // Flatten the array + return call_user_func_array('array_merge', $sortedServices); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 70017d727baea..cbb08c73d93ad 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -80,6 +80,7 @@ public function getConfigTreeBuilder() $this->addTranslatorSection($rootNode); $this->addValidationSection($rootNode); $this->addAnnotationsSection($rootNode); + $this->addSerializerSection($rootNode); return $treeBuilder; } @@ -393,4 +394,17 @@ private function addAnnotationsSection(ArrayNodeDefinition $rootNode) ->end() ; } + + private function addSerializerSection(ArrayNodeDefinition $rootNode) + { + $rootNode + ->children() + ->arrayNode('serializer') + ->info('serializer configuration') + ->canBeDisabled() + ->end() + ->end() + ; + } + } diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 6423754059b24..040cd6719e9db 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -110,6 +110,10 @@ public function load(array $configs, ContainerBuilder $container) $this->registerTranslatorConfiguration($config['translator'], $container); } + if (isset($config['serializer'])) { + $this->registerSerializerConfiguration($config['serializer'], $loader); + } + $this->registerAnnotationsConfiguration($config['annotations'], $container, $loader); $this->addClassesToCompile(array( @@ -661,6 +665,19 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde } } + /** + * Loads the Serializer configuration. + * + * @param array $config A Serializer configuration array + * @param XmlFileLoader $loader An XmlFileLoader instance + */ + private function registerSerializerConfiguration(array $config, XmlFileLoader $loader) + { + if ($config['enabled']) { + $loader->load('serializer.xml'); + } + } + /** * Returns the base path for the XSD files. * diff --git a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php index c4caaea4e988a..10d0a90c96242 100644 --- a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php +++ b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php @@ -25,6 +25,7 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CompilerDebugDumpPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass; +use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\HttpRenderingStrategyPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\PassConfig; @@ -66,6 +67,7 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new AddCacheClearerPass()); $container->addCompilerPass(new TranslationExtractorPass()); $container->addCompilerPass(new TranslationDumperPass()); + $container->addCompilerPass(new SerializerPass()); $container->addCompilerPass(new HttpRenderingStrategyPass(), PassConfig::TYPE_AFTER_REMOVING); if ($container->getParameter('kernel.debug')) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml new file mode 100644 index 0000000000000..00f08ba539b03 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml @@ -0,0 +1,25 @@ + + + + + + Symfony\Component\Serializer\Serializer + Symfony\Component\Serializer\Encoder\XmlEncoder + Symfony\Component\Serializer\Encoder\JsonEncoder + + + + + + + + + + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php new file mode 100644 index 0000000000000..a65d069c0b127 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass; + +/** + * Tests for the SerializerPass class + * + * @author Javier Lopez + */ +class SerializerPassTest extends \PHPUnit_Framework_TestCase +{ + + public function testThrowExceptionWhenNoNormalizers() + { + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); + + $container->expects($this->once()) + ->method('hasDefinition') + ->with('serializer') + ->will($this->returnValue(true)); + + $container->expects($this->once()) + ->method('findTaggedServiceIds') + ->with('serializer.normalizer') + ->will($this->returnValue(array())); + + $this->setExpectedException('RuntimeException'); + + $serializerPass = new SerializerPass(); + $serializerPass->process($container); + } + + public function testThrowExceptionWhenNoEncoders() + { + $definition = $this->getMock('Symfony\Component\DependencyInjection\Definition'); + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); + + $container->expects($this->once()) + ->method('hasDefinition') + ->with('serializer') + ->will($this->returnValue(true)); + + $container->expects($this->any()) + ->method('findTaggedServiceIds') + ->will($this->onConsecutiveCalls( + array('n' => array('serializer.normalizer')), + array() + )); + + $container->expects($this->once()) + ->method('getDefinition') + ->will($this->returnValue($definition)); + + $this->setExpectedException('RuntimeException'); + + $serializerPass = new SerializerPass(); + $serializerPass->process($container); + } + + public function testServicesAreOrderedAccordingToPriority() + { + $services = array( + 'n3' => array('tag' => array()), + 'n1' => array('tag' => array('priority' => 200)), + 'n2' => array('tag' => array('priority' => 100)) + ); + + $expected = array( + new Reference('n1'), + new Reference('n2'), + new Reference('n3') + ); + + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); + + $container->expects($this->atLeastOnce()) + ->method('findTaggedServiceIds') + ->will($this->returnValue($services)); + + $serializerPass = new SerializerPass(); + + $method = new \ReflectionMethod( + 'Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass', + 'findAndSortTaggedServices' + ); + $method->setAccessible(TRUE); + + $actual = $method->invoke($serializerPass, 'tag', $container); + + $this->assertEquals($expected, $actual); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy