PropertyInfoPass.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15. * Adds extractors to the property_info service.
  16. *
  17. * @author Kévin Dunglas <dunglas@gmail.com>
  18. */
  19. class PropertyInfoPass implements CompilerPassInterface
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function process(ContainerBuilder $container)
  25. {
  26. if (!$container->hasDefinition('property_info')) {
  27. return;
  28. }
  29. $listExtractors = $this->findAndSortTaggedServices('property_info.list_extractor', $container);
  30. $container->getDefinition('property_info')->replaceArgument(0, $listExtractors);
  31. $typeExtractors = $this->findAndSortTaggedServices('property_info.type_extractor', $container);
  32. $container->getDefinition('property_info')->replaceArgument(1, $typeExtractors);
  33. $descriptionExtractors = $this->findAndSortTaggedServices('property_info.description_extractor', $container);
  34. $container->getDefinition('property_info')->replaceArgument(2, $descriptionExtractors);
  35. $accessExtractors = $this->findAndSortTaggedServices('property_info.access_extractor', $container);
  36. $container->getDefinition('property_info')->replaceArgument(3, $accessExtractors);
  37. }
  38. /**
  39. * Finds all services with the given tag name and order them by their priority.
  40. *
  41. * @param string $tagName
  42. * @param ContainerBuilder $container
  43. *
  44. * @return array
  45. */
  46. private function findAndSortTaggedServices($tagName, ContainerBuilder $container)
  47. {
  48. $services = $container->findTaggedServiceIds($tagName);
  49. $sortedServices = array();
  50. foreach ($services as $serviceId => $attributes) {
  51. $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
  52. $sortedServices[$priority][] = new Reference($serviceId);
  53. }
  54. if (empty($sortedServices)) {
  55. return array();
  56. }
  57. krsort($sortedServices);
  58. // Flatten the array
  59. return \call_user_func_array('array_merge', $sortedServices);
  60. }
  61. }