PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/windwalker/base/proxy.php

https://bitbucket.org/asikart-extension/joomclouds-client
PHP | 284 lines | 140 code | 33 blank | 111 comment | 15 complexity | dde654648a0265838a31971b968743f6 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Windwalker.Framework
  4. * @subpackage Base
  5. *
  6. * @copyright Copyright (C) 2012 Asikart. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. * @author Generated by AKHelper - http://asikart.com
  9. */
  10. // no direct access
  11. defined('_JEXEC') or die;
  12. /**
  13. * A base proxy class for AKHelper to call any sub helpers.
  14. *
  15. * @package Windwalker.Framework
  16. * @subpackage Base
  17. */
  18. class AKProxy
  19. {
  20. /**
  21. * An array to hold included paths
  22. *
  23. * @var array
  24. */
  25. protected static $includePaths = array();
  26. /**
  27. * An array to hold method references
  28. *
  29. * @var array
  30. */
  31. protected static $registry = array();
  32. /**
  33. * A default prefix when method not exists, call this class instead.
  34. *
  35. * @var string
  36. */
  37. protected static $prefix = 'AKHelper' ;
  38. /**
  39. * Method to extract a key
  40. *
  41. * @param string $key The name of helper method to load, (prefix).(class).function
  42. * prefix and class are optional and can be used to load custom html helpers.
  43. *
  44. * @return array Contains lowercase key, prefix, file, function.
  45. */
  46. protected static function extract($key)
  47. {
  48. $key = preg_replace('#[^A-Z0-9_\.]#i', '', $key);
  49. // Check to see whether we need to load a helper file
  50. $parts = explode('.', $key);
  51. $file = '' ;
  52. $prefix = '' ;
  53. if(count($parts) == 3) {
  54. $prefix = array_shift($parts) ;
  55. $file = array_shift($parts) ;
  56. }elseif(count($parts) == 2){
  57. $prefix = self::$prefix ;
  58. $file = array_shift($parts) ;
  59. }else{
  60. $prefix = self::$prefix ;
  61. }
  62. $func = array_shift($parts);
  63. return array(strtolower($key), $prefix, $file, $func);
  64. }
  65. /**
  66. * Class loader method
  67. *
  68. * Additional arguments may be supplied and are passed to the sub-class.
  69. * Additional include paths are also able to be specified for third-party use
  70. *
  71. * @param string $key The name of helper method to load, (prefix).(class).function
  72. * prefix and class are optional and can be used to load custom
  73. * html helpers.
  74. *
  75. * @return mixed self::call($function, $args) or False on error
  76. */
  77. public static function _($key)
  78. {
  79. list($key, $prefix, $file, $func) = self::extract($key);
  80. if (array_key_exists($key, self::$registry))
  81. {
  82. $function = self::$registry[$key];
  83. $args = func_get_args();
  84. // Remove function name from arguments
  85. array_shift($args);
  86. return self::call($function, $args);
  87. }
  88. $className = $prefix . ucfirst($file);
  89. if (!class_exists($className))
  90. {
  91. jimport('joomla.filesystem.path');
  92. if ($path = JPath::find(self::$includePaths[$prefix], strtolower($file) . '.php'))
  93. {
  94. require_once $path;
  95. if (!class_exists($className))
  96. {
  97. //JError::raiseError(500, JText::sprintf('JLIB_HTML_ERROR_NOTFOUNDINFILE', $className, $func));
  98. //return false;
  99. }
  100. }
  101. }
  102. $toCall = array($className, $func);
  103. if (is_callable($toCall))
  104. {
  105. self::register($key, $toCall);
  106. $args = func_get_args();
  107. // Remove function name from arguments
  108. array_shift($args);
  109. return self::call($toCall, $args);
  110. }
  111. elseif( $prefix != 'AKHelper' )
  112. {
  113. $args = func_get_args();
  114. $args[0] = 'AKHelper.' . $file . '.' . $func ;
  115. return call_user_func_array( array('AKHelper', '_') , $args) ;
  116. }
  117. else
  118. {
  119. JError::raiseWarning(500, JText::sprintf('JLIB_HTML_ERROR_NOTSUPPORTED', $className, $func));
  120. return false;
  121. }
  122. }
  123. /**
  124. * Registers a function to be called with a specific key
  125. *
  126. * @param string $key The name of the key
  127. * @param string $function Function or method
  128. *
  129. * @return boolean True if the function is callable
  130. */
  131. public static function register($key, $function)
  132. {
  133. list($key) = self::extract($key);
  134. if (is_callable($function))
  135. {
  136. self::$registry[$key] = $function;
  137. return true;
  138. }
  139. return false;
  140. }
  141. /**
  142. * Removes a key for a method from registry.
  143. *
  144. * @param string $key The name of the key
  145. *
  146. * @return boolean True if a set key is unset
  147. */
  148. public static function unregister($key)
  149. {
  150. list($key) = self::extract($key);
  151. if (isset(self::$registry[$key]))
  152. {
  153. unset(self::$registry[$key]);
  154. return true;
  155. }
  156. return false;
  157. }
  158. /**
  159. * Test if the key is registered.
  160. *
  161. * @param string $key The name of the key
  162. *
  163. * @return boolean True if the key is registered.
  164. */
  165. public static function isRegistered($key)
  166. {
  167. list($key) = self::extract($key);
  168. return isset(self::$registry[$key]);
  169. }
  170. /**
  171. * Function caller method
  172. *
  173. * @param string $function Function or method to call
  174. * @param array $args Arguments to be passed to function
  175. *
  176. * @return mixed Function result or false on error.
  177. *
  178. * @see http://php.net/manual/en/function.call-user-func-array.php
  179. */
  180. protected static function call($function, $args)
  181. {
  182. if (is_callable($function))
  183. {
  184. // PHP 5.3 workaround
  185. $temp = array();
  186. foreach ($args as &$arg)
  187. {
  188. $temp[] = &$arg;
  189. }
  190. return call_user_func_array($function, $temp);
  191. }
  192. else
  193. {
  194. JError::raiseError(500, JText::_('JLIB_HTML_ERROR_FUNCTION_NOT_SUPPORTED'));
  195. return false;
  196. }
  197. }
  198. /**
  199. * Add a directory where self should search for helpers. You may
  200. * either pass a string or an array of directories.
  201. *
  202. * @param string $path A path to search.
  203. *
  204. * @return array An array with directory elements
  205. */
  206. public static function addIncludePath($path = '', $prefix = null)
  207. {
  208. // Force path to array
  209. settype($path, 'array');
  210. $prefix = $prefix ? $prefix : self::getPrefix();
  211. if(!isset(self::$includePaths[$prefix])) {
  212. self::$includePaths[$prefix] = array();
  213. }
  214. // Loop through the path directories
  215. foreach ($path as $dir)
  216. {
  217. if (!empty($dir) && !in_array($dir, self::$includePaths[$prefix]))
  218. {
  219. jimport('joomla.filesystem.path');
  220. array_unshift(self::$includePaths[$prefix], JPath::clean($dir));
  221. }
  222. }
  223. return self::$includePaths[$prefix];
  224. }
  225. /**
  226. * Set current sub class prefix.
  227. *
  228. * @param string $prefix A helper name for component.
  229. */
  230. public static function setPrefix($prefix)
  231. {
  232. self::$prefix = $prefix ;
  233. self::$includePaths[$prefix] = array();
  234. }
  235. /**
  236. * Get current prefix.
  237. *
  238. * @return string Current helper name.
  239. */
  240. public static function getPrefix()
  241. {
  242. return self::$prefix ;
  243. }
  244. /**
  245. * A debug tool.
  246. */
  247. public static function get($var)
  248. {
  249. return self::$$var ;
  250. }
  251. }