/panada/Gear.php

https://github.com/back2arie/Panada · PHP · 267 lines · 147 code · 57 blank · 63 comment · 26 complexity · b1f60a588cb7fdcd5f505c1cc0d38000 MD5 · raw file

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