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