[TASK] Update copyright year to 2013
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Registry.php
1 <?php
2 namespace TYPO3\CMS\Core;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Ingo Renner <ingo@typo3.org>
8 *
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
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 */
43 class Registry implements \TYPO3\CMS\Core\SingletonInterface {
44
45 /**
46 * @var array
47 */
48 protected $entries = array();
49
50 /**
51 * Returns a persistent entry.
52 *
53 * @param string $namespace Extension key for extensions starting with 'tx_' / 'Tx_' / 'user_' or 'core' for core registry entries
54 * @param string $key The key of the entry to return.
55 * @param mixed $defaultValue Optional default value to use if this entry has never been set. Defaults to NULL.
56 * @return mixed The value of the entry.
57 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
58 */
59 public function get($namespace, $key, $defaultValue = NULL) {
60 if (!isset($this->entries[$namespace])) {
61 $this->loadEntriesByNamespace($namespace);
62 }
63 return isset($this->entries[$namespace][$key]) ? $this->entries[$namespace][$key] : $defaultValue;
64 }
65
66 /**
67 * Sets a persistent entry.
68 *
69 * This is the main method that can be used to store a key-value. It is name spaced with
70 * a unique string. This name space should be chosen from extensions that it is unique.
71 * It is advised to use something like 'tx_extensionname'. The prefix 'core' is reserved
72 * for the TYPO3 core.
73 *
74 * Do not store binary data into the registry, it's not build to do that,
75 * instead use the proper way to store binary data: The filesystem.
76 *
77 * @param string $namespace Extension key for extensions starting with 'tx_' / 'Tx_' / 'user_' or 'core' for core registry entries.
78 * @param string $key The key of the entry to set.
79 * @param mixed $value The value to set. This can be any PHP data type; this class takes care of serialization if necessary.
80 * @return void
81 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
82 */
83 public function set($namespace, $key, $value) {
84 $this->validateNamespace($namespace);
85 $serializedValue = serialize($value);
86 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'sys_registry', 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry') . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry'));
87 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) < 1) {
88 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_registry', array(
89 'entry_namespace' => $namespace,
90 'entry_key' => $key,
91 'entry_value' => $serializedValue
92 ));
93 } else {
94 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_registry', 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry') . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry'), array(
95 'entry_value' => $serializedValue
96 ));
97 }
98 $this->entries[$namespace][$key] = $value;
99 }
100
101 /**
102 * Unsets a persistent entry.
103 *
104 * @param string $namespace Namespace. extension key for extensions or 'core' for core registry entries
105 * @param string $key The key of the entry to unset.
106 * @return void
107 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
108 */
109 public function remove($namespace, $key) {
110 $this->validateNamespace($namespace);
111 $GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_registry', 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry') . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry'));
112 unset($this->entries[$namespace][$key]);
113 }
114
115 /**
116 * Unsets all persistent entries of the given namespace.
117 *
118 * @param string $namespace Namespace. extension key for extensions or 'core' for core registry entries
119 * @return void
120 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
121 */
122 public function removeAllByNamespace($namespace) {
123 $this->validateNamespace($namespace);
124 $GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_registry', 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry'));
125 unset($this->entries[$namespace]);
126 }
127
128 /**
129 * Loads all entries of the given namespace into the internal $entries cache.
130 *
131 * @param string $namespace Namespace. extension key for extensions or 'core' for core registry entries
132 * @return void
133 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
134 */
135 protected function loadEntriesByNamespace($namespace) {
136 $this->validateNamespace($namespace);
137 $storedEntries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_registry', 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry'));
138 foreach ($storedEntries as $storedEntry) {
139 $key = $storedEntry['entry_key'];
140 $this->entries[$namespace][$key] = unserialize($storedEntry['entry_value']);
141 }
142 }
143
144 /**
145 * Checks the given namespace.
146 * It must be at least two characters long. The word 'core' is reserved for
147 * TYPO3 core usage.
148 *
149 * If it does not have a valid format an exception is thrown.
150 *
151 * @param string $namespace Namespace
152 * @return void
153 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
154 */
155 protected function validateNamespace($namespace) {
156 if (strlen($namespace) < 2) {
157 throw new \InvalidArgumentException('Given namespace must be longer than two characters.', 1249755131);
158 }
159 }
160
161 }
162
163
164 ?>