[TASK] FormEngine JS refactoring: copy JS files
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Registry.php
1 <?php
2 namespace TYPO3\CMS\Core;
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 /**
18 * A class to store and retrieve entries in a registry database table.
19 *
20 * The intention is to have a place where we can store things (mainly settings)
21 * that should live for more than one request, longer than a session, and that
22 * shouldn't expire like it would with a cache. You can actually think of it
23 * being like the Windows Registry in some ways.
24 *
25 * Credits: Heavily inspired by Drupal's variable_*() functions.
26 *
27 * @author Ingo Renner <ingo@typo3.org>
28 * @author Bastian Waidelich <bastian@typo3.org>
29 */
30 class Registry implements \TYPO3\CMS\Core\SingletonInterface {
31
32 /**
33 * @var array
34 */
35 protected $entries = array();
36
37 /**
38 * @var array
39 */
40 protected $loadedNamespaces = array();
41
42 /**
43 * Returns a persistent entry.
44 *
45 * @param string $namespace Extension key for extensions starting with 'tx_' / 'Tx_' / 'user_' or 'core' for core registry entries
46 * @param string $key The key of the entry to return.
47 * @param mixed $defaultValue Optional default value to use if this entry has never been set. Defaults to NULL.
48 * @return mixed The value of the entry.
49 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
50 */
51 public function get($namespace, $key, $defaultValue = NULL) {
52 $this->validateNamespace($namespace);
53 if (!$this->isNamespaceLoaded($namespace)) {
54 $this->loadEntriesByNamespace($namespace);
55 }
56 return isset($this->entries[$namespace][$key]) ? $this->entries[$namespace][$key] : $defaultValue;
57 }
58
59 /**
60 * Sets a persistent entry.
61 *
62 * This is the main method that can be used to store a key-value. It is name spaced with
63 * a unique string. This name space should be chosen from extensions that it is unique.
64 * It is advised to use something like 'tx_extensionname'. The prefix 'core' is reserved
65 * for the TYPO3 core.
66 *
67 * Do not store binary data into the registry, it's not build to do that,
68 * instead use the proper way to store binary data: The filesystem.
69 *
70 * @param string $namespace Extension key for extensions starting with 'tx_' / 'Tx_' / 'user_' or 'core' for core registry entries.
71 * @param string $key The key of the entry to set.
72 * @param mixed $value The value to set. This can be any PHP data type; this class takes care of serialization if necessary.
73 * @return void
74 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
75 */
76 public function set($namespace, $key, $value) {
77 $this->validateNamespace($namespace);
78 if (!$this->isNamespaceLoaded($namespace)) {
79 $this->loadEntriesByNamespace($namespace);
80 }
81 $serializedValue = serialize($value);
82 $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'));
83 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) < 1) {
84 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_registry', array(
85 'entry_namespace' => $namespace,
86 'entry_key' => $key,
87 'entry_value' => $serializedValue
88 ));
89 } else {
90 $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(
91 'entry_value' => $serializedValue
92 ));
93 }
94 $this->entries[$namespace][$key] = $value;
95 }
96
97 /**
98 * Unsets a persistent entry.
99 *
100 * @param string $namespace Namespace. extension key for extensions or 'core' for core registry entries
101 * @param string $key The key of the entry to unset.
102 * @return void
103 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
104 */
105 public function remove($namespace, $key) {
106 $this->validateNamespace($namespace);
107 $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'));
108 unset($this->entries[$namespace][$key]);
109 }
110
111 /**
112 * Unsets all persistent entries of the given namespace.
113 *
114 * @param string $namespace Namespace. extension key for extensions or 'core' for core registry entries
115 * @return void
116 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
117 */
118 public function removeAllByNamespace($namespace) {
119 $this->validateNamespace($namespace);
120 $GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_registry', 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry'));
121 unset($this->entries[$namespace]);
122 }
123
124 /**
125 * check if the given namespace is loaded
126 *
127 * @param string $namespace Namespace. extension key for extensions or 'core' for core registry entries
128 *
129 * @return bool
130 */
131 protected function isNamespaceLoaded($namespace) {
132 return isset($this->loadedNamespaces[$namespace]);
133 }
134
135 /**
136 * Loads all entries of the given namespace into the internal $entries cache.
137 *
138 * @param string $namespace Namespace. extension key for extensions or 'core' for core registry entries
139 * @return void
140 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
141 */
142 protected function loadEntriesByNamespace($namespace) {
143 $this->validateNamespace($namespace);
144 $storedEntries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_registry', 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry'));
145 foreach ($storedEntries as $storedEntry) {
146 $key = $storedEntry['entry_key'];
147 $this->entries[$namespace][$key] = unserialize($storedEntry['entry_value']);
148 }
149 $this->loadedNamespaces[$namespace] = TRUE;
150 }
151
152 /**
153 * Checks the given namespace.
154 * It must be at least two characters long. The word 'core' is reserved for
155 * TYPO3 core usage.
156 *
157 * If it does not have a valid format an exception is thrown.
158 *
159 * @param string $namespace Namespace
160 * @return void
161 * @throws \InvalidArgumentException Throws an exception if the given namespace is not valid
162 */
163 protected function validateNamespace($namespace) {
164 if (strlen($namespace) < 2) {
165 throw new \InvalidArgumentException('Given namespace must be longer than two characters.', 1249755131);
166 }
167 }
168
169 }