[FEATURE] Add wincache as a cache backend
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_registry.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Ingo Renner <ingo@typo3.org>
6 *
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * A class to store and retrieve entries in a registry database table.
31 *
32 * The intention is to have a place where we can store things (mainly settings)
33 * that should live for more than one request, longer than a session, and that
34 * shouldn't expire like it would with a cache. You can actually think of it
35 * being like the Windows Registry in some ways.
36 *
37 * Credits: Heavily inspired by Drupal's variable_*() functions.
38 *
39 * @author Ingo Renner <ingo@typo3.org>
40 * @author Bastian Waidelich <bastian@typo3.org>
41 * @package TYPO3
42 * @subpackage t3lib
43 */
44 class t3lib_Registry implements t3lib_Singleton {
45
46 /**
47 * @var array
48 */
49 protected $entries = array();
50
51 /**
52 * Returns a persistent entry.
53 *
54 * @param string $namespace Extension key for extensions starting with 'tx_' / 'Tx_' / 'user_' or 'core' for core registry entries
55 * @param string $key The key of the entry to return.
56 * @param mixed $defaultValue Optional default value to use if this entry has never been set. Defaults to NULL.
57 * @return mixed The value of the entry.
58 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
59 */
60 public function get($namespace, $key, $defaultValue = NULL) {
61 if (!isset($this->entries[$namespace])) {
62 $this->loadEntriesByNamespace($namespace);
63 }
64
65 return isset($this->entries[$namespace][$key]) ? $this->entries[$namespace][$key] : $defaultValue;
66 }
67
68 /**
69 * Sets a persistent entry.
70 *
71 * This is the main method that can be used to store a key-value. It is name spaced with
72 * a unique string. This name space should be chosen from extensions that it is unique.
73 * It is advised to use something like 'tx_extensionname'. The prefix 'core' is reserved
74 * for the TYPO3 core.
75 *
76 * Do not store binary data into the registry, it's not build to do that,
77 * instead use the proper way to store binary data: The filesystem.
78 *
79 * @param string $namespace Extension key for extensions starting with 'tx_' / 'Tx_' / 'user_' or 'core' for core registry entries.
80 * @param string $key The key of the entry to set.
81 * @param mixed $value The value to set. This can be any PHP data type; this class takes care of serialization if necessary.
82 * @return void
83 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
84 */
85 public function set($namespace, $key, $value) {
86 $this->validateNamespace($namespace);
87 $serializedValue = serialize($value);
88
89 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
90 'uid',
91 'sys_registry',
92 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry')
93 . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry')
94 );
95 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) < 1) {
96 $GLOBALS['TYPO3_DB']->exec_INSERTquery(
97 'sys_registry',
98 array(
99 'entry_namespace' => $namespace,
100 'entry_key' => $key,
101 'entry_value' => $serializedValue
102 )
103 );
104 } else {
105 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
106 'sys_registry',
107 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry')
108 . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry'),
109 array(
110 'entry_value' => $serializedValue
111 )
112 );
113 }
114
115 $this->entries[$namespace][$key] = $value;
116 }
117
118 /**
119 * Unsets a persistent entry.
120 *
121 * @param string $namespace Namespace. extension key for extensions or 'core' for core registry entries
122 * @param string $key The key of the entry to unset.
123 * @return void
124 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
125 */
126 public function remove($namespace, $key) {
127 $this->validateNamespace($namespace);
128
129 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
130 'sys_registry',
131 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry')
132 . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry')
133 );
134
135 unset($this->entries[$namespace][$key]);
136 }
137
138 /**
139 * Unsets all persistent entries of the given namespace.
140 *
141 * @param string $namespace Namespace. extension key for extensions or 'core' for core registry entries
142 * @return void
143 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
144 */
145 public function removeAllByNamespace($namespace) {
146 $this->validateNamespace($namespace);
147
148 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
149 'sys_registry',
150 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry')
151 );
152
153 unset($this->entries[$namespace]);
154 }
155
156 /**
157 * Loads all entries of the given namespace into the internal $entries cache.
158 *
159 * @param string $namespace Namespace. extension key for extensions or 'core' for core registry entries
160 * @return void
161 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
162 */
163 protected function loadEntriesByNamespace($namespace) {
164 $this->validateNamespace($namespace);
165
166 $storedEntries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
167 '*',
168 'sys_registry',
169 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry')
170 );
171
172 foreach ($storedEntries as $storedEntry) {
173 $key = $storedEntry['entry_key'];
174 $this->entries[$namespace][$key] = unserialize($storedEntry['entry_value']);
175 }
176 }
177
178 /**
179 * Checks the given namespace.
180 * It must be at least two characters long. The word 'core' is reserved for
181 * TYPO3 core usage.
182 *
183 * If it does not have a valid format an exception is thrown.
184 *
185 * @param string $namespace Namespace
186 * @return void
187 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
188 */
189 protected function validateNamespace($namespace) {
190 if (strlen($namespace) < 2) {
191 throw new InvalidArgumentException(
192 'Given namespace must be longer than two characters.',
193 1249755131
194 );
195 }
196 }
197 }
198
199 ?>