vendor/symfony/cache/Traits/ContractsTrait.php line 102

Open in your IDE?
  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\Traits;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Cache\Adapter\AdapterInterface;
  13. use Symfony\Component\Cache\CacheItem;
  14. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  15. use Symfony\Component\Cache\LockRegistry;
  16. use Symfony\Contracts\Cache\CacheInterface;
  17. use Symfony\Contracts\Cache\CacheTrait;
  18. use Symfony\Contracts\Cache\ItemInterface;
  19. /**
  20.  * @author Nicolas Grekas <p@tchwork.com>
  21.  *
  22.  * @internal
  23.  */
  24. trait ContractsTrait
  25. {
  26.     use CacheTrait {
  27.         doGet as private contractsGet;
  28.     }
  29.     private \Closure $callbackWrapper;
  30.     private array $computing = [];
  31.     /**
  32.      * Wraps the callback passed to ->get() in a callable.
  33.      *
  34.      * @return callable the previous callback wrapper
  35.      */
  36.     public function setCallbackWrapper(?callable $callbackWrapper): callable
  37.     {
  38.         if (!isset($this->callbackWrapper)) {
  39.             $this->callbackWrapper LockRegistry::compute(...);
  40.             if (\in_array(\PHP_SAPI, ['cli''phpdbg'], true)) {
  41.                 $this->setCallbackWrapper(null);
  42.             }
  43.         }
  44.         if (null !== $callbackWrapper && !$callbackWrapper instanceof \Closure) {
  45.             $callbackWrapper $callbackWrapper(...);
  46.         }
  47.         $previousWrapper $this->callbackWrapper;
  48.         $this->callbackWrapper $callbackWrapper ?? static fn (callable $callbackItemInterface $itembool &$saveCacheInterface $pool\Closure $setMetadata, ?LoggerInterface $logger) => $callback($item$save);
  49.         return $previousWrapper;
  50.     }
  51.     private function doGet(AdapterInterface $poolstring $key, callable $callback, ?float $beta, array &$metadata null): mixed
  52.     {
  53.         if ($beta ??= 1.0) {
  54.             throw new InvalidArgumentException(sprintf('Argument "$beta" provided to "%s::get()" must be a positive number, %f given.', static::class, $beta));
  55.         }
  56.         static $setMetadata;
  57.         $setMetadata ??= \Closure::bind(
  58.             static function (CacheItem $itemfloat $startTime, ?array &$metadata) {
  59.                 if ($item->expiry $endTime microtime(true)) {
  60.                     $item->newMetadata[CacheItem::METADATA_EXPIRY] = $metadata[CacheItem::METADATA_EXPIRY] = $item->expiry;
  61.                     $item->newMetadata[CacheItem::METADATA_CTIME] = $metadata[CacheItem::METADATA_CTIME] = (int) ceil(1000 * ($endTime $startTime));
  62.                 } else {
  63.                     unset($metadata[CacheItem::METADATA_EXPIRY], $metadata[CacheItem::METADATA_CTIME], $metadata[CacheItem::METADATA_TAGS]);
  64.                 }
  65.             },
  66.             null,
  67.             CacheItem::class
  68.         );
  69.         $this->callbackWrapper ??= LockRegistry::compute(...);
  70.         return $this->contractsGet($pool$key, function (CacheItem $itembool &$save) use ($pool$callback$setMetadata, &$metadata$key) {
  71.             // don't wrap nor save recursive calls
  72.             if (isset($this->computing[$key])) {
  73.                 $value $callback($item$save);
  74.                 $save false;
  75.                 return $value;
  76.             }
  77.             $this->computing[$key] = $key;
  78.             $startTime microtime(true);
  79.             if (!isset($this->callbackWrapper)) {
  80.                 $this->setCallbackWrapper($this->setCallbackWrapper(null));
  81.             }
  82.             try {
  83.                 $value = ($this->callbackWrapper)($callback$item$save$pool, function (CacheItem $item) use ($setMetadata$startTime, &$metadata) {
  84.                     $setMetadata($item$startTime$metadata);
  85.                 }, $this->logger ?? null);
  86.                 $setMetadata($item$startTime$metadata);
  87.                 return $value;
  88.             } finally {
  89.                 unset($this->computing[$key]);
  90.             }
  91.         }, $beta$metadata$this->logger ?? null);
  92.     }
  93. }