CachePoolPass.php 9.1 KB

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