PageRenderTime 44ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/Yab/Loader.php

https://github.com/vib94/Framework
PHP | 522 lines | 287 code | 211 blank | 24 comment | 48 complexity | f0f7931ae819276da7ea513ef84bdc2d 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. $label = $label ? '['.$label.']'.$space : '';
  167. $benchmark = $label.'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;
  168. return $benchmark;
  169. }
  170. final public function dump($var, $depth = 0, SplObjectStorage $recursion = null) {
  171. if($this->getRequest()->isCli())
  172. return print_r($var, true);
  173. $dump_type = 'color: #007700';
  174. $dump_value_attribute = 'color: #0000dd';
  175. $dump_value_string = 'color: #0000dd';
  176. $dump_value_numeric = 'color: #dd0000';
  177. $dump_value_boolean = 'color: #000000';
  178. $dump_operator = 'color: #007700';
  179. $dump_accessibility = 'color: #666666';
  180. $dump = '';
  181. $type = strtolower(gettype($var));
  182. switch($type) {
  183. case 'null' :
  184. $dump .= '<span style="'.$dump_value_boolean.'">NULL</span>';
  185. break;
  186. case 'resource' :
  187. $dump .= '<span style="'.$dump_type.'">resource</span> <span style="'.$dump_value_string.'">'.$var.'</span>';
  188. break;
  189. case 'string' :
  190. $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>';
  191. break;
  192. case 'int' :
  193. case 'integer' :
  194. $dump .= '<span style="'.$dump_type.'">int(</span><span style="'.$dump_value_numeric.'">'.$var.'</span><span style="'.$dump_type.'">)</span>';
  195. break;
  196. case 'float' :
  197. case 'double' :
  198. $dump .= '<span style="'.$dump_type.'">float(</span><span style="'.$dump_value_numeric.'">'.$var.'</span><span style="'.$dump_type.'">)</span>';
  199. break;
  200. case 'bool' :
  201. case 'boolean' :
  202. $dump .= '<span style="'.$dump_type.'">bool(</span><span style="'.$dump_value_boolean.'">'.($var ? 'TRUE' : 'FALSE').'</span><span style="'.$dump_type.'">)</span>';
  203. break;
  204. case 'array' :
  205. $dump .= '<span style="'.$dump_type.'">array(</span><span style="'.$dump_value_numeric.'">'.count($var).'</span><span style="'.$dump_type.'">)</span>';
  206. foreach($var as $key => $value)
  207. $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);
  208. break;
  209. default :
  210. if($recursion === null)
  211. $recursion = new SplObjectStorage();
  212. $dump .= '<span style="'.$dump_type.'">'.get_class($var).'</span>';
  213. if($recursion->contains($var))
  214. return $dump.' <span style="'.$dump_type.'">*RECURSION*</span>';
  215. $recursion->attach($var);
  216. $reflect = new ReflectionClass($var);
  217. $reflect = new ReflectionClass($var);
  218. $properties = $reflect->getProperties();
  219. foreach($properties as $property) {
  220. $access = 'public';
  221. if($property->isPrivate()) {
  222. $property->setAccessible(true);
  223. $access = 'private';
  224. }
  225. if($property->isProtected()) {
  226. $property->setAccessible(true);
  227. $access = 'protected';
  228. }
  229. $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);
  230. }
  231. break;
  232. }
  233. return $dump;
  234. }
  235. ###########################################################################
  236. ###########################################################################
  237. # FUTUR trait Yab_Mvc php 5.4
  238. ###########################################################################
  239. ###########################################################################
  240. # use Yab_Mvc;
  241. final public function startMvc($uri = null) {
  242. if($uri !== null)
  243. $this->getRequest()->setUri($uri);
  244. $this->getRouter()->route($this->getRequest());
  245. $this->getDispatcher()->dispatch($this->getRequest(), $this->getResponse());
  246. $this->getResponse()->send();
  247. }
  248. final public function getRequest($controller = null, $action = null, array $params = array()) {
  249. if(!$controller) {
  250. $request = self::getInstance('Yab_Controller_Request', array(true));
  251. if($request->getUri())
  252. return $request;
  253. if($request->isCli() && $request->getServer()->has('argv'))
  254. return $request->setUri('/'.$request->getServer()->cast('argv')->rem(0)->map('urlencode')->join('/'));
  255. if($request->isHttp() && $request->getServer()->has('REQUEST_URI'))
  256. return $request->setUri($request->getServer()->get('REQUEST_URI'));
  257. return $request;
  258. }
  259. if(!$action)
  260. $action = $this->getRouter()->getDefaultAction();
  261. $request = new Yab_Controller_Request();
  262. $request->setBaseUrl($this->getRequest()->getBaseUrl())
  263. ->setController($controller)
  264. ->setAction($action)
  265. ->setParams($params);
  266. $this->getRouter()->route($request);
  267. return $request;
  268. }
  269. final public function getResponse($controller = null, $action = null, array $params = array()) {
  270. if(!$controller || !$action)
  271. return self::getInstance('Yab_Controller_Response');
  272. $request = new Yab_Controller_Request();
  273. $request->setBaseUrl($this->getRequest()->getBaseUrl())
  274. ->setController($controller)
  275. ->setAction($action)
  276. ->setParams($params);
  277. $response = new Yab_Controller_Response();
  278. $this->getDispatcher()->dispatch($request, $response);
  279. return $response;
  280. }
  281. final public function getRouter() {
  282. return Yab_Loader::getInstance('Yab_Controller_Router');
  283. }
  284. final public function getDispatcher() {
  285. return Yab_Loader::getInstance('Yab_Controller_Dispatcher');
  286. }
  287. final public function getSession() {
  288. return Yab_Loader::getInstance('Yab_Session');
  289. }
  290. final public function getRegistry() {
  291. return Yab_Loader::getInstance('Yab_Object');
  292. }
  293. final public function getConfig() {
  294. return Yab_Loader::getInstance('Yab_Config');
  295. }
  296. final public function getLayout() {
  297. return Yab_Loader::getInstance('Yab_View');
  298. }
  299. final public function forward($controller, $action = null, array $params = array(), $code = null) {
  300. return $this->getResponse()->redirect($this->getRequest($controller, $action, $params), $code);
  301. }
  302. final public function redirect($uri, $code = null) {
  303. return $this->getResponse()->redirect($uri, $code);
  304. }
  305. ###########################################################################
  306. ###########################################################################
  307. ###########################################################################
  308. ###########################################################################
  309. ###########################################################################
  310. }
  311. // Do not clause PHP tags unless it is really necessary