CachePoolPass.php 9.0 KB

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