[BUGFIX] Remove early return in PageTreeDataProvider
[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 /**
31 * A class to store and retrieve entries in a registry database table.
32 *
33 * The intention is to have a place where we can store things (mainly settings)
34 * that should live for more than one request, longer than a session, and that
35 * shouldn't expire like it would with a cache. You can actually think of it
36 * being like the Windows Registry in some ways.
37 *
38 * Credits: Heavily inspired by Drupal's variable_*() functions.
39 *
40 * @author Ingo Renner <ingo@typo3.org>
41 * @author Bastian Waidelich <bastian@typo3.org>
42 * @package TYPO3
43 * @subpackage t3lib
44 */
45 class t3lib_Registry implements t3lib_Singleton {
46
47 /**
48 * @var array
49 */
50 protected $entries = array();
51
52
53 /**
54 * Returns a persistent entry.
55 *
56 * @param string Extension key for extensions starting with 'tx_' / 'Tx_' / 'user_' or 'core' for core registry entries
57 * @param string The key of the entry to return.
58 * @param mixed Optional default value to use if this entry has never been set. Defaults to NULL.
59 * @return mixed The value of the entry.
60 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
61 */
62 public function get($namespace, $key, $defaultValue = NULL) {
63 if (!isset($this->entries[$namespace])) {
64 $this->loadEntriesByNamespace($namespace);
65 }
66
67 return isset($this->entries[$namespace][$key]) ? $this->entries[$namespace][$key] : $defaultValue;
68 }
69
70 /**
71 * Sets a persistent entry.
72 * Do not store binary data into the registry, it's not build to do that,
73 * instead use the proper way to store binary data: The filesystem.
74 *
75 * @param string Extension key for extensions starting with 'tx_' / 'Tx_' / 'user_' or 'core' for core registry entries.
76 * @param string The key of the entry to set.
77 * @param mixed The value to set. This can be any PHP data type; this class takes care of serialization if necessary.
78 * @return void
79 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
80 */
81 public function set($namespace, $key, $value) {
82 $this->validateNamespace($namespace);
83 $serializedValue = serialize($value);
84
85 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
86 'uid',
87 'sys_registry',
88 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry')
89 . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry')
90 );
91 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) < 1) {
92 $GLOBALS['TYPO3_DB']->exec_INSERTquery(
93 'sys_registry',
94 array(
95 'entry_namespace' => $namespace,
96 'entry_key' => $key,
97 'entry_value' => $serializedValue
98 )
99 );
100 } else {
101 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
102 'sys_registry',
103 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry')
104 . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry'),
105 array(
106 'entry_value' => $serializedValue
107 )
108 );
109 }
110
111 $this->entries[$namespace][$key] = $value;
112 }
113
114 /**
115 * Unsets a persistent entry.
116 *
117 * @param string Namespace. extension key for extensions or 'core' for core registry entries
118 * @param string The key of the entry to unset.
119 * @return void
120 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
121 */
122 public function remove($namespace, $key) {
123 $this->validateNamespace($namespace);
124
125 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
126 'sys_registry',
127 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry')
128 . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry')
129 );
130
131 unset($this->entries[$namespace][$key]);
132 }
133
134 /**
135 * Unsets all persistent entries of the given namespace.
136 *
137 * @param string Namespace. extension key for extensions or 'core' for core registry entries
138 * @return void
139 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
140 */
141 public function removeAllByNamespace($namespace) {
142 $this->validateNamespace($namespace);
143
144 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
145 'sys_registry',
146 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry')
147 );
148
149 unset($this->entries[$namespace]);
150 }
151
152 /**
153 * Loads all entries of the given namespace into the internal $entries cache.
154 *
155 * @param string Namespace. extension key for extensions or 'core' for core registry entries
156 * @return void
157 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
158 */
159 protected function loadEntriesByNamespace($namespace) {
160 $this->validateNamespace($namespace);
161
162 $storedEntries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
163 '*',
164 'sys_registry',
165 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry')
166 );
167
168 foreach ($storedEntries as $storedEntry) {
169 $key = $storedEntry['entry_key'];
170 $this->entries[$namespace][$key] = unserialize($storedEntry['entry_value']);
171 }
172 }
173
174 /**
175 * Checks the given namespace. If it does not have a valid format an
176 * exception is thrown.
177 * Allowed namespaces are 'core', 'tx_*', 'Tx_*' and 'user_*'
178 *
179 * @param string Namespace. extension key for extensions or 'core' for core registry entries
180 * @return void
181 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid
182 */
183 protected function validateNamespace($namespace) {
184 if (t3lib_div::hasValidClassPrefix($namespace)) {
185 return;
186 }
187
188 if ($namespace !== 'core') {
189 throw new InvalidArgumentException(
190 '"' . $namespace . '" is no valid Namespace. The namespace has to be prefixed with "tx_", "Tx_", "user_" or must be equal to "core"',
191 1249755131
192 );
193 }
194 }
195
196 }
197
198
199 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_registry.php'])) {
200 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_registry.php']);
201 }
202
203 ?>