PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/FSN/library/Yab/Loader.php

https://gitlab.com/r.collas/site_central
PHP | 548 lines | 300 code | 224 blank | 24 comment | 54 complexity | 7e8f3f0db5069a7c2dd8941781dd5347 MD5 | raw file
  1. <?php
  2. /**
  3. * Yab Framework
  4. *
  5. * @category Yab
  6. * @package Yab_Loader
  7. * @author Yann BELLUZZI
  8. * @copyright (c) 2010 YBellu
  9. * @license http://www.ybellu.com/yab-framework/license.html
  10. * @link http://www.ybellu.com/yab-framework
  11. */
  12. require_once 'Exception.php';
  13. class Yab_Loader {
  14. private $_start = 0;
  15. static private $_instances = array();
  16. final private function __construct() {
  17. $this->_start = $this->getTime();
  18. spl_autoload_register(array($this, 'loadClass'));
  19. $this->addPath($this->getPath());
  20. }
  21. final public function configure($file = null, $environment = null) {
  22. $config = $this->getConfig();
  23. if($environment !== null)
  24. $config->setEnvironment($environment);
  25. if($file !== null)
  26. $config->setFile($file);
  27. $config->apply();
  28. return $this;
  29. }
  30. final static public function getInstance($class = __CLASS__, array $args = array(), $parent_class = null) {
  31. if(!array_key_exists($class, self::$_instances))
  32. self::$_instances[$class] = $class == __CLASS__ ? new self() : self::getInstance()->invoke($class, $args, $parent_class);
  33. return self::$_instances[$class];
  34. }
  35. final public function getPath() {
  36. return dirname(dirname(__FILE__));
  37. }
  38. final public function addPath($path) {
  39. if(!is_dir($path) || !is_readable($path))
  40. throw new Yab_Exception($path.' is not a valid readable directory path');
  41. set_include_path($path.PATH_SEPARATOR.get_include_path());
  42. return $this;
  43. }
  44. final public function isFile($filepath, $readable = true, $writable = false) {
  45. $realpath = null;
  46. if(is_file($filepath))
  47. $realpath = $filepath;
  48. if(!$realpath) {
  49. $pathes = explode(PATH_SEPARATOR, get_include_path());
  50. foreach($pathes as $path) {
  51. if(!is_file($path.DIRECTORY_SEPARATOR.$filepath))
  52. continue;
  53. $realpath = $path.DIRECTORY_SEPARATOR.$filepath;
  54. break;
  55. }
  56. }
  57. if(!$realpath)
  58. return false;
  59. if($readable && !is_readable($realpath))
  60. return false;
  61. if($writable && !is_writable($realpath))
  62. return false;
  63. return true;
  64. }
  65. final public function isDir($dirpath, $readable = true, $writable = false) {
  66. $realpath = null;
  67. if(is_dir($dirpath))
  68. $realpath = $dirpath;
  69. if(!$realpath) {
  70. $pathes = explode(PATH_SEPARATOR, get_include_path());
  71. foreach($pathes as $path) {
  72. if(!is_file($path.DIRECTORY_SEPARATOR.$dirpath))
  73. continue;
  74. $realpath = $path.DIRECTORY_SEPARATOR.$dirpath;
  75. break;
  76. }
  77. }
  78. if(!$realpath)
  79. return false;
  80. if($readable && !is_readable($realpath))
  81. return false;
  82. if($writable && !is_writable($realpath))
  83. return false;
  84. return true;
  85. }
  86. final public function loadClass($class_name) {
  87. if(class_exists($class_name, false))
  88. return $this;
  89. $class_path = $this->classToPath($class_name);
  90. if(!$this->isFile($class_path))
  91. throw new Yab_Exception($class_path.' was not found');
  92. require_once $class_path;
  93. if(!class_exists($class_name, false))
  94. throw new Yab_Exception($class_path.' does not define the class '.$class_name);
  95. return $this;
  96. }
  97. final public function classToPath($class_name) {
  98. return str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php';
  99. }
  100. final public function invoke($instance_or_class, $method_or_args, $args_or_parent_class = null) {
  101. $array_filter = new Yab_Filter_Array();
  102. # Invoke an object method
  103. if(is_object($instance_or_class)) {
  104. $instance = $instance_or_class;
  105. $method = (string) $method_or_args;
  106. $class = get_class($instance_or_class);
  107. $args = $array_filter->filter($args_or_parent_class);
  108. if(!method_exists($class, $method))
  109. throw new Yab_Exception('"'.$method.'" does not exists on '.$class);
  110. $reflection_method = new ReflectionMethod($class, $method);
  111. if(!$reflection_method->isPublic())
  112. throw new Yab_Exception('"'.$class.'->'.$method.'()" is not public and can not be called');
  113. if(count($args) < $reflection_method->getNumberOfRequiredParameters())
  114. throw new Yab_Exception($class.'->'.$method.'() needs '.$reflection_method->getNumberOfRequiredParameters().' parameters, '.count($args).' given');
  115. switch(count($args)) {
  116. case 0 : return $instance->$method();
  117. case 1 : return $instance->$method(array_shift($args));
  118. case 2 : return $instance->$method(array_shift($args), array_shift($args));
  119. case 3 : return $instance->$method(array_shift($args), array_shift($args), array_shift($args));
  120. case 4 : return $instance->$method(array_shift($args), array_shift($args), array_shift($args), array_shift($args));
  121. case 5 : return $instance->$method(array_shift($args), array_shift($args), array_shift($args), array_shift($args), array_shift($args));
  122. case 6 : return $instance->$method(array_shift($args), array_shift($args), array_shift($args), array_shift($args), array_shift($args), array_shift($args));
  123. }
  124. return call_user_func_array(array($instance, $method), $args);
  125. }
  126. # Invoke an object
  127. $class = (string) $instance_or_class;
  128. $args = $array_filter->filter($method_or_args);
  129. $parent_class = (string) $args_or_parent_class;
  130. $this->loadClass($class);
  131. if($parent_class)
  132. $this->loadClass($parent_class);
  133. $instance = null;
  134. switch(count($args)) {
  135. case 0 : $instance = new $class(); break;
  136. case 1 : $instance = new $class(array_shift($args)); break;
  137. case 2 : $instance = new $class(array_shift($args), array_shift($args)); break;
  138. case 3 : $instance = new $class(array_shift($args), array_shift($args), array_shift($args)); break;
  139. case 4 : $instance = new $class(array_shift($args), array_shift($args), array_shift($args), array_shift($args)); break;
  140. case 5 : $instance = new $class(array_shift($args), array_shift($args), array_shift($args), array_shift($args), array_shift($args)); break;
  141. case 6 : $instance = new $class(array_shift($args), array_shift($args), array_shift($args), array_shift($args), array_shift($args), array_shift($args)); break;
  142. }
  143. if(!$instance)
  144. throw new Yab_Exception('"invoke" with "'.count($args).'" args is not implemented');
  145. if($parent_class && !($instance instanceof $parent_class))
  146. throw new Yab_Exception('"'.$class.'" does not extends "'.$parent_class.'"');
  147. return $instance;
  148. }
  149. final public function getTime() {
  150. list($tps_usec, $tps_sec) = explode(" ", microtime());
  151. return ((float) $tps_usec + (float) $tps_sec);
  152. }
  153. final public function getMemoryUsage() {
  154. return function_exists('memory_get_usage') ? memory_get_usage(true) : 0;
  155. }
  156. final public function getMemoryPeakUsage() {
  157. return function_exists('memory_get_peak_usage') ? memory_get_peak_usage(true) : 0;
  158. }
  159. final public function getScriptLength($decimals = 6) {
  160. return number_format($this->getTime() - $this->_start, $decimals);
  161. }
  162. final public function bench($label = null) {
  163. $is_cli = $this->getRequest()->isCli();
  164. $space = $is_cli ? ' ' : '&nbsp;';
  165. $crlf = $is_cli ? PHP_EOL : '<br />';
  166. if($label === null) {
  167. $bt = debug_backtrace();
  168. $last_bt = array_shift($bt);
  169. if($last_bt['file'] == __FILE__)
  170. $last_bt = array_shift($bt);
  171. $label = '['.$last_bt['file'].':L'.$last_bt['line'].']';
  172. } else {
  173. $label = '['.$label.']';
  174. }
  175. $benchmark = $label.$space.'Length:'.$space.$this->getScriptLength(3).$space.'sec,'.$space.'Memory:'.$space.number_format($this->getMemoryUsage() / 1024 / 1024, 2).$space.'Mo,'.$space.'Peak:'.$space.number_format($this->getMemoryPeakUsage() / 1024 / 1024, 2).$space.'Mo'.$crlf;
  176. return $benchmark;
  177. }
  178. final public function dump($var, $depth = 0, SplObjectStorage $recursion = null) {
  179. if($this->getRequest()->isCli() || PHP_VERSION < '5.3')
  180. return print_r($var, true);
  181. $dump_type = 'color: #007700';
  182. $dump_value_attribute = 'color: #0000dd';
  183. $dump_value_string = 'color: #0000dd';
  184. $dump_value_numeric = 'color: #dd0000';
  185. $dump_value_boolean = 'color: #000000';
  186. $dump_operator = 'color: #007700';
  187. $dump_accessibility = 'color: #666666';
  188. $dump = '';
  189. $type = strtolower(gettype($var));
  190. switch($type) {
  191. case 'null' :
  192. $dump .= '<span style="'.$dump_value_boolean.'">NULL</span>';
  193. break;
  194. case 'resource' :
  195. $dump .= '<span style="'.$dump_type.'">resource</span> <span style="'.$dump_value_string.'">'.$var.'</span>';
  196. break;
  197. case 'string' :
  198. $dump .= '<span style="'.$dump_type.'">string(</span><span style="'.$dump_value_numeric.'">'.strlen($var).'</span><span style="'.$dump_type.'">, &quot;</span><span style="'.$dump_value_string.'">'.$var.'</span><span style="'.$dump_type.'">&quot;)</span>';
  199. break;
  200. case 'int' :
  201. case 'integer' :
  202. $dump .= '<span style="'.$dump_type.'">int(</span><span style="'.$dump_value_numeric.'">'.$var.'</span><span style="'.$dump_type.'">)</span>';
  203. break;
  204. case 'float' :
  205. case 'double' :
  206. $dump .= '<span style="'.$dump_type.'">float(</span><span style="'.$dump_value_numeric.'">'.$var.'</span><span style="'.$dump_type.'">)</span>';
  207. break;
  208. case 'bool' :
  209. case 'boolean' :
  210. $dump .= '<span style="'.$dump_type.'">bool(</span><span style="'.$dump_value_boolean.'">'.($var ? 'TRUE' : 'FALSE').'</span><span style="'.$dump_type.'">)</span>';
  211. break;
  212. case 'array' :
  213. $dump .= '<span style="'.$dump_type.'">array(</span><span style="'.$dump_value_numeric.'">'.count($var).'</span><span style="'.$dump_type.'">)</span>';
  214. foreach($var as $key => $value)
  215. $dump .= PHP_EOL.str_repeat("\t", $depth + 1).$this->dump($key, $depth + 1).' <span style="'.$dump_operator.'">=&gt;</span> '.$this->dump($value, $depth + 1, $recursion);
  216. break;
  217. default :
  218. if($recursion === null)
  219. $recursion = new SplObjectStorage();
  220. $dump .= '<span style="'.$dump_type.'">'.get_class($var).'</span>';
  221. if($recursion->contains($var))
  222. return $dump.' <span style="'.$dump_type.'">*RECURSION*</span>';
  223. $recursion->attach($var);
  224. $reflect = new ReflectionClass($var);
  225. $properties = $reflect->getProperties();
  226. foreach($properties as $property) {
  227. $access = 'public';
  228. if($property->isPrivate()) {
  229. $property->setAccessible(true);
  230. $access = 'private';
  231. }
  232. if($property->isProtected()) {
  233. $property->setAccessible(true);
  234. $access = 'protected';
  235. }
  236. $dump .= PHP_EOL.str_repeat("\t", $depth + 1).'<span style="'.$dump_accessibility.'">'.$access.($property->isStatic() ? ' static' : '').'</span> <span style="'.$dump_value_attribute.'">'.$property->getName().'</span> <span style="'.$dump_operator.'">=&gt;</span> '.$this->dump($property->getValue($var), $depth + 1, $recursion);
  237. }
  238. break;
  239. }
  240. return $dump;
  241. }
  242. ###########################################################################
  243. ###########################################################################
  244. # FUTUR trait Yab_Mvc php 5.4
  245. ###########################################################################
  246. ###########################################################################
  247. # use Yab_Mvc;
  248. final public function startMvc($uri = null) {
  249. if($uri !== null)
  250. $this->getRequest()->setUri($uri);
  251. $this->getRouter()->route($this->getRequest());
  252. $this->getDispatcher()->dispatch($this->getRequest(), $this->getResponse());
  253. $this->getResponse()->send();
  254. }
  255. final public function getRequest($controller_or_uri = null, $action = null, array $params = array()) {
  256. if(!$controller_or_uri) {
  257. $request = self::getInstance('Yab_Controller_Request', array(true));
  258. if($request->getUri())
  259. return $request;
  260. if($request->isCli() && $request->getServer()->has('argv'))
  261. return $request->setUri('/'.$request->getServer()->cast('argv')->rem(0)->map('urlencode')->join('/'));
  262. if($request->isHttp() && $request->getServer()->has('REQUEST_URI'))
  263. return $request->setUri($request->getServer()->get('REQUEST_URI'));
  264. return $request;
  265. }
  266. $request = new Yab_Controller_Request();
  267. if(!$action) {
  268. $request->setBaseUrl($this->getRequest()->getBaseUrl())
  269. ->setUri($controller_or_uri);
  270. } else {
  271. $request->setBaseUrl($this->getRequest()->getBaseUrl())
  272. ->setController($controller_or_uri)
  273. ->setAction($action)
  274. ->setParams($params);
  275. }
  276. $this->getRouter()->route($request);
  277. return $request;
  278. }
  279. final public function getResponse($controller = null, $action = null, array $params = array()) {
  280. if(!$controller || !$action)
  281. return self::getInstance('Yab_Controller_Response');
  282. $request = new Yab_Controller_Request();
  283. $request->setBaseUrl($this->getRequest()->getBaseUrl())
  284. ->setController($controller)
  285. ->setAction($action)
  286. ->setParams($params);
  287. $response = new Yab_Controller_Response();
  288. $this->getDispatcher()->dispatch($request, $response);
  289. return $response;
  290. }
  291. final public function getRouter() {
  292. return Yab_Loader::getInstance('Yab_Controller_Router');
  293. }
  294. final public function getDispatcher() {
  295. return Yab_Loader::getInstance('Yab_Controller_Dispatcher');
  296. }
  297. final public function getSession() {
  298. return Yab_Loader::getInstance('Yab_Session');
  299. }
  300. final public function getRegistry() {
  301. return Yab_Loader::getInstance('Yab_Object');
  302. }
  303. final public function getConfig() {
  304. return Yab_Loader::getInstance('Yab_Config');
  305. }
  306. final public function getLayout() {
  307. return Yab_Loader::getInstance('Yab_View');
  308. }
  309. final public function getLog() {
  310. return Yab_Loader::getInstance('Yab_Log');
  311. }
  312. final public function forward($controller, $action = null, array $params = array(), $code = null) {
  313. return $this->getResponse()->redirect($this->getRequest($controller, $action, $params), $code);
  314. }
  315. final public function redirect($uri, $code = null) {
  316. return $this->getResponse()->redirect($uri, $code);
  317. }
  318. ###########################################################################
  319. ###########################################################################
  320. ###########################################################################
  321. ###########################################################################
  322. ###########################################################################
  323. }
  324. // Do not clause PHP tags unless it is really necessary