PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/panada/Gear.php

https://github.com/jenalgit/Panada
PHP | 271 lines | 190 code | 29 blank | 52 comment | 9 complexity | 39abb8f77933e41154fb92d0cffd5b96 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This is the heart of the whole Panada system.
  4. *
  5. * @author Iskandar Soesman <k4ndar@yahoo.com>
  6. * @link http://panadaframework.com/
  7. * @license http://www.opensource.org/licenses/bsd-license.php
  8. * @since version 1.0.0
  9. * @package Core System
  10. */
  11. final class Gear
  12. {
  13. private $uriObj, $config, $firstUriPath;
  14. /**
  15. * Preparation step before anything else.
  16. *
  17. * @return void
  18. */
  19. public function __construct($errorReporting = E_ALL)
  20. {
  21. error_reporting( $errorReporting );
  22. spl_autoload_register( array($this, 'loader') );
  23. set_exception_handler( 'Resources\RunException::main' );
  24. set_error_handler( 'Resources\RunException::errorHandlerCallback', error_reporting() );
  25. $this->disableMagicQuotes();
  26. $this->config['main'] = Resources\Config::main();
  27. $this->uriObj = new Resources\Uri;
  28. $this->uriObj->setDefaultController($this->config['main']['defaultController']);
  29. $this->firstUriPath = ucwords( $this->uriObj->getClass() );
  30. $this->controllerHandler();
  31. }
  32. /**
  33. * Magic loader to load instantiated class.
  34. *
  35. * @param string $file Class namespace.
  36. * @return void
  37. */
  38. private function loader($file)
  39. {
  40. $prefix = explode('\\', $file);
  41. switch ($prefix[0]) {
  42. case 'Resources':
  43. $folder = GEAR;
  44. break;
  45. case 'Drivers':
  46. $folder = GEAR;
  47. break;
  48. case 'Modules':
  49. $folder = $this->config['main']['module']['path'];
  50. break;
  51. default:
  52. $folder = APP;
  53. break;
  54. }
  55. if( ! file_exists( $file = $folder . str_ireplace('\\', '/', $file) . '.php' ) )
  56. throw new Resources\RunException('Resource '.$file.' not available!');
  57. include $file;
  58. }
  59. /**
  60. * We don't need Magic Quotes, let's kill it.
  61. *
  62. * @return void
  63. */
  64. private function disableMagicQuotes()
  65. {
  66. if ( get_magic_quotes_gpc() ) {
  67. array_walk_recursive($_GET, array($this, 'stripslashesGpc') );
  68. array_walk_recursive($_POST, array($this, 'stripslashesGpc') );
  69. array_walk_recursive($_COOKIE, array($this, 'stripslashesGpc') );
  70. array_walk_recursive($_REQUEST, array($this, 'stripslashesGpc') );
  71. }
  72. }
  73. /**
  74. * Strip the slash mark.
  75. *
  76. * @param string $value
  77. * @return void
  78. */
  79. private function stripslashesGpc(&$value)
  80. {
  81. $value = stripslashes($value);
  82. }
  83. /**
  84. * Hendle the controller calling process.
  85. *
  86. * The steps is:
  87. * - Does alias controller are defined in main config?
  88. * - If not, is sub-controller exists?
  89. * - If not, module with this name exists?
  90. * - If all fault, then throw 404.
  91. *
  92. * @return void
  93. */
  94. private function controllerHandler()
  95. {
  96. $controllerNamespace = 'Controllers\\' . $this->firstUriPath;
  97. if ( ! file_exists( $classFile = APP . 'Controllers/' . $this->firstUriPath . '.php' ) ) {
  98. $this->subControllerHandler();
  99. return;
  100. }
  101. $method = $this->uriObj->getMethod();
  102. if( ! $request = $this->uriObj->getRequests() )
  103. $request = array();
  104. if( ! class_exists($controllerNamespace) )
  105. throw new Resources\RunException('Class '.$controllerNamespace.' not found in '.$classFile);
  106. $instance = new $controllerNamespace;
  107. if ( ! method_exists($instance, $method) ) {
  108. $request = array_slice( $this->uriObj->path(), 1);
  109. $method = $this->config['main']['alias']['method'];
  110. if( ! method_exists($instance, $method) )
  111. throw new Resources\HttpException('Method '.$this->uriObj->getMethod().' does not exists in controller '.$this->firstUriPath);
  112. }
  113. $this->run($instance, $method, $request);
  114. }
  115. /**
  116. * Hendle the sub-controller calling process.
  117. *
  118. * @return void
  119. */
  120. private function subControllerHandler()
  121. {
  122. if ( ! is_dir( $subControllerFolder = APP . 'Controllers/' . $this->firstUriPath .'/') ) {
  123. $this->moduleHandler();
  124. return;
  125. }
  126. $controllerClass = $this->uriObj->getMethod(null);
  127. // No argument? set to default controller.
  128. if( is_null($controllerClass) )
  129. $controllerClass = $this->config['main']['defaultController'];
  130. $controllerClass = ucwords( $controllerClass );
  131. if( ! file_exists( $classFile = $subControllerFolder . $controllerClass . '.php') )
  132. throw new Resources\HttpException('Controller '.$controllerClass.' does not exists in sub-controller '.$this->firstUriPath.'.');
  133. $controllerNamespace = 'Controllers\\' . $this->firstUriPath . '\\' .$controllerClass;
  134. if( ! class_exists($controllerNamespace) )
  135. throw new Resources\RunException('Class '.$controllerNamespace.' not found in '.$classFile);
  136. $instance = new $controllerNamespace;
  137. $request = array_slice( $this->uriObj->path(), 3);
  138. if( ! $method = $this->uriObj->path(2) )
  139. $method = 'index';
  140. if ( ! method_exists($instance, $method) ) {
  141. $request = array_slice( $this->uriObj->path(), 2);
  142. $method = $this->config['main']['alias']['method'];
  143. if( ! method_exists($instance, $method) )
  144. throw new Resources\HttpException('Method '.$this->uriObj->path(2).' does not exists in controller /'.$this->firstUriPath.'/'.$controllerClass.'.');
  145. }
  146. $this->run($instance, $method, $request);
  147. }
  148. /**
  149. * Hendle the module calling process.
  150. *
  151. * @return void
  152. */
  153. private function moduleHandler()
  154. {
  155. if ( ! is_dir( $moduleFolder = $this->config['main']['module']['path'] . 'Modules/'. $this->firstUriPath . '/' ) ) {
  156. if ( isset($this->config['main']['alias']['controller']['class']) ) {
  157. $controllerClass = $this->config['main']['alias']['controller']['class'];
  158. if( ! file_exists( APP . 'Controllers/' . $controllerClass . '.php') )
  159. throw new Resources\HttpException('Controller, sub-controller or module '.$this->firstUriPath.' does not exists');
  160. $controllerNamespace= 'Controllers\\' .$controllerClass;
  161. $method = $this->config['main']['alias']['controller']['method'];
  162. $instance = new $controllerNamespace;
  163. $request = $this->uriObj->path();
  164. $this->run($instance, $method, $request);
  165. return;
  166. }
  167. }
  168. if( ! $controllerClass = $this->uriObj->path(1) )
  169. $controllerClass = $this->config['main']['defaultController'];
  170. $controllerClass = ucwords( $controllerClass );
  171. // Does this class's file exists?
  172. if ( ! file_exists( $classFile = $moduleFolder . 'Controllers/' . $controllerClass . '.php' ) ) {
  173. if( ! isset($this->config['main']['alias']['controller']['class']) )
  174. throw new Resources\HttpException('Controller '.$controllerClass.' does not exists in module '.$this->firstUriPath);
  175. $controllerClass = $this->config['main']['alias']['controller']['class'];
  176. $method = $this->config['main']['alias']['controller']['method'];
  177. $request = array_slice( $this->uriObj->path(), 1);
  178. // Does class for alias file exists?
  179. if( ! file_exists( $classFile = $moduleFolder . 'Controllers/' . $controllerClass . '.php' ) )
  180. throw new Resources\HttpException('Controller '.$controllerClass.' does not exists in module '.$this->firstUriPath);
  181. goto createNamespace;
  182. }
  183. $request = array_slice( $this->uriObj->path(), 3);
  184. if( ! $method = $this->uriObj->path(2) )
  185. $method = 'index';
  186. createNamespace:
  187. $controllerNamespace = 'Modules\\'.$this->firstUriPath.'\Controllers\\'.$controllerClass;
  188. if( ! class_exists($controllerNamespace) )
  189. throw new Resources\RunException('Class '.$controllerNamespace.' not found in '.$classFile);
  190. $instance = new $controllerNamespace;
  191. if ( ! method_exists($instance, $method) ) {
  192. $request = array_slice( $this->uriObj->path(), 2);
  193. $method = $this->config['main']['alias']['method'];
  194. if( ! method_exists($instance, $method) )
  195. throw new Resources\HttpException('Method '.$method.' does not exists in controller '.$moduleFolder.$controllerClass);
  196. }
  197. $this->run($instance, $method, $request );
  198. }
  199. /**
  200. * Call the controller's method
  201. *
  202. * @param object $instance
  203. * @param string $method
  204. * @param array $request
  205. * @return void
  206. */
  207. private function run($instance, $method, $request)
  208. {
  209. call_user_func_array(array($instance, $method), $request);
  210. }
  211. }