TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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 
19 
30 class Registry implements SingletonInterface
31 {
35  protected $entries = [];
36 
40  protected $loadedNamespaces = [];
41 
51  public function get($namespace, $key, $defaultValue = null)
52  {
53  $this->validateNamespace($namespace);
54  if (!$this->isNamespaceLoaded($namespace)) {
55  $this->loadEntriesByNamespace($namespace);
56  }
57  return isset($this->entries[$namespace][$key]) ? $this->entries[$namespace][$key] : $defaultValue;
58  }
59 
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  );
93  } else {
94  $connection->update(
95  'sys_registry',
96  ['entry_value' => $serializedValue],
97  ['entry_namespace' => $namespace, 'entry_key' => $key]
98  );
99  }
100  $this->entries[$namespace][$key] = $value;
101  }
102 
111  public function remove($namespace, $key)
112  {
113  $this->validateNamespace($namespace);
114  GeneralUtility::makeInstance(ConnectionPool::class)
115  ->getConnectionForTable('sys_registry')
116  ->delete(
117  'sys_registry',
118  ['entry_namespace' => $namespace, 'entry_key' => $key]
119  );
120  unset($this->entries[$namespace][$key]);
121  }
122 
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 
160  protected function loadEntriesByNamespace($namespace)
161  {
162  $this->validateNamespace($namespace);
163  $this->entries[$namespace] = [];
164  $result = GeneralUtility::makeInstance(ConnectionPool::class)
165  ->getConnectionForTable('sys_registry')
166  ->select(
167  ['entry_key', 'entry_value'],
168  'sys_registry',
169  ['entry_namespace' => $namespace]
170  );
171  while ($row = $result->fetch()) {
172  $this->entries[$namespace][$row['entry_key']] = unserialize($row['entry_value']);
173  }
174  $this->loadedNamespaces[$namespace] = true;
175  }
176 
185  protected function validateNamespace($namespace)
186  {
187  if (strlen($namespace) < 2) {
188  throw new \InvalidArgumentException('Given namespace must be longer than two characters.', 1249755131);
189  }
190  }
191 }
set($namespace, $key, $value)
Definition: Registry.php:74
isNamespaceLoaded($namespace)
Definition: Registry.php:148
validateNamespace($namespace)
Definition: Registry.php:185
loadEntriesByNamespace($namespace)
Definition: Registry.php:160
static makeInstance($className,...$constructorArguments)
removeAllByNamespace($namespace)
Definition: Registry.php:130