TYPO3 CMS  TYPO3_7-6
AbstractBackend.php
Go to the documentation of this file.
1 <?php
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 
24 {
25  const DATETIME_EXPIRYTIME_UNLIMITED = '9999-12-31T23:59:59+0000';
26  const UNLIMITED_LIFETIME = 0;
32  protected $cache;
33 
37  protected $cacheIdentifier;
38 
48  protected $context;
49 
55  protected $defaultLifetime = 3600;
56 
65  public function __construct($context, array $options = [])
66  {
67  $this->context = $context;
68  if (is_array($options) || $options instanceof \ArrayAccess) {
69  foreach ($options as $optionKey => $optionValue) {
70  $methodName = 'set' . ucfirst($optionKey);
71  if (method_exists($this, $methodName)) {
72  $this->{$methodName}($optionValue);
73  } else {
74  throw new \InvalidArgumentException('Invalid cache backend option "' . $optionKey . '" for backend of type "' . get_class($this) . '"', 1231267498);
75  }
76  }
77  }
78  }
79 
87  public function setCache(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache)
88  {
89  $this->cache = $cache;
90  $this->cacheIdentifier = $this->cache->getIdentifier();
91  }
92 
102  {
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 
116  protected function calculateExpiryTime($lifetime = null)
117  {
118  if ($lifetime === self::UNLIMITED_LIFETIME || $lifetime === null && $this->defaultLifetime === self::UNLIMITED_LIFETIME) {
119  $expiryTime = new \DateTime(self::DATETIME_EXPIRYTIME_UNLIMITED, new \DateTimeZone('UTC'));
120  } else {
121  if ($lifetime === null) {
122  $lifetime = $this->defaultLifetime;
123  }
124  $expiryTime = new \DateTime('now +' . $lifetime . ' seconds', new \DateTimeZone('UTC'));
125  }
126  return $expiryTime;
127  }
128 }
setCache(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache)
__construct($context, array $options=[])