CacheItem.php 4.9 KB

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