‪TYPO3CMS  11.5
Registry.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core;
17 
21 
33 {
37  protected ‪$entries = [];
38 
42  protected ‪$loadedNamespaces = [];
43 
53  public function get($namespace, $key, $defaultValue = null)
54  {
55  $this->‪validateNamespace($namespace);
56  if (!$this->‪isNamespaceLoaded($namespace)) {
57  $this->‪loadEntriesByNamespace($namespace);
58  }
59  return $this->entries[$namespace][$key] ?? $defaultValue;
60  }
61 
75  public function set($namespace, $key, $value)
76  {
77  $this->‪validateNamespace($namespace);
78  if (!$this->‪isNamespaceLoaded($namespace)) {
79  $this->‪loadEntriesByNamespace($namespace);
80  }
81  $serializedValue = serialize($value);
82  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
83  ->getConnectionForTable('sys_registry');
84  $rowCount = $connection->count(
85  '*',
86  'sys_registry',
87  ['entry_namespace' => $namespace, 'entry_key' => $key]
88  );
89  if ((int)$rowCount < 1) {
90  $connection->insert(
91  'sys_registry',
92  ['entry_namespace' => $namespace, 'entry_key' => $key, 'entry_value' => $serializedValue],
93  ['entry_value' => ‪Connection::PARAM_LOB]
94  );
95  } else {
96  $connection->update(
97  'sys_registry',
98  ['entry_value' => $serializedValue],
99  ['entry_namespace' => $namespace, 'entry_key' => $key],
100  ['entry_value' => ‪Connection::PARAM_LOB]
101  );
102  }
103  $this->entries[$namespace][$key] = $value;
104  }
105 
113  public function remove($namespace, $key)
114  {
115  $this->‪validateNamespace($namespace);
116  GeneralUtility::makeInstance(ConnectionPool::class)
117  ->getConnectionForTable('sys_registry')
118  ->delete(
119  'sys_registry',
120  ['entry_namespace' => $namespace, 'entry_key' => $key]
121  );
122  unset($this->entries[$namespace][$key]);
123  }
124 
131  public function ‪removeAllByNamespace($namespace)
132  {
133  $this->‪validateNamespace($namespace);
134  GeneralUtility::makeInstance(ConnectionPool::class)
135  ->getConnectionForTable('sys_registry')
136  ->delete(
137  'sys_registry',
138  ['entry_namespace' => $namespace]
139  );
140  unset($this->entries[$namespace]);
141  }
142 
149  protected function ‪isNamespaceLoaded($namespace)
150  {
151  return isset($this->loadedNamespaces[$namespace]);
152  }
153 
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->fetchAssociative()) {
172  $this->entries[$namespace][$row['entry_key']] = unserialize($row['entry_value']);
173  }
174  $this->loadedNamespaces[$namespace] = true;
175  }
176 
184  protected function ‪validateNamespace($namespace)
185  {
186  if (strlen($namespace) < 2) {
187  throw new \InvalidArgumentException('Given namespace must be longer than two characters.', 1249755131);
188  }
189  }
190 }
‪TYPO3\CMS\Core\Registry\removeAllByNamespace
‪removeAllByNamespace($namespace)
Definition: Registry.php:129
‪TYPO3\CMS\Core\Registry\$entries
‪array $entries
Definition: Registry.php:36
‪TYPO3\CMS\Core\Registry
Definition: Registry.php:33
‪TYPO3\CMS\Core\Registry\validateNamespace
‪validateNamespace($namespace)
Definition: Registry.php:182
‪TYPO3\CMS\Core
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Registry\$loadedNamespaces
‪array $loadedNamespaces
Definition: Registry.php:40
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Registry\isNamespaceLoaded
‪bool isNamespaceLoaded($namespace)
Definition: Registry.php:147
‪TYPO3\CMS\Core\Registry\loadEntriesByNamespace
‪loadEntriesByNamespace($namespace)
Definition: Registry.php:158
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:59