CacheCollectorPass.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. private $dataCollectorCacheId;
  26. private $cachePoolTag;
  27. private $cachePoolRecorderInnerSuffix;
  28. public function __construct(string $dataCollectorCacheId = 'data_collector.cache', string $cachePoolTag = 'cache.pool', string $cachePoolRecorderInnerSuffix = '.recorder_inner')
  29. {
  30. $this->dataCollectorCacheId = $dataCollectorCacheId;
  31. $this->cachePoolTag = $cachePoolTag;
  32. $this->cachePoolRecorderInnerSuffix = $cachePoolRecorderInnerSuffix;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function process(ContainerBuilder $container)
  38. {
  39. if (!$container->hasDefinition($this->dataCollectorCacheId)) {
  40. return;
  41. }
  42. foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $attributes) {
  43. $this->addToCollector($id, $container);
  44. if (($attributes[0]['name'] ?? $id) !== $id) {
  45. $this->addToCollector($attributes[0]['name'], $container);
  46. }
  47. }
  48. }
  49. private function addToCollector(string $id, ContainerBuilder $container)
  50. {
  51. $definition = $container->getDefinition($id);
  52. if ($definition->isAbstract()) {
  53. return;
  54. }
  55. $collectorDefinition = $container->getDefinition($this->dataCollectorCacheId);
  56. $recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class);
  57. $recorder->setTags($definition->getTags());
  58. if (!$definition->isPublic() || !$definition->isPrivate()) {
  59. $recorder->setPublic($definition->isPublic());
  60. }
  61. $recorder->setArguments([new Reference($innerId = $id.$this->cachePoolRecorderInnerSuffix)]);
  62. $definition->setTags([]);
  63. $definition->setPublic(false);
  64. $container->setDefinition($innerId, $definition);
  65. $container->setDefinition($id, $recorder);
  66. // Tell the collector to add the new instance
  67. $collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]);
  68. $collectorDefinition->setPublic(false);
  69. }
  70. }