ProfilerPass.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 tagged data_collector services to profiler service.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class ProfilerPass implements CompilerPassInterface
  20. {
  21. public function process(ContainerBuilder $container)
  22. {
  23. if (false === $container->hasDefinition('profiler')) {
  24. return;
  25. }
  26. $definition = $container->getDefinition('profiler');
  27. $collectors = new \SplPriorityQueue();
  28. $order = PHP_INT_MAX;
  29. foreach ($container->findTaggedServiceIds('data_collector') as $id => $attributes) {
  30. $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
  31. $template = null;
  32. if (isset($attributes[0]['template'])) {
  33. if (!isset($attributes[0]['id'])) {
  34. throw new \InvalidArgumentException(sprintf('Data collector service "%s" must have an id attribute in order to specify a template', $id));
  35. }
  36. $template = array($attributes[0]['id'], $attributes[0]['template']);
  37. }
  38. $collectors->insert(array($id, $template), array($priority, --$order));
  39. }
  40. $templates = array();
  41. foreach ($collectors as $collector) {
  42. $definition->addMethodCall('add', array(new Reference($collector[0])));
  43. $templates[$collector[0]] = $collector[1];
  44. }
  45. $container->setParameter('data_collector.templates', $templates);
  46. }
  47. }