CachePoolPass.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\AbstractAdapter;
  12. use Symfony\Component\Cache\Adapter\ArrayAdapter;
  13. use Symfony\Component\DependencyInjection\ChildDefinition;
  14. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Definition;
  17. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  18. use Symfony\Component\DependencyInjection\Reference;
  19. /**
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. */
  22. class CachePoolPass implements CompilerPassInterface
  23. {
  24. private $cachePoolTag;
  25. private $kernelResetTag;
  26. private $cacheClearerId;
  27. private $cachePoolClearerTag;
  28. private $cacheSystemClearerId;
  29. private $cacheSystemClearerTag;
  30. public function __construct(string $cachePoolTag = 'cache.pool', string $kernelResetTag = 'kernel.reset', string $cacheClearerId = 'cache.global_clearer', string $cachePoolClearerTag = 'cache.pool.clearer', string $cacheSystemClearerId = 'cache.system_clearer', string $cacheSystemClearerTag = 'kernel.cache_clearer')
  31. {
  32. $this->cachePoolTag = $cachePoolTag;
  33. $this->kernelResetTag = $kernelResetTag;
  34. $this->cacheClearerId = $cacheClearerId;
  35. $this->cachePoolClearerTag = $cachePoolClearerTag;
  36. $this->cacheSystemClearerId = $cacheSystemClearerId;
  37. $this->cacheSystemClearerTag = $cacheSystemClearerTag;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function process(ContainerBuilder $container)
  43. {
  44. if ($container->hasParameter('cache.prefix.seed')) {
  45. $seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
  46. } else {
  47. $seed = '_'.$container->getParameter('kernel.project_dir');
  48. }
  49. $seed .= '.'.$container->getParameter('kernel.container_class');
  50. $pools = [];
  51. $clearers = [];
  52. $attributes = [
  53. 'provider',
  54. 'name',
  55. 'namespace',
  56. 'default_lifetime',
  57. 'reset',
  58. ];
  59. foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $tags) {
  60. $adapter = $pool = $container->getDefinition($id);
  61. if ($pool->isAbstract()) {
  62. continue;
  63. }
  64. $class = $adapter->getClass();
  65. while ($adapter instanceof ChildDefinition) {
  66. $adapter = $container->findDefinition($adapter->getParent());
  67. $class = $class ?: $adapter->getClass();
  68. if ($t = $adapter->getTag($this->cachePoolTag)) {
  69. $tags[0] += $t[0];
  70. }
  71. }
  72. $name = $tags[0]['name'] ?? $id;
  73. if (!isset($tags[0]['namespace'])) {
  74. $namespaceSeed = $seed;
  75. if (null !== $class) {
  76. $namespaceSeed .= '.'.$class;
  77. }
  78. $tags[0]['namespace'] = $this->getNamespace($namespaceSeed, $name);
  79. }
  80. if (isset($tags[0]['clearer'])) {
  81. $clearer = $tags[0]['clearer'];
  82. while ($container->hasAlias($clearer)) {
  83. $clearer = (string) $container->getAlias($clearer);
  84. }
  85. } else {
  86. $clearer = null;
  87. }
  88. unset($tags[0]['clearer'], $tags[0]['name']);
  89. if (isset($tags[0]['provider'])) {
  90. $tags[0]['provider'] = new Reference(static::getServiceProvider($container, $tags[0]['provider']));
  91. }
  92. $i = 0;
  93. foreach ($attributes as $attr) {
  94. if (!isset($tags[0][$attr])) {
  95. // no-op
  96. } elseif ('reset' === $attr) {
  97. if ($tags[0][$attr]) {
  98. $pool->addTag($this->kernelResetTag, ['method' => $tags[0][$attr]]);
  99. }
  100. } elseif ('namespace' !== $attr || ArrayAdapter::class !== $adapter->getClass()) {
  101. $pool->replaceArgument($i++, $tags[0][$attr]);
  102. }
  103. unset($tags[0][$attr]);
  104. }
  105. if (!empty($tags[0])) {
  106. throw new InvalidArgumentException(sprintf('Invalid "%s" tag for service "%s": accepted attributes are "clearer", "provider", "name", "namespace", "default_lifetime" and "reset", found "%s".', $this->cachePoolTag, $id, implode('", "', array_keys($tags[0]))));
  107. }
  108. if (null !== $clearer) {
  109. $clearers[$clearer][$name] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
  110. }
  111. $pools[$name] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
  112. }
  113. $notAliasedCacheClearerId = $this->cacheClearerId;
  114. while ($container->hasAlias($this->cacheClearerId)) {
  115. $this->cacheClearerId = (string) $container->getAlias($this->cacheClearerId);
  116. }
  117. if ($container->hasDefinition($this->cacheClearerId)) {
  118. $clearers[$notAliasedCacheClearerId] = $pools;
  119. }
  120. foreach ($clearers as $id => $pools) {
  121. $clearer = $container->getDefinition($id);
  122. if ($clearer instanceof ChildDefinition) {
  123. $clearer->replaceArgument(0, $pools);
  124. } else {
  125. $clearer->setArgument(0, $pools);
  126. }
  127. $clearer->addTag($this->cachePoolClearerTag);
  128. if ($this->cacheSystemClearerId === $id) {
  129. $clearer->addTag($this->cacheSystemClearerTag);
  130. }
  131. }
  132. if ($container->hasDefinition('console.command.cache_pool_list')) {
  133. $container->getDefinition('console.command.cache_pool_list')->replaceArgument(0, array_keys($pools));
  134. }
  135. }
  136. private function getNamespace($seed, $id)
  137. {
  138. return substr(str_replace('/', '-', base64_encode(hash('sha256', $id.$seed, true))), 0, 10);
  139. }
  140. /**
  141. * @internal
  142. */
  143. public static function getServiceProvider(ContainerBuilder $container, $name)
  144. {
  145. $container->resolveEnvPlaceholders($name, null, $usedEnvs);
  146. if ($usedEnvs || preg_match('#^[a-z]++:#', $name)) {
  147. $dsn = $name;
  148. if (!$container->hasDefinition($name = '.cache_connection.'.ContainerBuilder::hash($dsn))) {
  149. $definition = new Definition(AbstractAdapter::class);
  150. $definition->setPublic(false);
  151. $definition->setFactory([AbstractAdapter::class, 'createConnection']);
  152. $definition->setArguments([$dsn, ['lazy' => true]]);
  153. $container->setDefinition($name, $definition);
  154. }
  155. }
  156. return $name;
  157. }
  158. }