[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Frontend / VariableFrontend.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 * A cache frontend for any kinds of PHP variables
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 class VariableFrontend extends \TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend {
26
27 /**
28 * If the extension "igbinary" is installed, use it for increased performance.
29 * Caching the result of extension_loaded() here is faster than calling extension_loaded() multiple times.
30 *
31 * @var boolean
32 */
33 protected $useIgBinary = FALSE;
34
35 /**
36 * Initializes this cache frontend
37 *
38 * @return void
39 */
40 public function initializeObject() {
41 $this->useIgBinary = extension_loaded('igbinary');
42 }
43
44 /**
45 * Saves the value of a PHP variable in the cache. Note that the variable
46 * will be serialized if necessary.
47 *
48 * @param string $entryIdentifier An identifier used for this cache entry
49 * @param mixed $variable The variable to cache
50 * @param array $tags Tags to associate with this cache entry
51 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
52 * @return void
53 * @throws \InvalidArgumentException if the identifier or tag is not valid
54 * @api
55 */
56 public function set($entryIdentifier, $variable, array $tags = array(), $lifetime = NULL) {
57 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
58 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058264);
59 }
60 foreach ($tags as $tag) {
61 if (!$this->isValidTag($tag)) {
62 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233058269);
63 }
64 }
65 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_variablefrontend.php']['set'])) {
66 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_variablefrontend.php']['set'] as $_funcRef) {
67 $params = array(
68 'entryIdentifier' => &$entryIdentifier,
69 'variable' => &$variable,
70 'tags' => &$tags,
71 'lifetime' => &$lifetime
72 );
73 \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($_funcRef, $params, $this);
74 }
75 }
76 if ($this->useIgBinary === TRUE) {
77 $this->backend->set($entryIdentifier, igbinary_serialize($variable), $tags, $lifetime);
78 } else {
79 $this->backend->set($entryIdentifier, serialize($variable), $tags, $lifetime);
80 }
81 }
82
83 /**
84 * Finds and returns a variable value from the cache.
85 *
86 * @param string $entryIdentifier Identifier of the cache entry to fetch
87 * @return mixed The value
88 * @throws \InvalidArgumentException if the identifier is not valid
89 * @api
90 */
91 public function get($entryIdentifier) {
92 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
93 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058294);
94 }
95 $rawResult = $this->backend->get($entryIdentifier);
96 if ($rawResult === FALSE) {
97 return FALSE;
98 } else {
99 return $this->useIgBinary === TRUE ? igbinary_unserialize($rawResult) : unserialize($rawResult);
100 }
101 }
102
103 /**
104 * Finds and returns all cache entries which are tagged by the specified tag.
105 *
106 * @param string $tag The tag to search for
107 * @return array An array with the content of all matching entries. An empty array if no entries matched
108 * @throws \InvalidArgumentException if the tag is not valid
109 * @api
110 */
111 public function getByTag($tag) {
112 if (!$this->isValidTag($tag)) {
113 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233058312);
114 }
115 $entries = array();
116 $identifiers = $this->backend->findIdentifiersByTag($tag);
117 foreach ($identifiers as $identifier) {
118 $rawResult = $this->backend->get($identifier);
119 if ($rawResult !== FALSE) {
120 $entries[] = $this->useIgBinary === TRUE ? igbinary_unserialize($rawResult) : unserialize($rawResult);
121 }
122 }
123 return $entries;
124 }
125
126 }