/panada/library/module.php

https://github.com/ismanramadhan/Panada · PHP · 309 lines · 157 code · 86 blank · 66 comment · 30 complexity · c731fe8e68826f3deaeb7ab2f62072cf MD5 · raw file

  1. <?php defined('THISPATH') or die('Can\'t access directly!');
  2. /**
  3. * Module class parent.
  4. * Every module controller should be child of this class.
  5. *
  6. * @package Panada
  7. * @subpackage Library
  8. * @author Iskandar Soesman
  9. * @since Version 0.3
  10. */
  11. class Panada_module extends Panada {
  12. static public $_module_name;
  13. public function __construct(){
  14. parent::__construct();
  15. spl_autoload_register(array($this, 'component_loader'));
  16. spl_autoload_register('__autoload');
  17. $this->base_path = GEAR . 'module/' . self::$_module_name . '/';
  18. }
  19. public function component_loader($class){
  20. $class = strtolower($class);
  21. $file_name = explode('_', $class);
  22. $components = array('library', 'model');
  23. $module_name= false;
  24. foreach($components as $component){
  25. $key = array_search($component, $file_name);
  26. if( $key !== false ){
  27. $module_name = array_slice($file_name, 0, $key);
  28. $module_name = implode('_', $module_name);
  29. $class_name = array_slice($file_name, $key + 1, count($file_name));
  30. $class_name = implode('_', $class_name);
  31. break;
  32. }
  33. }
  34. if( ! $module_name )
  35. return false;
  36. $file = GEAR . 'module/'. $module_name . '/' . $component . '/' . $class_name .'.php';
  37. include_once $file;
  38. }
  39. }
  40. //end Panada_module class
  41. /**
  42. * Module loader.
  43. * A class for load a module from main controller.
  44. *
  45. * @package Panada
  46. * @subpackage Library
  47. * @author Iskandar Soesman
  48. * @since Version 0.3
  49. */
  50. class Library_module {
  51. private $class_inctance;
  52. private $module_name;
  53. /**
  54. * Loader for module
  55. *
  56. * @param mix String or array for module configuration
  57. * @return object
  58. */
  59. public function __construct($args = false){
  60. if( ! $args )
  61. return false;
  62. if( is_string($args) ){
  63. $args = array(
  64. 'name' => $args,
  65. 'controller' => 'home'
  66. );
  67. }
  68. $default = array(
  69. 'controller' => 'home'
  70. );
  71. $module = array_merge($default, $args);
  72. $file = 'module/' . $module['name'] . '/controller/' . $module['controller'];
  73. Panada_module::$_module_name = $module['name'];
  74. $this->module_name = $module['name'];
  75. $this->load_file($file);
  76. }
  77. /**
  78. * Include the actual class file
  79. *
  80. * @param string $file_path Absolute path of the file location
  81. * @return void
  82. */
  83. private function load_file($file_path){
  84. $arr = explode('/', $file_path);
  85. $file_name = end( $arr );
  86. $prefix = $arr[2];
  87. $file_path = GEAR . $file_path.'.php';
  88. if( ! file_exists( $file_path ) )
  89. Library_error::_500('<b>Error:</b> No <b>'.$file_name.'</b> file in '.$arr[0].' folder.');
  90. $class = ucwords($this->module_name).'_'.$prefix.'_'.$file_name;
  91. include_once $file_path;
  92. if( ! class_exists($class) )
  93. Library_error::_500('<b>Error:</b> No class <b>'.$class.'</b> exists in file '.$file_name.'.');
  94. $this->class_inctance = new $class();
  95. //assigning the autoloaded class into each vars object
  96. if( ! empty($this->config->auto_loader) ) {
  97. $object_vars = get_object_vars($this->class_inctance);
  98. unset($object_vars['config'], $object_vars['base_url']);
  99. $object_vars = array_keys($object_vars);
  100. $auto_loader = (array) $this->config->auto_loader;
  101. foreach( $auto_loader as $class_name){
  102. $var = Panada::var_name($class_name);
  103. $class_instance = new $class_name();
  104. $this->class_inctance->$var = $class_instance;
  105. foreach($object_vars as $object_vars)
  106. if($object_vars != $var && is_object($this->class_inctance->$object_vars) )
  107. $this->class_inctance->$object_vars->$var = $class_instance;
  108. }
  109. }
  110. }
  111. /**
  112. * Magic method for calling a method from loaded class
  113. *
  114. * @param string Method name
  115. * @param array Method arguments
  116. * @return mix Method return value
  117. */
  118. public function __call($name, $arguments = array() ){
  119. return call_user_func_array( array($this->class_inctance, $name), $arguments );
  120. }
  121. /**
  122. * Magic method for getting a property from loaded class
  123. *
  124. * @param string Property name
  125. * @return mix The value of the property
  126. */
  127. public function __get($name){
  128. return $this->class_inctance->$name;
  129. }
  130. /**
  131. * Magic method for setting a property for loaded class
  132. *
  133. * @param string Properties name
  134. * @param mix the value to store
  135. * @return void
  136. */
  137. public function __set($name, $value){
  138. $this->class_inctance->$name = $value;
  139. }
  140. /**
  141. * Hendle routing direct from url. The url
  142. * should looks: http://website.com/index.php/module_name/controller_name/method_name
  143. *
  144. */
  145. public function public_routing(){
  146. $pan_uri = new Library_uri();
  147. $module_name = $pan_uri->get_class();
  148. $controller = $pan_uri->get_method('home');
  149. $method = $pan_uri->break_uri_string(3);
  150. $class = ucwords($module_name).'_controller_'.$controller;
  151. Panada_module::$_module_name = $module_name;
  152. $module_config = $this->config(true);
  153. // Does this controller can be accessed via URL?
  154. if( ! $module_config['allow_url_routing'] )
  155. Library_error::_404();
  156. if( is_array($module_config['allow_url_routing']) && ! empty($module_config['allow_url_routing']) ){
  157. if( array_search($controller, $module_config['allow_url_routing']) === false )
  158. goto alias_controller;
  159. }
  160. if( empty($method) )
  161. $method = 'index';
  162. if( ! $request = $pan_uri->get_requests(4) )
  163. $request = array();
  164. if( ! file_exists( $file = GEAR . 'module/' . $module_name . '/controller/' . $controller . '.php' ) ){
  165. alias_controller:
  166. // Does alias controller config exists?
  167. if( empty($module_config['alias_controller']) )
  168. Library_error::_404();
  169. $controller = array_keys($module_config['alias_controller']);
  170. $controller = $controller[0];
  171. $method = array_values($module_config['alias_controller']);
  172. $method = $method[0];
  173. $class = ucwords($module_name).'_controller_'.$controller;
  174. if( ! $request = $pan_uri->get_requests(2) )
  175. $request = array();
  176. if( ! file_exists( $file = GEAR . 'module/' . $module_name . '/controller/' . $controller . '.php' ) )
  177. Library_error::_500('<b>Error:</b> No alias controller exists in module <b>' . $module_name . '</b>. Check your module configuration.');
  178. }
  179. include_once $file;
  180. $Panada = new $class();
  181. // autoloader
  182. if( ! empty($Panada->config->auto_loader) ) {
  183. $object_vars = get_object_vars($Panada);
  184. unset($object_vars['config'], $object_vars['base_url']);
  185. $object_vars = array_keys($object_vars);
  186. $auto_loader = (array) $Panada->config->auto_loader;
  187. foreach( $auto_loader as $class_name){
  188. $var = Panada::var_name($class_name);
  189. $class_instance = new $class_name();
  190. $Panada->$var = $class_instance;
  191. foreach($object_vars as $object_vars)
  192. if($object_vars != $var && is_object($Panada->$object_vars) )
  193. $Panada->$object_vars->$var = $class_instance;
  194. }
  195. }
  196. if( ! method_exists($Panada, $method) ){
  197. $request = ( ! empty($request) ) ? array_merge(array($method), $request) : array($method);
  198. $method = $Panada->config->alias_method;
  199. if( ! method_exists($Panada, $method) )
  200. Library_error::_404();
  201. }
  202. call_user_func_array(array($Panada, $method), $request);
  203. }
  204. /**
  205. * Load the module config if exists
  206. *
  207. * @param bool
  208. * @return mix
  209. */
  210. public function config($as_array = false){
  211. if( ! file_exists($file = GEAR . 'module/' . Panada_module::$_module_name . '/config.php') )
  212. return false;
  213. include_once $file;
  214. if( ! isset($CONFIG) || empty($CONFIG) )
  215. return false;
  216. if( $as_array )
  217. return $CONFIG;
  218. $return = new stdClass();
  219. foreach($CONFIG as $key => $val)
  220. $return->$key = Library_tools::array_to_object($val);
  221. return $return;
  222. }
  223. } // End Library_module