CacheItem.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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;
  11. use Psr\Cache\CacheItemInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. final class CacheItem implements CacheItemInterface
  18. {
  19. protected $key;
  20. protected $value;
  21. protected $isHit = false;
  22. protected $expiry;
  23. protected $defaultLifetime;
  24. protected $tags = [];
  25. protected $prevTags = [];
  26. protected $innerItem;
  27. protected $poolHash;
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getKey()
  32. {
  33. return $this->key;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function get()
  39. {
  40. return $this->value;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function isHit()
  46. {
  47. return $this->isHit;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function set($value)
  53. {
  54. $this->value = $value;
  55. return $this;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function expiresAt($expiration)
  61. {
  62. if (null === $expiration) {
  63. $this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
  64. } elseif ($expiration instanceof \DateTimeInterface) {
  65. $this->expiry = (int) $expiration->format('U');
  66. } else {
  67. throw new InvalidArgumentException(sprintf('Expiration date must implement DateTimeInterface or be null, "%s" given', \is_object($expiration) ? \get_class($expiration) : \gettype($expiration)));
  68. }
  69. return $this;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function expiresAfter($time)
  75. {
  76. if (null === $time) {
  77. $this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
  78. } elseif ($time instanceof \DateInterval) {
  79. $this->expiry = (int) \DateTime::createFromFormat('U', time())->add($time)->format('U');
  80. } elseif (\is_int($time)) {
  81. $this->expiry = $time + time();
  82. } else {
  83. throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given', \is_object($time) ? \get_class($time) : \gettype($time)));
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Adds a tag to a cache item.
  89. *
  90. * @param string|string[] $tags A tag or array of tags
  91. *
  92. * @return static
  93. *
  94. * @throws InvalidArgumentException When $tag is not valid
  95. */
  96. public function tag($tags)
  97. {
  98. if (!\is_array($tags)) {
  99. $tags = [$tags];
  100. }
  101. foreach ($tags as $tag) {
  102. if (!\is_string($tag)) {
  103. throw new InvalidArgumentException(sprintf('Cache tag must be string, "%s" given', \is_object($tag) ? \get_class($tag) : \gettype($tag)));
  104. }
  105. if (isset($this->tags[$tag])) {
  106. continue;
  107. }
  108. if ('' === $tag) {
  109. throw new InvalidArgumentException('Cache tag length must be greater than zero');
  110. }
  111. if (false !== strpbrk($tag, '{}()/\@:')) {
  112. throw new InvalidArgumentException(sprintf('Cache tag "%s" contains reserved characters {}()/\@:', $tag));
  113. }
  114. $this->tags[$tag] = $tag;
  115. }
  116. return $this;
  117. }
  118. /**
  119. * Returns the list of tags bound to the value coming from the pool storage if any.
  120. *
  121. * @return array
  122. */
  123. public function getPreviousTags()
  124. {
  125. return $this->prevTags;
  126. }
  127. /**
  128. * Validates a cache key according to PSR-6.
  129. *
  130. * @param string $key The key to validate
  131. *
  132. * @return string
  133. *
  134. * @throws InvalidArgumentException When $key is not valid
  135. */
  136. public static function validateKey($key)
  137. {
  138. if (!\is_string($key)) {
  139. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', \is_object($key) ? \get_class($key) : \gettype($key)));
  140. }
  141. if ('' === $key) {
  142. throw new InvalidArgumentException('Cache key length must be greater than zero');
  143. }
  144. if (false !== strpbrk($key, '{}()/\@:')) {
  145. throw new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters {}()/\@:', $key));
  146. }
  147. return $key;
  148. }
  149. /**
  150. * Internal logging helper.
  151. *
  152. * @internal
  153. */
  154. public static function log(LoggerInterface $logger = null, $message, $context = [])
  155. {
  156. if ($logger) {
  157. $logger->warning($message, $context);
  158. } else {
  159. $replace = [];
  160. foreach ($context as $k => $v) {
  161. if (is_scalar($v)) {
  162. $replace['{'.$k.'}'] = $v;
  163. }
  164. }
  165. @trigger_error(strtr($message, $replace), E_USER_WARNING);
  166. }
  167. }
  168. }