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