RegisterEncodersPass.php 1.0 KB

1234567891011121314151617181920212223242526272829
  1. <?php
  2. namespace JMS\SerializerBundle\DependencyInjection\Compiler;
  3. use JMS\SerializerBundle\Exception\RuntimeException;
  4. use Symfony\Component\DependencyInjection\ContainerBuilder;
  5. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  6. class RegisterEncodersPass implements CompilerPassInterface
  7. {
  8. public function process(ContainerBuilder $container)
  9. {
  10. $encoders = array();
  11. foreach ($container->findTaggedServiceIds('jms_serializer.encoder') as $id => $attributes) {
  12. if (!isset($attributes[0]['format'])) {
  13. throw new RuntimeException(sprintf('"format" attribute must be specified for service "%s" and tag "jms_serializer.encoder".', $id));
  14. }
  15. $encoders[$attributes[0]['format']] = $id;
  16. }
  17. foreach (array_keys($container->findTaggedServiceIds('jms_serializer.serializer')) as $id) {
  18. $container
  19. ->getDefinition($id)
  20. ->addArgument($encoders)
  21. ;
  22. }
  23. }
  24. }