TYPO3 CMS  TYPO3_8-7
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 
20 
31 class Registry implements SingletonInterface
32 {
36  protected $entries = [];
37 
41  protected $loadedNamespaces = [];
42 
52  public function get($namespace, $key, $defaultValue = null)
53  {
54  $this->validateNamespace($namespace);
55  if (!$this->isNamespaceLoaded($namespace)) {
56  $this->loadEntriesByNamespace($namespace);
57  }
58  return isset($this->entries[$namespace][$key]) ? $this->entries[$namespace][$key] : $defaultValue;
59  }
60 
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  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
82  ->getConnectionForTable('sys_registry');
83  $rowCount = $connection->count(
84  '*',
85  'sys_registry',
86  ['entry_namespace' => $namespace, 'entry_key' => $key]
87  );
88  if ((int)$rowCount < 1) {
89  $connection->insert(
90  'sys_registry',
91  ['entry_namespace' => $namespace, 'entry_key' => $key, 'entry_value' => $serializedValue],
92  ['entry_value' => Connection::PARAM_LOB]
93  );
94  } else {
95  $connection->update(
96  'sys_registry',
97  ['entry_value' => $serializedValue],
98  ['entry_namespace' => $namespace, 'entry_key' => $key],
99  ['entry_value' => Connection::PARAM_LOB]
100  );
101  }
102  $this->entries[$namespace][$key] = $value;
103  }
104 
112  public function remove($namespace, $key)
113  {
114  $this->validateNamespace($namespace);
115  GeneralUtility::makeInstance(ConnectionPool::class)
116  ->getConnectionForTable('sys_registry')
117  ->delete(
118  'sys_registry',
119  ['entry_namespace' => $namespace, 'entry_key' => $key]
120  );
121  unset($this->entries[$namespace][$key]);
122  }
123 
130  public function removeAllByNamespace($namespace)
131  {
132  $this->validateNamespace($namespace);
133  GeneralUtility::makeInstance(ConnectionPool::class)
134  ->getConnectionForTable('sys_registry')
135  ->delete(
136  'sys_registry',
137  ['entry_namespace' => $namespace]
138  );
139  unset($this->entries[$namespace]);
140  }
141 
148  protected function isNamespaceLoaded($namespace)
149  {
150  return isset($this->loadedNamespaces[$namespace]);
151  }
152 
159  protected function loadEntriesByNamespace($namespace)
160  {
161  $this->validateNamespace($namespace);
162  $this->entries[$namespace] = [];
163  $result = GeneralUtility::makeInstance(ConnectionPool::class)
164  ->getConnectionForTable('sys_registry')
165  ->select(
166  ['entry_key', 'entry_value'],
167  'sys_registry',
168  ['entry_namespace' => $namespace]
169  );
170  while ($row = $result->fetch()) {
171  $this->entries[$namespace][$row['entry_key']] = unserialize($row['entry_value']);
172  }
173  $this->loadedNamespaces[$namespace] = true;
174  }
175 
183  protected function validateNamespace($namespace)
184  {
185  if (strlen($namespace) < 2) {
186  throw new \InvalidArgumentException('Given namespace must be longer than two characters.', 1249755131);
187  }
188  }
189 }
removeAllByNamespace($namespace)
Definition: Registry.php:130
validateNamespace($namespace)
Definition: Registry.php:183
static makeInstance($className,... $constructorArguments)
isNamespaceLoaded($namespace)
Definition: Registry.php:148
loadEntriesByNamespace($namespace)
Definition: Registry.php:159