From ca250b0fae4a1ccfa103f4f05fadb87fd2271b42 Mon Sep 17 00:00:00 2001 From: loalf Date: Sat, 25 Aug 2012 19:32:54 +0100 Subject: [PATCH 01/18] Adding the Serializer service to the Framework Bundle --- .../DependencyInjection/Configuration.php | 19 ++++++++++++ .../FrameworkExtension.php | 17 ++++++++++ .../FrameworkBundle/FrameworkBundle.php | 2 ++ .../Resources/config/serializer.xml | 31 +++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index e5c1387cfc47f..420d0aa044f0a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -78,6 +78,7 @@ public function getConfigTreeBuilder() $this->addTranslatorSection($rootNode); $this->addValidationSection($rootNode); $this->addAnnotationsSection($rootNode); + $this->addSerializerSection($rootNode); return $treeBuilder; } @@ -409,4 +410,22 @@ private function addAnnotationsSection(ArrayNodeDefinition $rootNode) ->end() ; } + + private function addSerializerSection(ArrayNodeDefinition $rootNode) + { + $rootNode + ->children() + ->arrayNode('serializer') + ->info('serializer configuration') + ->canBeUnset() + ->treatNullLike(array('enabled' => true)) + ->treatTrueLike(array('enabled' => true)) + ->children() + ->booleanNode('enabled')->defaultTrue()->end() + ->end() + ->end() + ->end() + ; + } + } diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index d9eed7d87abb0..5bc066fc51c74 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -102,6 +102,10 @@ public function load(array $configs, ContainerBuilder $container) $this->registerTranslatorConfiguration($config['translator'], $container); } + if (isset($config['serializer'])) { + $this->registerSerializerConfiguration($config['serializer'], $container, $loader); + } + $this->registerAnnotationsConfiguration($config['annotations'], $container, $loader); $this->addClassesToCompile(array( @@ -639,6 +643,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 (!empty($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 544f2026baf27..46d6d6b1798a8 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\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\Scope; @@ -63,6 +64,7 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new AddCacheClearerPass()); $container->addCompilerPass(new TranslationExtractorPass()); $container->addCompilerPass(new TranslationDumperPass()); + $container->addCompilerPass(new SerializerPass()); if ($container->getParameter('kernel.debug')) { $container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING); 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..af643f0b1a0c0 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml @@ -0,0 +1,31 @@ + + + + + + Symfony\Component\Serializer\Serializer + Symfony\Component\Serializer\Encoder\XmlEncoder + Symfony\Component\Serializer\Encoder\JsonEncoder + Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer + + + + + + + + + + + + + + + + + + + From 0b514715a4a0f88037f26a17915328e704bf7938 Mon Sep 17 00:00:00 2001 From: loalf Date: Sat, 25 Aug 2012 19:34:03 +0100 Subject: [PATCH 02/18] Adding the Compiler Pass to add encoders and normalizers as tagged services --- .../Compiler/SerializerPass.php | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php 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..61c16a144a870 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -0,0 +1,68 @@ + + * + * 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; + +/** + * 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 = array(); + + $min_priority = 0; + foreach ($container->findTaggedServiceIds('serializer.normalizer') as $serviceId => $tag) { + + if(isset($tag[0]['priority']){ + $priority = $tag[0]['priority']; + }else{ + $priority = $min_priority; + $min_priority++; + } + + $encoders[$priority] = $serviceId; + } + + + $container->getDefinition('serializer')->replaceArgument(1, $normalizers); + + // Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service + $encoders = array(); + + $min_priority = 0; + foreach ($container->findTaggedServiceIds('serializer.normalizer') as $serviceId => $tag) { + + if(isset($tag[0]['priority']){ + $priority = $tag[0]['priority']; + }else{ + $priority = $min_priority; + $min_priority++; + } + + $encoders[$priority] = $serviceId; + } + + $container->getDefinition('serializer')->replaceArgument(2, $encoders); + } +} From 7b791fef8c734817d80a825ad6aa2416a3303112 Mon Sep 17 00:00:00 2001 From: loalf Date: Sat, 25 Aug 2012 21:22:33 +0100 Subject: [PATCH 03/18] Fixing some issues with SerializerPass --- .../Compiler/SerializerPass.php | 18 ++++++++---------- .../DependencyInjection/FrameworkExtension.php | 2 +- .../Resources/config/serializer.xml | 8 +++++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index 61c16a144a870..3ada14b31e646 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -13,7 +13,7 @@ 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. @@ -33,36 +33,34 @@ public function process(ContainerBuilder $container) $min_priority = 0; foreach ($container->findTaggedServiceIds('serializer.normalizer') as $serviceId => $tag) { - - if(isset($tag[0]['priority']){ + if(isset($tag[0]['priority'])){ $priority = $tag[0]['priority']; }else{ $priority = $min_priority; $min_priority++; } - $encoders[$priority] = $serviceId; + $normalizers[$priority] = new Reference($serviceId); } - - $container->getDefinition('serializer')->replaceArgument(1, $normalizers); + $container->getDefinition('serializer')->replaceArgument(0, $normalizers); // Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service $encoders = array(); $min_priority = 0; - foreach ($container->findTaggedServiceIds('serializer.normalizer') as $serviceId => $tag) { + foreach ($container->findTaggedServiceIds('serializer.encoder') as $serviceId => $tag) { - if(isset($tag[0]['priority']){ + if(isset($tag[0]['priority'])){ $priority = $tag[0]['priority']; }else{ $priority = $min_priority; $min_priority++; } - $encoders[$priority] = $serviceId; + $encoders[$priority] = new Reference($serviceId); } - $container->getDefinition('serializer')->replaceArgument(2, $encoders); + $container->getDefinition('serializer')->replaceArgument(1, $encoders); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 5bc066fc51c74..bc5f2fb3ce4f1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -103,7 +103,7 @@ public function load(array $configs, ContainerBuilder $container) } if (isset($config['serializer'])) { - $this->registerSerializerConfiguration($config['serializer'], $container, $loader); + $this->registerSerializerConfiguration($config['serializer'], $loader); } $this->registerAnnotationsConfiguration($config['annotations'], $container, $loader); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml index af643f0b1a0c0..c02a350f04bfb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml @@ -13,7 +13,9 @@ - + + + @@ -22,9 +24,9 @@ - + - From fb1685cf6f680e04975682b3769dd3596b7bdfd9 Mon Sep 17 00:00:00 2001 From: loalf Date: Sat, 25 Aug 2012 21:26:01 +0100 Subject: [PATCH 04/18] Now it sorts the encoders and normalizers depending on its priority --- .../DependencyInjection/Compiler/SerializerPass.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index 3ada14b31e646..8d567eeaffc0e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -43,6 +43,7 @@ public function process(ContainerBuilder $container) $normalizers[$priority] = new Reference($serviceId); } + krsort($normalizers); $container->getDefinition('serializer')->replaceArgument(0, $normalizers); // Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service @@ -61,6 +62,7 @@ public function process(ContainerBuilder $container) $encoders[$priority] = new Reference($serviceId); } + krsort($encoders); $container->getDefinition('serializer')->replaceArgument(1, $encoders); } } From 881e9a681a43f0c2764739c12fcc461cdc0bfe75 Mon Sep 17 00:00:00 2001 From: loalf Date: Sat, 25 Aug 2012 22:24:59 +0100 Subject: [PATCH 05/18] Updated serializer.xml --- .../Bundle/FrameworkBundle/Resources/config/serializer.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml index c02a350f04bfb..c3d80cd5ea38b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml @@ -13,9 +13,7 @@ - - - + From 0278df806dfdfa3cd328193cccc7c7d3b8935dc5 Mon Sep 17 00:00:00 2001 From: loalf Date: Sun, 26 Aug 2012 01:03:19 +0100 Subject: [PATCH 06/18] Getting rid of the priorities --- .../Compiler/SerializerPass.php | 24 +++---------------- .../Resources/config/serializer.xml | 6 ++--- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index 8d567eeaffc0e..25934bf443b4d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -14,6 +14,7 @@ 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. @@ -31,38 +32,19 @@ public function process(ContainerBuilder $container) // Looks for all the services tagged "serializer.normalizer" and adds them to the Serializer service $normalizers = array(); - $min_priority = 0; foreach ($container->findTaggedServiceIds('serializer.normalizer') as $serviceId => $tag) { - if(isset($tag[0]['priority'])){ - $priority = $tag[0]['priority']; - }else{ - $priority = $min_priority; - $min_priority++; - } - - $normalizers[$priority] = new Reference($serviceId); + $normalizers[] = new Reference($serviceId); } - krsort($normalizers); $container->getDefinition('serializer')->replaceArgument(0, $normalizers); // Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service $encoders = array(); - $min_priority = 0; foreach ($container->findTaggedServiceIds('serializer.encoder') as $serviceId => $tag) { - - if(isset($tag[0]['priority'])){ - $priority = $tag[0]['priority']; - }else{ - $priority = $min_priority; - $min_priority++; - } - - $encoders[$priority] = new Reference($serviceId); + $encoders[] = new Reference($serviceId); } - krsort($encoders); $container->getDefinition('serializer')->replaceArgument(1, $encoders); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml index c3d80cd5ea38b..eb735a62aaa64 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml @@ -18,14 +18,14 @@ - + - + - + From e579aacfe38a518d26f83795af3c6baf8c293ae5 Mon Sep 17 00:00:00 2001 From: loalf Date: Mon, 3 Sep 2012 20:57:44 +0100 Subject: [PATCH 07/18] Added priority to the services according to the Symfony standards --- .../Compiler/SerializerPass.php | 29 ++++++++++++------- .../Resources/config/serializer.xml | 6 ++-- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index 25934bf443b4d..0c479c77ffcfb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -30,21 +30,30 @@ public function process(ContainerBuilder $container) } // Looks for all the services tagged "serializer.normalizer" and adds them to the Serializer service - $normalizers = array(); - - foreach ($container->findTaggedServiceIds('serializer.normalizer') as $serviceId => $tag) { - $normalizers[] = new Reference($serviceId); - } - + $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 = array(); + $encoders = $this->findAndSortTaggedServices('serializer.encoder', $container); + $container->getDefinition('serializer')->replaceArgument(1, $encoders); + } - foreach ($container->findTaggedServiceIds('serializer.encoder') as $serviceId => $tag) { - $encoders[] = new Reference($serviceId); + private function findAndSortTaggedServices($tag, $container) + { + // Find tagged services + $servs = array(); + foreach ($container->findTaggedServiceIds($tag) as $serviceId => $value) { + $priority = isset($value[0]['priority']) ? $value[0]['priority'] : 0; + $servs[$priority][] = new Reference($serviceId); } - $container->getDefinition('serializer')->replaceArgument(1, $encoders); + // Sort them + krsort($servs); + + // Flatten the array + $services = array(); + array_walk_recursive($servs, function($a) use (&$services) { $services[] = $a; }); + + return $services; } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml index eb735a62aaa64..10a28ad3e0bcc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml @@ -18,14 +18,14 @@ - + - + - + From 91812e20993dc1b49b94ed660027bd3d67d39e25 Mon Sep 17 00:00:00 2001 From: loalf Date: Mon, 3 Sep 2012 21:23:04 +0100 Subject: [PATCH 08/18] Iterating through all the tags on the service --- .../DependencyInjection/Compiler/SerializerPass.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index 0c479c77ffcfb..0e062c4602835 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -38,13 +38,15 @@ public function process(ContainerBuilder $container) $container->getDefinition('serializer')->replaceArgument(1, $encoders); } - private function findAndSortTaggedServices($tag, $container) + private function findAndSortTaggedServices($tag_name, $container) { // Find tagged services $servs = array(); - foreach ($container->findTaggedServiceIds($tag) as $serviceId => $value) { - $priority = isset($value[0]['priority']) ? $value[0]['priority'] : 0; - $servs[$priority][] = new Reference($serviceId); + foreach ($container->findTaggedServiceIds($tag_name) as $serviceId => $tags) { + foreach($tags as $tag) { + $priority = isset($tag['priority']) ? $tag['priority'] : 0; + $servs[$priority][] = new Reference($serviceId); + } } // Sort them @@ -53,7 +55,7 @@ private function findAndSortTaggedServices($tag, $container) // Flatten the array $services = array(); array_walk_recursive($servs, function($a) use (&$services) { $services[] = $a; }); - + return $services; } } From 4d6fdc049858473c74002d30ac0c128ebdd40bc3 Mon Sep 17 00:00:00 2001 From: loalf Date: Mon, 3 Sep 2012 22:11:01 +0100 Subject: [PATCH 09/18] Simplyfing the way to flatten the array acording to @stof comments --- .../DependencyInjection/Compiler/SerializerPass.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index 0e062c4602835..af8d60b6cd037 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -41,20 +41,21 @@ public function process(ContainerBuilder $container) private function findAndSortTaggedServices($tag_name, $container) { // Find tagged services - $servs = array(); + $services = array(); foreach ($container->findTaggedServiceIds($tag_name) as $serviceId => $tags) { foreach($tags as $tag) { $priority = isset($tag['priority']) ? $tag['priority'] : 0; - $servs[$priority][] = new Reference($serviceId); + $services[$priority][] = new Reference($serviceId); } } // Sort them - krsort($servs); + krsort($services); // Flatten the array - $services = array(); - array_walk_recursive($servs, function($a) use (&$services) { $services[] = $a; }); + if(!empty($services)) { + $services = call_user_func_array('array_merge', $services); + } return $services; } From d16480609bf59c3007a833eaa011e7ae6a673db4 Mon Sep 17 00:00:00 2001 From: loalf Date: Thu, 18 Oct 2012 19:23:28 +0100 Subject: [PATCH 10/18] Using camel case and typehinting the container --- .../DependencyInjection/Compiler/SerializerPass.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index af8d60b6cd037..43174607b0f40 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -38,11 +38,11 @@ public function process(ContainerBuilder $container) $container->getDefinition('serializer')->replaceArgument(1, $encoders); } - private function findAndSortTaggedServices($tag_name, $container) + private function findAndSortTaggedServices($tagName, ContainerBuilder $container) { // Find tagged services $services = array(); - foreach ($container->findTaggedServiceIds($tag_name) as $serviceId => $tags) { + foreach ($container->findTaggedServiceIds($tagName) as $serviceId => $tags) { foreach($tags as $tag) { $priority = isset($tag['priority']) ? $tag['priority'] : 0; $services[$priority][] = new Reference($serviceId); From b343488caddf01bbb148a5a7f8fc16311110a978 Mon Sep 17 00:00:00 2001 From: loalf Date: Thu, 18 Oct 2012 19:25:29 +0100 Subject: [PATCH 11/18] Remove priority in encoders and normalizer as this is irrelevant --- .../Bundle/FrameworkBundle/Resources/config/serializer.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml index 10a28ad3e0bcc..eb735a62aaa64 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml @@ -18,14 +18,14 @@ - + - + - + From 75d7ed95da2b471b56c97296176842fc5dfc01ec Mon Sep 17 00:00:00 2001 From: loalf Date: Thu, 18 Oct 2012 19:37:26 +0100 Subject: [PATCH 12/18] Using canBeDisabled() --- .../DependencyInjection/Configuration.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 99870d412ac0b..20b9ca9ab8fd4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -398,13 +398,8 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode) ->children() ->arrayNode('serializer') ->info('serializer configuration') - ->canBeUnset() - ->treatNullLike(array('enabled' => true)) - ->treatTrueLike(array('enabled' => true)) - ->children() - ->booleanNode('enabled')->defaultTrue()->end() - ->end() - ->end() + ->canBeDisabled() + ->end() ->end() ; } From 8ccb53043fb386da4bc5674e118d581d2325692d Mon Sep 17 00:00:00 2001 From: loalf Date: Thu, 18 Oct 2012 19:40:58 +0100 Subject: [PATCH 13/18] Modifying CHANGELOG to reflect the new feature --- src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index f652d53195b71..5a5b858f79e58 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -7,6 +7,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 2.1.0 ----- From 299c03ea811ccca995d61081a0cea8bd5814a855 Mon Sep 17 00:00:00 2001 From: loalf Date: Sat, 20 Oct 2012 10:50:51 +0100 Subject: [PATCH 14/18] Missing space after 'if' --- .../DependencyInjection/Compiler/SerializerPass.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index 43174607b0f40..b8c72e588d320 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -53,7 +53,7 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container krsort($services); // Flatten the array - if(!empty($services)) { + if (!empty($services)) { $services = call_user_func_array('array_merge', $services); } From c6253ce8f0c5140e00adfbafc4174577877c4440 Mon Sep 17 00:00:00 2001 From: loalf Date: Sat, 25 Aug 2012 19:32:54 +0100 Subject: [PATCH 15/18] Adding the Serializer service to the Framework Bundle --- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../Compiler/SerializerPass.php | 62 +++++++++++++++++++ .../DependencyInjection/Configuration.php | 14 +++++ .../FrameworkExtension.php | 17 +++++ .../FrameworkBundle/FrameworkBundle.php | 2 + .../Resources/config/serializer.xml | 26 ++++++++ 6 files changed, 122 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index f652d53195b71..5a5b858f79e58 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -7,6 +7,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 2.1.0 ----- 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..b8c72e588d320 --- /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) + { + // Find tagged services + $services = array(); + foreach ($container->findTaggedServiceIds($tagName) as $serviceId => $tags) { + foreach($tags as $tag) { + $priority = isset($tag['priority']) ? $tag['priority'] : 0; + $services[$priority][] = new Reference($serviceId); + } + } + + // Sort them + krsort($services); + + // Flatten the array + if (!empty($services)) { + $services = call_user_func_array('array_merge', $services); + } + + return $services; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 4ff15f16e73d3..20b9ca9ab8fd4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -78,6 +78,7 @@ public function getConfigTreeBuilder() $this->addTranslatorSection($rootNode); $this->addValidationSection($rootNode); $this->addAnnotationsSection($rootNode); + $this->addSerializerSection($rootNode); return $treeBuilder; } @@ -390,4 +391,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 ad1de4438cba7..72cc3ad12edc8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -106,6 +106,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( @@ -650,6 +654,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 (!empty($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 544f2026baf27..46d6d6b1798a8 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\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\Scope; @@ -63,6 +64,7 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new AddCacheClearerPass()); $container->addCompilerPass(new TranslationExtractorPass()); $container->addCompilerPass(new TranslationDumperPass()); + $container->addCompilerPass(new SerializerPass()); if ($container->getParameter('kernel.debug')) { $container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING); 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..491ccbcf2cf53 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml @@ -0,0 +1,26 @@ + + + + + + Symfony\Component\Serializer\Serializer + Symfony\Component\Serializer\Encoder\XmlEncoder + Symfony\Component\Serializer\Encoder\JsonEncoder + + + + + + + + + + + + + + + + From 731a05e5f2a965095a134d41423a54f77588ab17 Mon Sep 17 00:00:00 2001 From: loalf Date: Sun, 16 Dec 2012 21:00:21 +0000 Subject: [PATCH 16/18] SerializerPass throws an exception if no encoders or normalizers are tagged --- .../Compiler/SerializerPass.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index b8c72e588d320..990b638b421ad 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -40,23 +40,23 @@ public function process(ContainerBuilder $container) private function findAndSortTaggedServices($tagName, ContainerBuilder $container) { - // Find tagged services - $services = array(); - foreach ($container->findTaggedServiceIds($tagName) as $serviceId => $tags) { + $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; - $services[$priority][] = new Reference($serviceId); + $sortedServices[$priority][] = new Reference($serviceId); } } - // Sort them - krsort($services); + krsort($sortedServices); // Flatten the array - if (!empty($services)) { - $services = call_user_func_array('array_merge', $services); - } - - return $services; + return call_user_func_array('array_merge', $sortedServices); } } From 627b69cd482082e34d0d42dcace6b98619fbd3d2 Mon Sep 17 00:00:00 2001 From: loalf Date: Sun, 16 Dec 2012 21:01:04 +0000 Subject: [PATCH 17/18] Adding functional testing for the SerializerPass class --- .../Compiler/SerializerPassTest.php | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php 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..de1a348770b17 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php @@ -0,0 +1,106 @@ + + * + * 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); + } +} From 8cb0564151d7b513408e16fd670e3f89b3b6d279 Mon Sep 17 00:00:00 2001 From: loalf Date: Sat, 19 Jan 2013 20:24:26 +0000 Subject: [PATCH 18/18] Fixed minor issues --- .../DependencyInjection/Compiler/SerializerPass.php | 6 +++--- .../DependencyInjection/FrameworkExtension.php | 2 +- .../DependencyInjection/Compiler/SerializerPassTest.php | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index 990b638b421ad..1a697c4bf0294 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -42,13 +42,13 @@ 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)); + 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) { + foreach ($tags as $tag) { $priority = isset($tag['priority']) ? $tag['priority'] : 0; $sortedServices[$priority][] = new Reference($serviceId); } diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 19e0b9b81c943..040cd6719e9db 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -673,7 +673,7 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde */ private function registerSerializerConfiguration(array $config, XmlFileLoader $loader) { - if (!empty($config['enabled'])) { + if ($config['enabled']) { $loader->load('serializer.xml'); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php index de1a348770b17..a65d069c0b127 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php @@ -23,7 +23,6 @@ class SerializerPassTest extends \PHPUnit_Framework_TestCase { - public function testThrowExceptionWhenNoNormalizers() { $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); 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