CacheCollectorPass.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Component\Cache\DependencyInjection;
  11. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  12. use Symfony\Component\Cache\Adapter\TraceableAdapter;
  13. use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
  14. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Definition;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. /**
  19. * Inject a data collector to all the cache services to be able to get detailed statistics.
  20. *
  21. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  22. */
  23. class CacheCollectorPass implements CompilerPassInterface
  24. {
  25. public function process(ContainerBuilder $container): void
  26. {
  27. if (!$container->hasDefinition('data_collector.cache')) {
  28. return;
  29. }
  30. foreach ($container->findTaggedServiceIds('cache.pool') as $id => $attributes) {
  31. $poolName = $attributes[0]['name'] ?? $id;
  32. $this->addToCollector($id, $poolName, $container);
  33. }
  34. }
  35. private function addToCollector(string $id, string $name, ContainerBuilder $container): void
  36. {
  37. $definition = $container->getDefinition($id);
  38. if ($definition->isAbstract()) {
  39. return;
  40. }
  41. $collectorDefinition = $container->getDefinition('data_collector.cache');
  42. $recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class);
  43. $recorder->setTags($definition->getTags());
  44. if (!$definition->isPublic() || !$definition->isPrivate()) {
  45. $recorder->setPublic($definition->isPublic());
  46. }
  47. $recorder->setArguments([new Reference($innerId = $id.'.recorder_inner')]);
  48. foreach ($definition->getMethodCalls() as [$method, $args]) {
  49. if ('setCallbackWrapper' !== $method || !$args[0] instanceof Definition || !($args[0]->getArguments()[2] ?? null) instanceof Definition) {
  50. continue;
  51. }
  52. if ([new Reference($id), 'setCallbackWrapper'] == $args[0]->getArguments()[2]->getFactory()) {
  53. $args[0]->getArguments()[2]->setFactory([new Reference($innerId), 'setCallbackWrapper']);
  54. }
  55. }
  56. $definition->setTags([]);
  57. $definition->setPublic(false);
  58. $container->setDefinition($innerId, $definition);
  59. $container->setDefinition($id, $recorder);
  60. // Tell the collector to add the new instance
  61. $collectorDefinition->addMethodCall('addInstance', [$name, new Reference($id)]);
  62. }
  63. }