ProfilerPass.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\Bundle\FrameworkBundle\DataCollector\TemplateAwareDataCollectorInterface;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. /**
  17. * Adds tagged data_collector services to profiler service.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class ProfilerPass implements CompilerPassInterface
  22. {
  23. public function process(ContainerBuilder $container)
  24. {
  25. if (false === $container->hasDefinition('profiler')) {
  26. return;
  27. }
  28. $definition = $container->getDefinition('profiler');
  29. $collectors = new \SplPriorityQueue();
  30. $order = \PHP_INT_MAX;
  31. foreach ($container->findTaggedServiceIds('data_collector', true) as $id => $attributes) {
  32. $priority = $attributes[0]['priority'] ?? 0;
  33. $template = null;
  34. $collectorClass = $container->findDefinition($id)->getClass();
  35. $isTemplateAware = is_subclass_of($collectorClass, TemplateAwareDataCollectorInterface::class);
  36. if (isset($attributes[0]['template']) || $isTemplateAware) {
  37. $idForTemplate = $attributes[0]['id'] ?? $collectorClass;
  38. if (!$idForTemplate) {
  39. throw new InvalidArgumentException(sprintf('Data collector service "%s" must have an id attribute in order to specify a template.', $id));
  40. }
  41. $template = [$idForTemplate, $attributes[0]['template'] ?? $collectorClass::getTemplate()];
  42. }
  43. $collectors->insert([$id, $template], [$priority, --$order]);
  44. }
  45. $templates = [];
  46. foreach ($collectors as $collector) {
  47. $definition->addMethodCall('add', [new Reference($collector[0])]);
  48. $templates[$collector[0]] = $collector[1];
  49. }
  50. $container->setParameter('data_collector.templates', $templates);
  51. }
  52. }