EarlyExpirationMessage.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Messenger;
  11. use Symfony\Component\Cache\Adapter\AdapterInterface;
  12. use Symfony\Component\Cache\CacheItem;
  13. use Symfony\Component\DependencyInjection\ReverseContainer;
  14. /**
  15. * Conveys a cached value that needs to be computed.
  16. */
  17. final class EarlyExpirationMessage
  18. {
  19. public static function create(ReverseContainer $reverseContainer, callable $callback, CacheItem $item, AdapterInterface $pool): ?self
  20. {
  21. try {
  22. $item = clone $item;
  23. $item->set(null);
  24. } catch (\Exception) {
  25. return null;
  26. }
  27. $pool = $reverseContainer->getId($pool);
  28. if ($callback instanceof \Closure && !($r = new \ReflectionFunction($callback))->isAnonymous()) {
  29. $callback = [$r->getClosureThis() ?? $r->getClosureCalledClass()?->name, $r->name];
  30. $callback[0] ?: $callback = $r->name;
  31. }
  32. if (\is_object($callback)) {
  33. if (null === $id = $reverseContainer->getId($callback)) {
  34. return null;
  35. }
  36. $callback = '@'.$id;
  37. } elseif (!\is_array($callback)) {
  38. $callback = (string) $callback;
  39. } elseif (!\is_object($callback[0])) {
  40. $callback = [(string) $callback[0], (string) $callback[1]];
  41. } else {
  42. if (null === $id = $reverseContainer->getId($callback[0])) {
  43. return null;
  44. }
  45. $callback = ['@'.$id, (string) $callback[1]];
  46. }
  47. return new self($item, $pool, $callback);
  48. }
  49. public function getItem(): CacheItem
  50. {
  51. return $this->item;
  52. }
  53. public function getPool(): string
  54. {
  55. return $this->pool;
  56. }
  57. /**
  58. * @return string|string[]
  59. */
  60. public function getCallback(): string|array
  61. {
  62. return $this->callback;
  63. }
  64. public function findPool(ReverseContainer $reverseContainer): AdapterInterface
  65. {
  66. return $reverseContainer->getService($this->pool);
  67. }
  68. public function findCallback(ReverseContainer $reverseContainer): callable
  69. {
  70. if (\is_string($callback = $this->callback)) {
  71. return '@' === $callback[0] ? $reverseContainer->getService(substr($callback, 1)) : $callback;
  72. }
  73. if ('@' === $callback[0][0]) {
  74. $callback[0] = $reverseContainer->getService(substr($callback[0], 1));
  75. }
  76. return $callback;
  77. }
  78. private function __construct(
  79. private CacheItem $item,
  80. private string $pool,
  81. private string|array $callback,
  82. ) {
  83. }
  84. }