CacheCollectorPass.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if (0 < \func_num_args()) {
  31. trigger_deprecation('symfony/cache', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  32. }
  33. $this->dataCollectorCacheId = $dataCollectorCacheId;
  34. $this->cachePoolTag = $cachePoolTag;
  35. $this->cachePoolRecorderInnerSuffix = $cachePoolRecorderInnerSuffix;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function process(ContainerBuilder $container)
  41. {
  42. if (!$container->hasDefinition($this->dataCollectorCacheId)) {
  43. return;
  44. }
  45. foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $attributes) {
  46. $poolName = $attributes[0]['name'] ?? $id;
  47. $this->addToCollector($id, $poolName, $container);
  48. }
  49. }
  50. private function addToCollector(string $id, string $name, ContainerBuilder $container)
  51. {
  52. $definition = $container->getDefinition($id);
  53. if ($definition->isAbstract()) {
  54. return;
  55. }
  56. $collectorDefinition = $container->getDefinition($this->dataCollectorCacheId);
  57. $recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class);
  58. $recorder->setTags($definition->getTags());
  59. if (!$definition->isPublic() || !$definition->isPrivate()) {
  60. $recorder->setPublic($definition->isPublic());
  61. }
  62. $recorder->setArguments([new Reference($innerId = $id.$this->cachePoolRecorderInnerSuffix)]);
  63. foreach ($definition->getMethodCalls() as [$method, $args]) {
  64. if ('setCallbackWrapper' !== $method || !$args[0] instanceof Definition || !($args[0]->getArguments()[2] ?? null) instanceof Definition) {
  65. continue;
  66. }
  67. if ([new Reference($id), 'setCallbackWrapper'] == $args[0]->getArguments()[2]->getFactory()) {
  68. $args[0]->getArguments()[2]->setFactory([new Reference($innerId), 'setCallbackWrapper']);
  69. }
  70. }
  71. $definition->setTags([]);
  72. $definition->setPublic(false);
  73. $container->setDefinition($innerId, $definition);
  74. $container->setDefinition($id, $recorder);
  75. // Tell the collector to add the new instance
  76. $collectorDefinition->addMethodCall('addInstance', [$name, new Reference($id)]);
  77. $collectorDefinition->setPublic(false);
  78. }
  79. }