TYPO3 CMS  TYPO3_7-6
Registry.php
Go to the documentation of this file.
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 
28 {
32  protected $entries = [];
33 
37  protected $loadedNamespaces = [];
38 
48  public function get($namespace, $key, $defaultValue = null)
49  {
50  $this->validateNamespace($namespace);
51  if (!$this->isNamespaceLoaded($namespace)) {
52  $this->loadEntriesByNamespace($namespace);
53  }
54  return isset($this->entries[$namespace][$key]) ? $this->entries[$namespace][$key] : $defaultValue;
55  }
56 
74  public function set($namespace, $key, $value)
75  {
76  $this->validateNamespace($namespace);
77  if (!$this->isNamespaceLoaded($namespace)) {
78  $this->loadEntriesByNamespace($namespace);
79  }
80  $serializedValue = serialize($value);
81  $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'));
82  if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) < 1) {
83  $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_registry', [
84  'entry_namespace' => $namespace,
85  'entry_key' => $key,
86  'entry_value' => $serializedValue
87  ]);
88  } else {
89  $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'), [
90  'entry_value' => $serializedValue
91  ]);
92  }
93  $this->entries[$namespace][$key] = $value;
94  }
95 
104  public function remove($namespace, $key)
105  {
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 
118  public function removeAllByNamespace($namespace)
119  {
120  $this->validateNamespace($namespace);
121  $GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_registry', 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry'));
122  unset($this->entries[$namespace]);
123  }
124 
132  protected function isNamespaceLoaded($namespace)
133  {
134  return isset($this->loadedNamespaces[$namespace]);
135  }
136 
144  protected function loadEntriesByNamespace($namespace)
145  {
146  $this->validateNamespace($namespace);
147  $storedEntries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_registry', 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry'));
148  foreach ($storedEntries as $storedEntry) {
149  $key = $storedEntry['entry_key'];
150  $this->entries[$namespace][$key] = unserialize($storedEntry['entry_value']);
151  }
152  $this->loadedNamespaces[$namespace] = true;
153  }
154 
166  protected function validateNamespace($namespace)
167  {
168  if (strlen($namespace) < 2) {
169  throw new \InvalidArgumentException('Given namespace must be longer than two characters.', 1249755131);
170  }
171  }
172 }
removeAllByNamespace($namespace)
Definition: Registry.php:118
validateNamespace($namespace)
Definition: Registry.php:166
isNamespaceLoaded($namespace)
Definition: Registry.php:132
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
loadEntriesByNamespace($namespace)
Definition: Registry.php:144