/system/registry.class.php

https://github.com/amereservant/Chevereto-Expanded · PHP · 265 lines · 110 code · 18 blank · 137 comment · 11 complexity · 6a4bb85249b41e79298118714d9db432 MD5 · raw file

  1. <?php
  2. /**
  3. * Registry Class
  4. *
  5. * Holds all of the global variables so they can be used throughout the script
  6. * without having to declare a bunch of variables global.
  7. * It also centralizes all of the global variables and can easily be dumped for
  8. * debugging/development.
  9. *
  10. * PHP5
  11. *
  12. * @package Chevereto
  13. * @author David Miles <david@amereservant.com>
  14. * @version 2.0
  15. * @since 2.0
  16. * @license http://creativecommons.org/licenses/MIT/ MIT License
  17. */
  18. class Registry
  19. {
  20. /**
  21. * Holds all of the variables for callback.
  22. *
  23. * @staticvar array
  24. * @access private
  25. * @since 2.0
  26. */
  27. private static $_vars;
  28. /**
  29. * Set User Variable
  30. *
  31. * This should be used by plug-ins and themes ONLY when non-local variables
  32. * are needed. That way variable name collisions are avoided.
  33. *
  34. * The $plugin_name parameter should be unique and it's highly recommended to use
  35. * the plug-in/theme's name as this value.
  36. *
  37. * @param string $plugin_name The plug-in or theme's name, used to group variables
  38. * @param string $key The name of the key the variable will be referenced by.
  39. * @param mixed $value The value for the variable.
  40. * @param bool $overridable Determines whether or not the variable can be
  41. * overridden elsehwere. If set to false, the
  42. * value can only be set once.
  43. * @return bool True on success, False on fail
  44. * @access public
  45. * @static
  46. * @since 2.0
  47. */
  48. public static function set_var( $plugin_name, $key, $value, $overridable=true )
  49. {
  50. list( $plugin_name, $key, $value, $overridable ) = execute_hook( 'set_var', $plugin_name, $key, $value, $overridable );
  51. if( isset(self::$_vars['user'][$plugin_name][$key]['value']) &&
  52. self::$_vars['user'][$plugin_name][$key]['override'] == false )
  53. {
  54. add_error( 'plugin', 'The value `'. $key .'` has already been set and isn\'t overridable!' );
  55. return false;
  56. }
  57. self::$_vars['user'][$plugin_name][$key]['value'] = $value;
  58. self::$_vars['user'][$plugin_name][$key]['override'] = $overridable;
  59. return true;
  60. }
  61. /**
  62. * Get User Variable
  63. *
  64. * Used to retrieve the user variable.
  65. *
  66. * @param string $plugin_name The plug-in or theme's name to get value from
  67. * @param string $key The name of the key for the variable being retrieved
  68. * @return mixed The data on success, false if it isn't set.
  69. * @access public
  70. * @static
  71. * @since 2.0
  72. */
  73. public static function get_var( $plugin_name, $key )
  74. {
  75. if( !isset(self::$_vars['user'][$plugin_name][$key]) )
  76. {
  77. add_error( 'debug', 'The value for plugin name `'. $plugin_name .'` and key `'.
  78. $key .'` is not set!');
  79. return false;
  80. }
  81. return self::$_vars['user'][$plugin_name][$key]['value'];
  82. }
  83. /**
  84. * Get System Variable
  85. *
  86. * This is used to access system variables from the system array that have been
  87. * set by {@link get_system_var()} method.
  88. *
  89. * It also adds a 'debug' error message if a system variable isn't set when
  90. * being called.
  91. * Also note care should be used if retrieving a bool value since !isset() will
  92. * also return false.
  93. *
  94. * @param string $section The section read from. Example: 'hooks', 'actions', 'query', etc.
  95. * @param string $item The item to get. Example: 'css'
  96. * @param bool Is this function being used to determine if the system
  97. * variable is being called to see if the item isset?
  98. * @return mixed Depends on the data set to the variable being requested.
  99. * @access public
  100. * @static
  101. * @since 2.0
  102. */
  103. public static function get_system_var( $section, $item, $isset=false )
  104. {
  105. if( !isset( self::$_vars['system'][$section][$item] ) )
  106. {
  107. if( !$isset )
  108. {
  109. add_error( 'debug', 'The system variable `'. $item .'` in section `'. $section .
  110. '` is not set!' );
  111. }
  112. return false;
  113. }
  114. elseif( $isset ) return true;
  115. return self::$_vars['system'][$section][$item];
  116. }
  117. /**
  118. * Set System Variable
  119. *
  120. * This should NOT be accessed by any plug-ins or user functions!
  121. * This method provides a way for core classes to add variables to the system
  122. * array so the values can be used in other areas.
  123. *
  124. * It also adds a 'debug' error message if a system variable is being replaced
  125. * in order to help locate issues associated with this.
  126. *
  127. * @param string $section The section to add to. Example: 'hooks', 'actions', 'query', etc.
  128. * @param string $item The item to add to. An example would be adding
  129. * the css array to the plugin section. 'css' would be the item.
  130. * @param mixed $val The value to assign the item.
  131. * @return void
  132. * @access public
  133. * @static
  134. * @since 2.0
  135. */
  136. public static function set_system_var( $section, $item, $val )
  137. {
  138. list( $section, $item, $val ) = execute_hook( 'set_system_var', $section, $item, $val );
  139. if( isset( self::$_vars['system'][$section][$item] ) )
  140. {
  141. // Hooks will be set after being added, so no error is neccessary here.
  142. if( $section != 'hooks' )
  143. {
  144. add_error( 'debug', 'The system variable `'. $section .'` => `'. $item .
  145. '` is already set!' );
  146. }
  147. }
  148. self::$_vars['system'][$section][$item] = $val;
  149. }
  150. /**
  151. * Unset System Variable
  152. *
  153. * Used to remove a system variable.
  154. *
  155. * @param string $section The system variable section to remove a variable from
  156. * @param string $item The item we're unsetting
  157. * @return bool False if variable isn't set, true if it was sucessfully removed
  158. * @access public
  159. * @static
  160. * @since 2.0
  161. */
  162. public static function remove_system_var( $section, $item )
  163. {
  164. if( !isset( self::$_vars['system'][$section][$item] ) )
  165. {
  166. add_error( 'debug', 'The system variable `'. $section .'` => `'. $item .
  167. '` is not set! Removal failed!' );
  168. return false;
  169. }
  170. unset( self::$_vars['system'][$section][$item] );
  171. return true;
  172. }
  173. /**
  174. * Unset User Variable
  175. *
  176. * Used to remove a user variable.
  177. *
  178. * @param string $section The user variable section to remove a variable from
  179. * @param string $item The item we're unsetting
  180. * @return bool False if variable isn't set, true if it was sucessfully removed
  181. * @access public
  182. * @static
  183. * @since 2.0
  184. */
  185. public static function remove_var( $section, $item )
  186. {
  187. if( !isset( self::$_vars['user'][$section][$item] ) )
  188. {
  189. add_error( 'debug', 'The user variable `'. $section .'` => `'. $item .
  190. '` is not set! Removal failed!' );
  191. return false;
  192. }
  193. unset( self::$_vars['user'][$section][$item] );
  194. return true;
  195. }
  196. /**
  197. * Dump All Registry Variables
  198. *
  199. * Used ONLY for development/debugging. It will display all set variables at
  200. * the time it is called.
  201. *
  202. * @param void
  203. * @return void prints out a <pre></pre> formatted array.
  204. * @access public
  205. * @static
  206. * @since 2.0
  207. */
  208. public static function dump_vars()
  209. {
  210. echo 'Registry Vars:';
  211. echo '<pre>'. print_r(self::$_vars, true) .'</pre>';
  212. }
  213. }
  214. /**
  215. * ///// Procedural Functions /////
  216. *
  217. * These should be used instead of directly accessing the class methods!
  218. *
  219. */
  220. function set_var( $plugin_name, $key, $value, $overridable=true )
  221. {
  222. return Registry::set_var( $plugin_name, $key, $value, $overridable=true );
  223. }
  224. function get_var( $plugin_name, $key )
  225. {
  226. return Registry::get_var( $plugin_name, $key );
  227. }
  228. function set_system_var( $section, $item, $val )
  229. {
  230. return Registry::set_system_var( $section, $item, $val );
  231. }
  232. function get_system_var( $section, $item )
  233. {
  234. return Registry::get_system_var( $section, $item );
  235. }
  236. function system_var_isset( $section, $item )
  237. {
  238. return Registry::get_system_var( $section, $item, true );
  239. }
  240. function remove_var( $section, $item )
  241. {
  242. return Registry::remove_var( $section, $item );
  243. }
  244. function remove_system_var( $section, $item )
  245. {
  246. return Registry::remove_var( $section, $item );
  247. }