[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Backend / AbstractBackend.php
1 <?php
2 namespace TYPO3\CMS\Core\Cache\Backend;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 /**
17 * An abstract caching backend
18 *
19 * This file is a backport from FLOW3
20 *
21 * @author Robert Lemke <robert@typo3.org>
22 * @author Karsten Dambekalns <karsten@typo3.org>
23 * @author Ingo Renner <ingo@typo3.org>
24 * @api
25 */
26 abstract class AbstractBackend implements \TYPO3\CMS\Core\Cache\Backend\BackendInterface {
27
28 const DATETIME_EXPIRYTIME_UNLIMITED = '9999-12-31T23:59:59+0000';
29 const UNLIMITED_LIFETIME = 0;
30 /**
31 * Reference to the cache which uses this backend
32 *
33 * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
34 */
35 protected $cache;
36
37 /**
38 * @var string
39 */
40 protected $cacheIdentifier;
41
42 /**
43 * The current application context
44 *
45 * TYPO3 v4 note: This variable is currently unused in v4 context and set to
46 * "production" always. It is only kept to stay in sync with
47 * FLOW3 code.
48 *
49 * @var string
50 */
51 protected $context;
52
53 /**
54 * Default lifetime of a cache entry in seconds
55 *
56 * @var integer
57 */
58 protected $defaultLifetime = 3600;
59
60 /**
61 * Constructs this backend
62 *
63 * @param string $context FLOW3's application context
64 * @param array $options Configuration options - depends on the actual backend
65 * @throws \InvalidArgumentException
66 * @api
67 */
68 public function __construct($context, array $options = array()) {
69 $this->context = $context;
70 if (is_array($options) || $options instanceof \ArrayAccess) {
71 foreach ($options as $optionKey => $optionValue) {
72 $methodName = 'set' . ucfirst($optionKey);
73 if (method_exists($this, $methodName)) {
74 $this->{$methodName}($optionValue);
75 } else {
76 throw new \InvalidArgumentException('Invalid cache backend option "' . $optionKey . '" for backend of type "' . get_class($this) . '"', 1231267498);
77 }
78 }
79 }
80 }
81
82 /**
83 * Sets a reference to the cache frontend which uses this backend
84 *
85 * @param \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache The frontend for this backend
86 * @return void
87 * @api
88 */
89 public function setCache(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache) {
90 $this->cache = $cache;
91 $this->cacheIdentifier = $this->cache->getIdentifier();
92 }
93
94 /**
95 * Sets the default lifetime for this cache backend
96 *
97 * @param integer $defaultLifetime Default lifetime of this cache backend in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
98 * @return void
99 * @throws \InvalidArgumentException
100 * @api
101 */
102 public function setDefaultLifetime($defaultLifetime) {
103 if (!is_int($defaultLifetime) || $defaultLifetime < 0) {
104 throw new \InvalidArgumentException('The default lifetime must be given as a positive integer.', 1233072774);
105 }
106 $this->defaultLifetime = $defaultLifetime;
107 }
108
109 /**
110 * Calculates the expiry time by the given lifetime. If no lifetime is
111 * specified, the default lifetime is used.
112 *
113 * @param integer $lifetime The lifetime in seconds
114 * @return \DateTime The expiry time
115 */
116 protected function calculateExpiryTime($lifetime = NULL) {
117 if ($lifetime === self::UNLIMITED_LIFETIME || $lifetime === NULL && $this->defaultLifetime === self::UNLIMITED_LIFETIME) {
118 $expiryTime = new \DateTime(self::DATETIME_EXPIRYTIME_UNLIMITED, new \DateTimeZone('UTC'));
119 } else {
120 if ($lifetime === NULL) {
121 $lifetime = $this->defaultLifetime;
122 }
123 $expiryTime = new \DateTime('now +' . $lifetime . ' seconds', new \DateTimeZone('UTC'));
124 }
125 return $expiryTime;
126 }
127
128 }