|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2009-2011 Ingo Renner <ingo@typo3.org> 00006 * 00007 * All rights reserved 00008 * 00009 * This script is part of the TYPO3 project. The TYPO3 project is 00010 * free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * The GNU General Public License can be found at 00016 * http://www.gnu.org/copyleft/gpl.html. 00017 * A copy is found in the textfile GPL.txt and important notices to the license 00018 * from the author is found in LICENSE.txt distributed with these scripts. 00019 * 00020 * 00021 * This script is distributed in the hope that it will be useful, 00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00024 * GNU General Public License for more details. 00025 * 00026 * This copyright notice MUST APPEAR in all copies of the script! 00027 ***************************************************************/ 00028 00029 00030 /** 00031 * A class to store and retrieve entries in a registry database table. 00032 * 00033 * The intention is to have a place where we can store things (mainly settings) 00034 * that should live for more than one request, longer than a session, and that 00035 * shouldn't expire like it would with a cache. You can actually think of it 00036 * being like the Windows Registry in some ways. 00037 * 00038 * Credits: Heavily inspired by Drupal's variable_*() functions. 00039 * 00040 * @author Ingo Renner <ingo@typo3.org> 00041 * @author Bastian Waidelich <bastian@typo3.org> 00042 * @package TYPO3 00043 * @subpackage t3lib 00044 */ 00045 class t3lib_Registry implements t3lib_Singleton { 00046 00047 /** 00048 * @var array 00049 */ 00050 protected $entries = array(); 00051 00052 00053 /** 00054 * Returns a persistent entry. 00055 * 00056 * @param string Extension key for extensions starting with 'tx_' / 'user_' or 'core' for core registry entries 00057 * @param string The key of the entry to return. 00058 * @param mixed Optional default value to use if this entry has never been set. Defaults to null. 00059 * @return mixed The value of the entry. 00060 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid 00061 */ 00062 public function get($namespace, $key, $defaultValue = null) { 00063 if (!isset($this->entries[$namespace])) { 00064 $this->loadEntriesByNamespace($namespace); 00065 } 00066 00067 return isset($this->entries[$namespace][$key]) ? $this->entries[$namespace][$key] : $defaultValue; 00068 } 00069 00070 /** 00071 * Sets a persistent entry. 00072 * Do not store binary data into the registry, it's not build to do that, 00073 * instead use the proper way to store binary data: The filesystem. 00074 * 00075 * @param string Extension key for extensions starting with 'tx_' / 'user_' or 'core' for core registry entries. 00076 * @param string The key of the entry to set. 00077 * @param mixed The value to set. This can be any PHP data type; this class takes care of serialization if necessary. 00078 * @return void 00079 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid 00080 */ 00081 public function set($namespace, $key, $value) { 00082 $this->validateNamespace($namespace); 00083 $serializedValue = serialize($value); 00084 00085 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery( 00086 'uid', 00087 'sys_registry', 00088 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry') 00089 . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry') 00090 ); 00091 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) < 1) { 00092 $GLOBALS['TYPO3_DB']->exec_INSERTquery( 00093 'sys_registry', 00094 array( 00095 'entry_namespace' => $namespace, 00096 'entry_key' => $key, 00097 'entry_value' => $serializedValue 00098 ) 00099 ); 00100 } else { 00101 $GLOBALS['TYPO3_DB']->exec_UPDATEquery( 00102 'sys_registry', 00103 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry') 00104 . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry'), 00105 array( 00106 'entry_value' => $serializedValue 00107 ) 00108 ); 00109 } 00110 00111 $this->entries[$namespace][$key] = $value; 00112 } 00113 00114 /** 00115 * Unsets a persistent entry. 00116 * 00117 * @param string Namespace. extension key for extensions or 'core' for core registry entries 00118 * @param string The key of the entry to unset. 00119 * @return void 00120 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid 00121 */ 00122 public function remove($namespace, $key) { 00123 $this->validateNamespace($namespace); 00124 00125 $GLOBALS['TYPO3_DB']->exec_DELETEquery( 00126 'sys_registry', 00127 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry') 00128 . ' AND entry_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'sys_registry') 00129 ); 00130 00131 unset($this->entries[$namespace][$key]); 00132 } 00133 00134 /** 00135 * Unsets all persistent entries of the given namespace. 00136 * 00137 * @param string Namespace. extension key for extensions or 'core' for core registry entries 00138 * @return void 00139 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid 00140 */ 00141 public function removeAllByNamespace($namespace) { 00142 $this->validateNamespace($namespace); 00143 00144 $GLOBALS['TYPO3_DB']->exec_DELETEquery( 00145 'sys_registry', 00146 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry') 00147 ); 00148 00149 unset($this->entries[$namespace]); 00150 } 00151 00152 /** 00153 * Loads all entries of the given namespace into the internal $entries cache. 00154 * 00155 * @param string Namespace. extension key for extensions or 'core' for core registry entries 00156 * @return void 00157 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid 00158 */ 00159 protected function loadEntriesByNamespace($namespace) { 00160 $this->validateNamespace($namespace); 00161 00162 $storedEntries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( 00163 '*', 00164 'sys_registry', 00165 'entry_namespace = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($namespace, 'sys_registry') 00166 ); 00167 00168 foreach ($storedEntries as $storedEntry) { 00169 $key = $storedEntry['entry_key']; 00170 $this->entries[$namespace][$key] = unserialize($storedEntry['entry_value']); 00171 } 00172 } 00173 00174 /** 00175 * Checks the given namespace. If it does not have a valid format an 00176 * exception is thrown. 00177 * Allowed namespaces are 'core', 'tx_*' and 'user_*' 00178 * 00179 * @param string Namespace. extension key for extensions or 'core' for core registry entries 00180 * @return void 00181 * @throws InvalidArgumentException Throws an exception if the given namespace is not valid 00182 */ 00183 protected function validateNamespace($namespace) { 00184 if (t3lib_div::isFirstPartOfStr($namespace, 'tx_') || t3lib_div::isFirstPartOfStr($namespace, 'user_')) { 00185 return; 00186 } 00187 00188 if ($namespace !== 'core') { 00189 throw new InvalidArgumentException( 00190 '"' . $namespace . '" is no valid Namespace. The namespace has to be prefixed with "tx_", "user_" or must be equal to "core"', 00191 1249755131 00192 ); 00193 } 00194 } 00195 00196 } 00197 00198 00199 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_registry.php'])) { 00200 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_registry.php']); 00201 } 00202 00203 ?>
1.8.0