[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Frontend / AbstractFrontend.php
1 <?php
2 namespace TYPO3\CMS\Core\Cache\Frontend;
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 cache
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 * @api
24 */
25 abstract class AbstractFrontend implements \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface {
26
27 /**
28 * Identifies this cache
29 *
30 * @var string
31 */
32 protected $identifier;
33
34 /**
35 * @var \TYPO3\CMS\Core\Cache\Backend\AbstractBackend|\TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface
36 */
37 protected $backend;
38
39 /**
40 * Constructs the cache
41 *
42 * @param string $identifier A identifier which describes this cache
43 * @param \TYPO3\CMS\Core\Cache\Backend\BackendInterface $backend Backend to be used for this cache
44 * @throws \InvalidArgumentException if the identifier doesn't match PATTERN_ENTRYIDENTIFIER
45 */
46 public function __construct($identifier, \TYPO3\CMS\Core\Cache\Backend\BackendInterface $backend) {
47 if (preg_match(self::PATTERN_ENTRYIDENTIFIER, $identifier) !== 1) {
48 throw new \InvalidArgumentException('"' . $identifier . '" is not a valid cache identifier.', 1203584729);
49 }
50 $this->identifier = $identifier;
51 $this->backend = $backend;
52 $this->backend->setCache($this);
53 }
54
55 /**
56 * Returns this cache's identifier
57 *
58 * @return string The identifier for this cache
59 * @api
60 */
61 public function getIdentifier() {
62 return $this->identifier;
63 }
64
65 /**
66 * Returns the backend used by this cache
67 *
68 * @return \TYPO3\CMS\Core\Cache\Backend\BackendInterface The backend used by this cache
69 * @api
70 */
71 public function getBackend() {
72 return $this->backend;
73 }
74
75 /**
76 * Checks if a cache entry with the specified identifier exists.
77 *
78 * @param string $entryIdentifier An identifier specifying the cache entry
79 * @return boolean TRUE if such an entry exists, FALSE if not
80 * @throws \InvalidArgumentException If $entryIdentifier is invalid
81 * @api
82 */
83 public function has($entryIdentifier) {
84 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
85 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058486);
86 }
87 return $this->backend->has($entryIdentifier);
88 }
89
90 /**
91 * Removes the given cache entry from the cache.
92 *
93 * @param string $entryIdentifier An identifier specifying the cache entry
94 * @return boolean TRUE if such an entry exists, FALSE if not
95 * @throws \InvalidArgumentException
96 * @api
97 */
98 public function remove($entryIdentifier) {
99 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
100 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058495);
101 }
102 return $this->backend->remove($entryIdentifier);
103 }
104
105 /**
106 * Removes all cache entries of this cache.
107 *
108 * @return void
109 * @api
110 */
111 public function flush() {
112 $this->backend->flush();
113 }
114
115 /**
116 * Removes all cache entries of this cache which are tagged by the specified tag.
117 *
118 * @param string $tag The tag the entries must have
119 * @return void
120 * @throws \InvalidArgumentException
121 * @api
122 */
123 public function flushByTag($tag) {
124 if (!$this->isValidTag($tag)) {
125 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233057359);
126 }
127 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_abstractfrontend.php']['flushByTag'])) {
128 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_abstractfrontend.php']['flushByTag'] as $_funcRef) {
129 $params = array('tag' => $tag);
130 \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($_funcRef, $params, $this);
131 }
132 }
133 if ($this->backend instanceof \TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface) {
134 $this->backend->flushByTag($tag);
135 }
136 }
137
138 /**
139 * Does garbage collection
140 *
141 * @return void
142 * @api
143 */
144 public function collectGarbage() {
145 $this->backend->collectGarbage();
146 }
147
148 /**
149 * Checks the validity of an entry identifier. Returns TRUE if it's valid.
150 *
151 * @param string $identifier An identifier to be checked for validity
152 * @return boolean
153 * @api
154 */
155 public function isValidEntryIdentifier($identifier) {
156 return preg_match(self::PATTERN_ENTRYIDENTIFIER, $identifier) === 1;
157 }
158
159 /**
160 * Checks the validity of a tag. Returns TRUE if it's valid.
161 *
162 * @param string $tag An identifier to be checked for validity
163 * @return boolean
164 * @api
165 */
166 public function isValidTag($tag) {
167 return preg_match(self::PATTERN_TAG, $tag) === 1;
168 }
169
170 }