AddFormExtensionCompilerPass.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. /**
  17. * Add all dependencies to the Admin class, this avoid to write to many lines
  18. * in the configuration files.
  19. *
  20. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  21. */
  22. class AddFormExtensionCompilerPass implements CompilerPassInterface
  23. {
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function process(ContainerBuilder $container)
  28. {
  29. $ignores = array('form', 'csrf');
  30. foreach ($container->findTaggedServiceIds('form.type') as $id => $attributes) {
  31. $name = isset($attributes[0]['alias']) ? $attributes[0]['alias'] : false;
  32. if (!$name || in_array($name, $ignores)) {
  33. continue;
  34. }
  35. $definition = new Definition('Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension', array($name));
  36. $definition->addTag('form.type_extension', array('alias' => $name));
  37. $container->setDefinition(sprintf('sonata.admin.form.extension.%s', $name), $definition);
  38. }
  39. // append the extension
  40. $typeExtensions = array();
  41. foreach ($container->findTaggedServiceIds('form.type_extension') as $serviceId => $tag) {
  42. $alias = isset($tag[0]['alias'])
  43. ? $tag[0]['alias']
  44. : $serviceId;
  45. $typeExtensions[$alias][] = $serviceId;
  46. }
  47. $container->getDefinition('form.extension')->replaceArgument(2, $typeExtensions);
  48. }
  49. }