vendor/google/auth/src/Cache/MemoryCacheItemPool.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright 2016 Google Inc.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. namespace Google\Auth\Cache;
  18. use Psr\Cache\CacheItemInterface;
  19. use Psr\Cache\CacheItemPoolInterface;
  20. /**
  21.  * Simple in-memory cache implementation.
  22.  */
  23. final class MemoryCacheItemPool implements CacheItemPoolInterface
  24. {
  25.     /**
  26.      * @var CacheItemInterface[]
  27.      */
  28.     private $items;
  29.     /**
  30.      * @var CacheItemInterface[]
  31.      */
  32.     private $deferredItems;
  33.     /**
  34.      * {@inheritdoc}
  35.      *
  36.      * @return CacheItemInterface The corresponding Cache Item.
  37.      */
  38.     public function getItem($key): CacheItemInterface
  39.     {
  40.         return current($this->getItems([$key]));  // @phpstan-ignore-line
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      *
  45.      * @return iterable<CacheItemInterface>
  46.      *   A traversable collection of Cache Items keyed by the cache keys of
  47.      *   each item. A Cache item will be returned for each key, even if that
  48.      *   key is not found. However, if no keys are specified then an empty
  49.      *   traversable MUST be returned instead.
  50.      */
  51.     public function getItems(array $keys = []): iterable
  52.     {
  53.         $items = [];
  54.         $itemClass = \PHP_VERSION_ID >= 80000 TypedItem::class : Item::class;
  55.         foreach ($keys as $key) {
  56.             $items[$key] = $this->hasItem($key) ? clone $this->items[$key] : new $itemClass($key);
  57.         }
  58.         return $items;
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      *
  63.      * @return bool
  64.      *   True if item exists in the cache, false otherwise.
  65.      */
  66.     public function hasItem($key): bool
  67.     {
  68.         $this->isValidKey($key);
  69.         return isset($this->items[$key]) && $this->items[$key]->isHit();
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      *
  74.      * @return bool
  75.      *   True if the pool was successfully cleared. False if there was an error.
  76.      */
  77.     public function clear(): bool
  78.     {
  79.         $this->items = [];
  80.         $this->deferredItems = [];
  81.         return true;
  82.     }
  83.     /**
  84.      * {@inheritdoc}
  85.      *
  86.      * @return bool
  87.      *   True if the item was successfully removed. False if there was an error.
  88.      */
  89.     public function deleteItem($key): bool
  90.     {
  91.         return $this->deleteItems([$key]);
  92.     }
  93.     /**
  94.      * {@inheritdoc}
  95.      *
  96.      * @return bool
  97.      *   True if the items were successfully removed. False if there was an error.
  98.      */
  99.     public function deleteItems(array $keys): bool
  100.     {
  101.         array_walk($keys, [$this'isValidKey']);
  102.         foreach ($keys as $key) {
  103.             unset($this->items[$key]);
  104.         }
  105.         return true;
  106.     }
  107.     /**
  108.      * {@inheritdoc}
  109.      *
  110.      * @return bool
  111.      *   True if the item was successfully persisted. False if there was an error.
  112.      */
  113.     public function save(CacheItemInterface $item): bool
  114.     {
  115.         $this->items[$item->getKey()] = $item;
  116.         return true;
  117.     }
  118.     /**
  119.      * {@inheritdoc}
  120.      *
  121.      * @return bool
  122.      *   False if the item could not be queued or if a commit was attempted and failed. True otherwise.
  123.      */
  124.     public function saveDeferred(CacheItemInterface $item): bool
  125.     {
  126.         $this->deferredItems[$item->getKey()] = $item;
  127.         return true;
  128.     }
  129.     /**
  130.      * {@inheritdoc}
  131.      *
  132.      * @return bool
  133.      *   True if all not-yet-saved items were successfully saved or there were none. False otherwise.
  134.      */
  135.     public function commit(): bool
  136.     {
  137.         foreach ($this->deferredItems as $item) {
  138.             $this->save($item);
  139.         }
  140.         $this->deferredItems = [];
  141.         return true;
  142.     }
  143.     /**
  144.      * Determines if the provided key is valid.
  145.      *
  146.      * @param string $key
  147.      * @return bool
  148.      * @throws InvalidArgumentException
  149.      */
  150.     private function isValidKey($key)
  151.     {
  152.         $invalidCharacters '{}()/\\\\@:';
  153.         if (!is_string($key) || preg_match("#[$invalidCharacters]#"$key)) {
  154.             throw new InvalidArgumentException('The provided key is not valid: ' var_export($keytrue));
  155.         }
  156.         return true;
  157.     }
  158. }