Limèu Registry€“PHP Lazy Registry类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Limèu Registry€“PHP Lazy Registry类相关的知识,希望对你有一定的参考价值。
Managing globals with the registry pattern. This class provides lazy loading functionallity. This means it is possible to add a callback or class (with arguments) which will be called or instanciated when it is being used for the first time.
<?php /** * Lim_Registry Class * * Registry for managing global vars and references with support for lazy loading. * * @package Registry * @version 2.0.0 * @author Victor Villaverde Laan * @link http://www.freelancephp.net/lim_registry-php-lazy-registry-class/ * @license MIT license */ class Lim_Registry { /** * Default overwrite entries when being set more than once * @var boolean */ protected $_defaultOverwrite = true; /** * Singleton pattern * @var array */ /** * Containing all entries * @var array */ /** * Protected contructor for singleton protection */ protected final function __construct() { } /** * Private clone method for singleton protection */ private final function __clone() { } /** * Get instance of (called) class * @return Lim_Registry */ public static function getInstance() { $class = self::_getClass(); self::$_instances[$class] = new $class; return self::$_instances[$class]; } /** * Set default overwriting entries when being set more than once. * @param boolean $overwrite Optional, else used as a getter * @return boolean */ public static function defaultOverwrite($overwrite = null) { if ($overwrite !== null) self::getInstance()->_defaultOverwrite = (bool) $overwrite; return self::getInstance()->_defaultOverwrite; } /** * Set value of given key * @param string $key * @param mixed $value * @param boolean $overwrite Optional, when set will ignore the default overwrite setting * @return boolean Value was set succesfully */ public static function set($key, $value, $overwrite = null) { return self::getInstance()->_setEntry($key, $settings, $overwrite); } /** * Set value by reference (for arrays) * @param string $key * @param mixed $value * @return boolean Value was set succesfully */ public static function setByRef($key, & $value) { return self::getInstance()->_setEntry($key, $settings, true); } /** * Set lazy entry of a class. Instance will only be created when entry is requested using the get method. * @param string $key * @param string $class * @param array $args * @param boolean $overwrite Optional, when set will ignore the default overwrite setting * @return boolean Value was set succesfully */ $settings = array('type' => 'class', 'class' => $class, 'args' => $args, 'loaded' => false, 'value' => null); return self::getInstance()->_setEntry($key, $settings, $overwrite); } /** * Set lazy entry of a callback function. Function will only be called when entry is requested using the get method. * @param string $key * @param array $callback * @param array $args * @param boolean $overwrite Optional, when set will ignore the default overwrite setting * @return boolean Value was set succesfully */ $settings = array('type' => 'callback', 'callback' => $callback, 'args' => $args, 'loaded' => false, 'value' => null); return self::getInstance()->_setEntry($key, $settings, $overwrite); } /** * Get value of given key * @param string $key * @param boolean $reload Reload if class or callback was already loaded * @return mixed * @throw Exception */ public static function get($key, $reload = false) { $instance = self::getInstance(); // check if entry exists self::_throwException('Key "' . $key . '" could not be found.'); // get current entry $entry = $instance->_entries[$key]; // check if entry should be loaded or reloaded if (!$entry['loaded'] || $reload) { if ($entry['type'] == 'class') { // get reflection class $refClass = new ReflectionClass($entry['class']); // create instance $entry['value'] = $refClass->newInstanceArgs(); } else { $entry['value'] = $refClass->newInstanceArgs($entry['args']); } } else if ($entry['type'] == 'callback') { // run callback and set return value } else { self::_throwException('Type "' . $type . '" is not supported.'); } // set (new) value and change "loaded" setting $instance->_entries[$key]['value'] = $entry['value']; $instance->_entries[$key]['loaded'] = true; } return $entry['value']; } /** * Get value by reference (for arrays) * @param string $key * @return mixed By reference */ public static function & getByRef($key) { $instance = self::getInstance(); // check if entry exists self::_throwException('Key "' . $key . '" could not be found.'); // get current entry $entry = $instance->_entries[$key]; return $entry['value']; } /** * Check if given entry exists * @param string $key * @return boolean */ public static function has($key) { } /** * Check if given (lazy) entry is already loaded * @param string $key * @return boolean */ public static function isLoaded($key) { return (self::has($key) && self::getInstance()->_entries[$key]['loaded'] === true); } /** * Remove the given entry * @param string $key * @throw Exception */ public static function remove($key) { if (!self::has($key)) self::_throwException('Key "' . $key . '" does not exist.'); } /** * Remove all entries */ public static function clear() { foreach ($keys as $key) self::remove($key); } /** * Set value of given key * @param string $key * @param mixed $settings * @param boolean $overwrite Optional, when set will ignore the default overwrite setting * @return boolean Value was set succesfully */ protected function _setEntry($key, $settings, $overwrite = null) { // check if overwriting is allowed $overwrite = ($overwrite === null) ? $this->_defaultOverwrite : $overwrite; // check if entry exists and overwriting is allowed return false; $this->_entries[$key] = $settings; return true; } /** * Throw exception * @param string $msg * @throw Exception */ protected static function _throwException($msg) { throw new Exception(self::_getClass() . ' - ' . $msg); } /** * get_called_class() equivalent also support for PHP < 5.3. * @return string */ protected static function _getClass() { return get_called_class(); $class = __CLASS__; try { $l = 0; do { $l++; $callerLine = $lines[$bt[$l]['line']-1]; } while ($matches[1] === 'parent' && $matches[1]); $class = $matches[1]; } catch (Exception $e) { } return $class; } } // Lim_Registry
以上是关于Limèu Registry€“PHP Lazy Registry类的主要内容,如果未能解决你的问题,请参考以下文章
Datoteka zaglavlja-listaèu polje.h.酒店