FormPass.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. /**
  14. * Adds all services with the tags "form.type" and "form.type_guesser" as
  15. * arguments of the "form.extension" service.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. class FormPass implements CompilerPassInterface
  20. {
  21. public function process(ContainerBuilder $container)
  22. {
  23. if (!$container->hasDefinition('form.extension')) {
  24. return;
  25. }
  26. $definition = $container->getDefinition('form.extension');
  27. // Builds an array with service IDs as keys and tag aliases as values
  28. $types = array();
  29. foreach ($container->findTaggedServiceIds('form.type') as $serviceId => $tag) {
  30. $serviceDefinition = $container->getDefinition($serviceId);
  31. if (!$serviceDefinition->isPublic()) {
  32. throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form types are lazy-loaded.', $serviceId));
  33. }
  34. // The following if-else block is deprecated and will be removed
  35. // in Symfony 3.0
  36. // Deprecation errors are triggered in the form registry
  37. if (isset($tag[0]['alias'])) {
  38. $types[$tag[0]['alias']] = $serviceId;
  39. } else {
  40. $types[$serviceId] = $serviceId;
  41. }
  42. // Support type access by FQCN
  43. $types[$serviceDefinition->getClass()] = $serviceId;
  44. }
  45. $definition->replaceArgument(1, $types);
  46. $typeExtensions = array();
  47. foreach ($container->findTaggedServiceIds('form.type_extension') as $serviceId => $tag) {
  48. $serviceDefinition = $container->getDefinition($serviceId);
  49. if (!$serviceDefinition->isPublic()) {
  50. throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form type extensions are lazy-loaded.', $serviceId));
  51. }
  52. if (isset($tag[0]['extended_type'])) {
  53. $extendedType = $tag[0]['extended_type'];
  54. } elseif (isset($tag[0]['alias'])) {
  55. @trigger_error(sprintf('The alias option of the form.type_extension tag of service "%s" is deprecated since Symfony 2.8 and will be removed in 3.0. Use the extended_type option instead.', $serviceId), E_USER_DEPRECATED);
  56. $extendedType = $tag[0]['alias'];
  57. } else {
  58. @trigger_error(sprintf('The extended_type option of the form.type_extension tag of service "%s" is required since Symfony 2.8.', $serviceId), E_USER_DEPRECATED);
  59. $extendedType = $serviceId;
  60. }
  61. $typeExtensions[$extendedType][] = $serviceId;
  62. }
  63. $definition->replaceArgument(2, $typeExtensions);
  64. // Find all services annotated with "form.type_guesser"
  65. $guessers = array_keys($container->findTaggedServiceIds('form.type_guesser'));
  66. foreach ($guessers as $serviceId) {
  67. $serviceDefinition = $container->getDefinition($serviceId);
  68. if (!$serviceDefinition->isPublic()) {
  69. throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form type guessers are lazy-loaded.', $serviceId));
  70. }
  71. }
  72. $definition->replaceArgument(3, $guessers);
  73. }
  74. }