vendor/sensio/framework-extra-bundle/src/Configuration/Cache.php line 21

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 Sensio\Bundle\FrameworkExtraBundle\Configuration;
  11. /**
  12. * The Cache class handles the Cache annotation parts.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @Annotation
  16. */
  17. #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
  18. class Cache extends ConfigurationAnnotation
  19. {
  20. /**
  21. * The expiration date as a valid date for the strtotime() function.
  22. *
  23. * @var string
  24. */
  25. private $expires;
  26. /**
  27. * The number of seconds that the response is considered fresh by a private
  28. * cache like a web browser.
  29. *
  30. * @var int|string|null
  31. */
  32. private $maxage;
  33. /**
  34. * The number of seconds that the response is considered fresh by a public
  35. * cache like a reverse proxy cache.
  36. *
  37. * @var int|string|null
  38. */
  39. private $smaxage;
  40. /**
  41. * Whether the response is public or not.
  42. *
  43. * @var bool
  44. */
  45. private $public;
  46. /**
  47. * Whether or not the response must be revalidated.
  48. *
  49. * @var bool
  50. */
  51. private $mustRevalidate;
  52. /**
  53. * Additional "Vary:"-headers.
  54. *
  55. * @var array
  56. */
  57. private $vary;
  58. /**
  59. * An expression to compute the Last-Modified HTTP header.
  60. *
  61. * @var string
  62. */
  63. private $lastModified;
  64. /**
  65. * An expression to compute the ETag HTTP header.
  66. *
  67. * @var string
  68. */
  69. private $etag;
  70. /**
  71. * max-stale Cache-Control header
  72. * It can be expressed in seconds or with a relative time format (1 day, 2 weeks, ...).
  73. *
  74. * @var int|string
  75. */
  76. private $maxStale;
  77. /**
  78. * stale-while-revalidate Cache-Control header
  79. * It can be expressed in seconds or with a relative time format (1 day, 2 weeks, ...).
  80. *
  81. * @var int|string
  82. */
  83. private $staleWhileRevalidate;
  84. /**
  85. * stale-if-error Cache-Control header
  86. * It can be expressed in seconds or with a relative time format (1 day, 2 weeks, ...).
  87. *
  88. * @var int|string
  89. */
  90. private $staleIfError;
  91. /**
  92. * @param int|string|null $maxage
  93. * @param int|string|null $smaxage
  94. * @param int|string|null $maxstale
  95. * @param int|string|null $staleWhileRevalidate
  96. * @param int|string|null $staleIfError
  97. */
  98. public function __construct(
  99. array $values = [],
  100. string $expires = null,
  101. $maxage = null,
  102. $smaxage = null,
  103. bool $public = null,
  104. bool $mustRevalidate = null,
  105. array $vary = null,
  106. string $lastModified = null,
  107. string $etag = null,
  108. $maxstale = null,
  109. $staleWhileRevalidate = null,
  110. $staleIfError = null
  111. ) {
  112. $values['expires'] = $values['expires'] ?? $expires;
  113. $values['maxage'] = $values['maxage'] ?? $maxage;
  114. $values['smaxage'] = $values['smaxage'] ?? $smaxage;
  115. $values['public'] = $values['public'] ?? $public;
  116. $values['mustRevalidate'] = $values['mustRevalidate'] ?? $mustRevalidate;
  117. $values['vary'] = $values['vary'] ?? $vary;
  118. $values['lastModified'] = $values['lastModified'] ?? $lastModified;
  119. $values['Etag'] = $values['Etag'] ?? $etag;
  120. $values['maxstale'] = $values['maxstale'] ?? $maxstale;
  121. $values['staleWhileRevalidate'] = $values['staleWhileRevalidate'] ?? $staleWhileRevalidate;
  122. $values['staleIfError'] = $values['staleIfError'] ?? $staleIfError;
  123. $values = array_filter($values, function ($v) {
  124. return null !== $v;
  125. });
  126. parent::__construct($values);
  127. }
  128. /**
  129. * Returns the expiration date for the Expires header field.
  130. *
  131. * @return string
  132. */
  133. public function getExpires()
  134. {
  135. return $this->expires;
  136. }
  137. /**
  138. * Sets the expiration date for the Expires header field.
  139. *
  140. * @param string $expires A valid php date
  141. */
  142. public function setExpires($expires)
  143. {
  144. $this->expires = $expires;
  145. }
  146. /**
  147. * Sets the number of seconds for the max-age cache-control header field.
  148. *
  149. * @param int $maxage A number of seconds
  150. */
  151. public function setMaxAge($maxage)
  152. {
  153. $this->maxage = $maxage;
  154. }
  155. /**
  156. * Returns the number of seconds the response is considered fresh by a
  157. * private cache.
  158. *
  159. * @return int
  160. */
  161. public function getMaxAge()
  162. {
  163. return $this->maxage;
  164. }
  165. /**
  166. * Sets the number of seconds for the s-maxage cache-control header field.
  167. *
  168. * @param int $smaxage A number of seconds
  169. */
  170. public function setSMaxAge($smaxage)
  171. {
  172. $this->smaxage = $smaxage;
  173. }
  174. /**
  175. * Returns the number of seconds the response is considered fresh by a
  176. * public cache.
  177. *
  178. * @return int
  179. */
  180. public function getSMaxAge()
  181. {
  182. return $this->smaxage;
  183. }
  184. /**
  185. * Returns whether or not a response is public.
  186. *
  187. * @return bool
  188. */
  189. public function isPublic()
  190. {
  191. return true === $this->public;
  192. }
  193. /**
  194. * @return bool
  195. */
  196. public function mustRevalidate()
  197. {
  198. return true === $this->mustRevalidate;
  199. }
  200. /**
  201. * Forces a response to be revalidated.
  202. *
  203. * @param bool $mustRevalidate
  204. */
  205. public function setMustRevalidate($mustRevalidate)
  206. {
  207. $this->mustRevalidate = (bool) $mustRevalidate;
  208. }
  209. /**
  210. * Returns whether or not a response is private.
  211. *
  212. * @return bool
  213. */
  214. public function isPrivate()
  215. {
  216. return false === $this->public;
  217. }
  218. /**
  219. * Sets a response public.
  220. *
  221. * @param bool $public A boolean value
  222. */
  223. public function setPublic($public)
  224. {
  225. $this->public = (bool) $public;
  226. }
  227. /**
  228. * Returns the custom "Vary"-headers.
  229. *
  230. * @return array
  231. */
  232. public function getVary()
  233. {
  234. return $this->vary;
  235. }
  236. /**
  237. * Add additional "Vary:"-headers.
  238. *
  239. * @param array $vary
  240. */
  241. public function setVary($vary)
  242. {
  243. $this->vary = $vary;
  244. }
  245. /**
  246. * Sets the "Last-Modified"-header expression.
  247. *
  248. * @param string $expression
  249. */
  250. public function setLastModified($expression)
  251. {
  252. $this->lastModified = $expression;
  253. }
  254. /**
  255. * Returns the "Last-Modified"-header expression.
  256. *
  257. * @return string
  258. */
  259. public function getLastModified()
  260. {
  261. return $this->lastModified;
  262. }
  263. /**
  264. * Sets the "ETag"-header expression.
  265. *
  266. * @param string $expression
  267. */
  268. public function setEtag($expression)
  269. {
  270. $this->etag = $expression;
  271. }
  272. /**
  273. * Returns the "ETag"-header expression.
  274. *
  275. * @return string
  276. */
  277. public function getEtag()
  278. {
  279. return $this->etag;
  280. }
  281. /**
  282. * @return int|string
  283. */
  284. public function getMaxStale()
  285. {
  286. return $this->maxStale;
  287. }
  288. /**
  289. * Sets the number of seconds for the max-stale cache-control header field.
  290. *
  291. * @param int|string $maxStale A number of seconds
  292. */
  293. public function setMaxStale($maxStale)
  294. {
  295. $this->maxStale = $maxStale;
  296. }
  297. /**
  298. * @return int|string
  299. */
  300. public function getStaleWhileRevalidate()
  301. {
  302. return $this->staleWhileRevalidate;
  303. }
  304. /**
  305. * @param int|string $staleWhileRevalidate
  306. *
  307. * @return self
  308. */
  309. public function setStaleWhileRevalidate($staleWhileRevalidate)
  310. {
  311. $this->staleWhileRevalidate = $staleWhileRevalidate;
  312. return $this;
  313. }
  314. /**
  315. * @return int|string
  316. */
  317. public function getStaleIfError()
  318. {
  319. return $this->staleIfError;
  320. }
  321. /**
  322. * @param int|string $staleIfError
  323. *
  324. * @return self
  325. */
  326. public function setStaleIfError($staleIfError)
  327. {
  328. $this->staleIfError = $staleIfError;
  329. return $this;
  330. }
  331. /**
  332. * Returns the annotation alias name.
  333. *
  334. * @return string
  335. *
  336. * @see ConfigurationInterface
  337. */
  338. public function getAliasName()
  339. {
  340. return 'cache';
  341. }
  342. /**
  343. * Only one cache directive is allowed.
  344. *
  345. * @return bool
  346. *
  347. * @see ConfigurationInterface
  348. */
  349. public function allowArray()
  350. {
  351. return false;
  352. }
  353. }