Posted By

freelancephp on 03/30/10


Tagged

class php register Singleton callback registry static lazy


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

dantreacy


LRegistry - Lazy Registry Class


 / Published in: PHP
 

URL: http://www.freelancephp.net/php-lazy-registry-class/

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.

  1. <?php
  2. /**
  3.  * LRegistry Class
  4.  *
  5.  * Managing globals with the registry pattern.
  6.  * This class provides lazy loading functionallity. This means it is possible to
  7.  * add a callback or class (with arguments) which will be called or instanciated
  8.  * when it is being used for the first time.
  9.  *
  10.  * @version 1.1
  11.  * @author Victor Villaverde Laan
  12.  * @link http://www.freelancephp.net/php-lazy-registry-class/
  13.  * @license MIT license
  14.  */
  15. class LRegistry {
  16.  
  17. /**
  18. * Singleton pattern
  19. * @var Registry object
  20. */
  21. protected static $_instance = NULL;
  22.  
  23. /**
  24. * Containing all entries
  25. * @var array
  26. */
  27. protected $_entries = array();
  28.  
  29. /**
  30. * Default overwrite entries when being set more than once
  31. * @var boolean
  32. */
  33. protected $_default_overwrite = TRUE;
  34.  
  35. /**
  36. * Private contructor for singleton protection
  37. */
  38. private final function __construct() {}
  39.  
  40. /**
  41. * Private clone method for singleton protection
  42. */
  43. private final function __clone() {}
  44.  
  45. /**
  46. * Get instance of this class
  47. * @return Registry object
  48. */
  49. public static function get_instance() {
  50. if ( self::$_instance === NULL )
  51. self::$_instance = new LRegistry;
  52.  
  53. return self::$_instance;
  54. }
  55.  
  56. /**
  57. * Get value of given key
  58. * @param string $key
  59. * @param boolean $reload reload if class or callback was already loaded
  60. * @return mixed
  61. * @throw Exception
  62. */
  63. public static function get( $key, $reload = FALSE ) {
  64. $instance = self::get_instance();
  65.  
  66. // check if entry exists
  67. if ( ! key_exists( $key, $instance->_entries ) )
  68. throw new Exception( get_class( self::get_instance() ) . ': Key "' . $key . '" could not be found.' );
  69.  
  70. // get current entry
  71. $entry = $instance->_entries[$key];
  72.  
  73. // check if entry should be loaded or reloaded
  74. if ( ! $entry['loaded'] OR $reload ) {
  75. if ( $entry['type'] == 'class' ) {
  76. // get reflection class
  77. $ref_class = new ReflectionClass( $entry['class'] );
  78. // create instance
  79. $entry['value'] = $ref_class->newInstanceArgs( $entry['args'] );
  80. } else if ( $entry['type'] == 'callback' ) {
  81. // run callback and set return value
  82. $entry['value'] = call_user_func_array( $entry['callback'], $entry['args'] );
  83. } else {
  84. throw new Exception( get_class( self::get_instance() ) . ': Type "' . $type . '" is not supported.' );
  85. }
  86.  
  87. // set (new) value and change "loaded" setting
  88. $instance->_entries[$key]['value'] = $entry['value'];
  89. $instance->_entries[$key]['loaded'] = TRUE;
  90. }
  91.  
  92. return $entry['value'];
  93. }
  94.  
  95. /**
  96. * Set value of given key
  97. * @param string $key
  98. * @param mixed $value
  99. * @param boolean $overwrite optional, when set will ignore the default overwrite setting
  100. * @return boolean value was set succesfully
  101. */
  102. public static function set( $key, $value, $overwrite = NULL ) {
  103. // set normal value settings
  104. $settings = array( 'type' => 'value',
  105. 'loaded' => TRUE,
  106. 'value' => $value );
  107.  
  108. return self::get_instance()->_set( $key, $settings, $overwrite );
  109. }
  110.  
  111. /**
  112. * Set lazy entry of a class. Instance will only be created when entry
  113. * is requested using the get method.
  114. * @param string $key
  115. * @param string $class
  116. * @param array $args
  117. * @param boolean $overwrite optional, when set will ignore the default overwrite setting
  118. * @return boolean value was set succesfully
  119. */
  120. public static function set_lazy_class( $key, $class, $args = array(), $overwrite = NULL ) {
  121. // set lazy class settings
  122. $settings = array( 'type' => 'class',
  123. 'class' => $class,
  124. 'args' => $args,
  125. 'loaded' => FALSE,
  126. 'value' => NULL );
  127.  
  128. return self::get_instance()->_set( $key, $settings, $overwrite );
  129. }
  130.  
  131. /**
  132. * Set lazy entry of a callback function.Function will only be called when entry
  133. * is requested using the get method.
  134. * @param string $key
  135. * @param array $callback
  136. * @param array $args
  137. * @param boolean $overwrite optional, when set will ignore the default overwrite setting
  138. * @return boolean value was set succesfully
  139. */
  140. public static function set_lazy_callback( $key, $callback, $args = array(), $overwrite = NULL ) {
  141. // set lazy callback settings
  142. $settings = array( 'type' => 'callback',
  143. 'callback' => $callback,
  144. 'args' => $args,
  145. 'loaded' => FALSE,
  146. 'value' => NULL );
  147.  
  148. return self::get_instance()->_set( $key, $settings, $overwrite );
  149. }
  150.  
  151. /**
  152. * Check if given entry exists
  153. * @param string $key
  154. * @return boolean
  155. */
  156. public static function has( $key ) {
  157. return key_exists( $key, self::get_instance()->_entries );
  158. }
  159.  
  160. /**
  161. * Check if given (lazy) entry is already loaded
  162. * @param string $key
  163. * @return boolean
  164. */
  165. public static function is_loaded( $key ) {
  166. return ( self::has( $key ) AND self::get_instance()->_entries[$key]['loaded'] === TRUE );
  167. }
  168.  
  169. /**
  170. * Remove the given entry
  171. * @param string $key
  172. * @throw Exception
  173. */
  174. public static function remove( $key ) {
  175. if ( ! self::has( $key ) )
  176. throw new Exception( get_class( self::get_instance() ) . ': Key "' . $key . '"does not exist.' );
  177.  
  178. unset( self::get_instance()->_entries[$key] );
  179. }
  180.  
  181. /**
  182. * Set default overwriting entries when being set more than once.
  183. * @param boolean|NULL $overwrite optional, if empty can be used as a getter
  184. * @return boolean
  185. */
  186. public static function default_overwrite( $overwrite = NULL ) {
  187. if ( $overwrite !== NULL )
  188. self::get_instance()->_default_overwrite = (bool) $overwrite;
  189.  
  190. return self::get_instance()->_default_overwrite;
  191. }
  192.  
  193. /**
  194. * Set value of given key (private use)
  195. * @param string $key
  196. * @param mixed $settings
  197. * @param boolean $overwrite optional, when set will ignore the default overwrite setting
  198. * @return boolean value was set succesfully
  199. */
  200. protected function _set( $key, $settings, $overwrite = NULL ) {
  201. $instance = self::get_instance();
  202.  
  203. // check if overwriting is allowed
  204. $overwrite = ( $overwrite === NULL )
  205. ? $this->_default_overwrite
  206. : $overwrite;
  207.  
  208. // check if entry exists and overwriting is allowed
  209. if ( key_exists( $key, $this->_entries ) AND ! $overwrite )
  210. return FALSE;
  211.  
  212. $this->_entries[$key] = $settings;
  213.  
  214. return TRUE;
  215. }
  216.  
  217. }
  218.  
  219. /*?> // ommit closing tag, to prevent unwanted whitespace at the end of the parts generated by the included files */

Report this snippet  

You need to login to post a comment.