diff options
Diffstat (limited to 'vendor/symfony/cache/DependencyInjection/CacheCollectorPass.php')
-rw-r--r-- | vendor/symfony/cache/DependencyInjection/CacheCollectorPass.php | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/vendor/symfony/cache/DependencyInjection/CacheCollectorPass.php b/vendor/symfony/cache/DependencyInjection/CacheCollectorPass.php new file mode 100644 index 0000000..ed95740 --- /dev/null +++ b/vendor/symfony/cache/DependencyInjection/CacheCollectorPass.php | |||
@@ -0,0 +1,75 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Cache\DependencyInjection; | ||
13 | |||
14 | use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface; | ||
15 | use Symfony\Component\Cache\Adapter\TraceableAdapter; | ||
16 | use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter; | ||
17 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
18 | use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
19 | use Symfony\Component\DependencyInjection\Definition; | ||
20 | use Symfony\Component\DependencyInjection\Reference; | ||
21 | |||
22 | /** | ||
23 | * Inject a data collector to all the cache services to be able to get detailed statistics. | ||
24 | * | ||
25 | * @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
26 | */ | ||
27 | class CacheCollectorPass implements CompilerPassInterface | ||
28 | { | ||
29 | public function process(ContainerBuilder $container): void | ||
30 | { | ||
31 | if (!$container->hasDefinition('data_collector.cache')) { | ||
32 | return; | ||
33 | } | ||
34 | |||
35 | foreach ($container->findTaggedServiceIds('cache.pool') as $id => $attributes) { | ||
36 | $poolName = $attributes[0]['name'] ?? $id; | ||
37 | |||
38 | $this->addToCollector($id, $poolName, $container); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | private function addToCollector(string $id, string $name, ContainerBuilder $container): void | ||
43 | { | ||
44 | $definition = $container->getDefinition($id); | ||
45 | if ($definition->isAbstract()) { | ||
46 | return; | ||
47 | } | ||
48 | |||
49 | $collectorDefinition = $container->getDefinition('data_collector.cache'); | ||
50 | $recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class); | ||
51 | $recorder->setTags($definition->getTags()); | ||
52 | if (!$definition->isPublic() || !$definition->isPrivate()) { | ||
53 | $recorder->setPublic($definition->isPublic()); | ||
54 | } | ||
55 | $recorder->setArguments([new Reference($innerId = $id.'.recorder_inner')]); | ||
56 | |||
57 | foreach ($definition->getMethodCalls() as [$method, $args]) { | ||
58 | if ('setCallbackWrapper' !== $method || !$args[0] instanceof Definition || !($args[0]->getArguments()[2] ?? null) instanceof Definition) { | ||
59 | continue; | ||
60 | } | ||
61 | if ([new Reference($id), 'setCallbackWrapper'] == $args[0]->getArguments()[2]->getFactory()) { | ||
62 | $args[0]->getArguments()[2]->setFactory([new Reference($innerId), 'setCallbackWrapper']); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | $definition->setTags([]); | ||
67 | $definition->setPublic(false); | ||
68 | |||
69 | $container->setDefinition($innerId, $definition); | ||
70 | $container->setDefinition($id, $recorder); | ||
71 | |||
72 | // Tell the collector to add the new instance | ||
73 | $collectorDefinition->addMethodCall('addInstance', [$name, new Reference($id)]); | ||
74 | } | ||
75 | } | ||