PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/ kosimpin/system/application/libraries/fooStack/fooLoader.php

http://kosimpin.googlecode.com/
PHP | 387 lines | 242 code | 47 blank | 98 comment | 37 complexity | ad18a9e8e1090b83984e66416cae4134 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. /*
  3. * fooStack, CIUnit for CodeIgniter
  4. * Copyright (c) 2008-2009 Clemens Gruenberger
  5. * Released under the MIT license, see:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. */
  8. /*
  9. * CodeIgniter source modified for fooStack / CIUnit
  10. */
  11. class fooLoader extends CI_Loader {
  12. var $_ci_loaded_files = array();
  13. function __construct()
  14. {
  15. parent::__construct();
  16. }
  17. /**
  18. * Load class
  19. *
  20. * This function loads the requested class.
  21. *
  22. * @access private
  23. * @param string the item that is being loaded
  24. * @param mixed any additional parameters
  25. * @return void
  26. */
  27. function _ci_load_class($class, $params = NULL, $object_name = NULL)
  28. {
  29. // Get the class name
  30. $class = str_replace(EXT, '', $class);
  31. $prefix = config_item('subclass_prefix');
  32. $is_fooclass = FALSE;
  33. //is it in a subfolder?
  34. $folders = explode('/', $class);
  35. if (count($folders) > 1)
  36. {
  37. $class = array_pop($folders);
  38. $folders = join('/', $folders).'/';
  39. }
  40. else
  41. {
  42. $folders = '';
  43. }
  44. //print_r($folders);
  45. // We'll test for both lowercase and capitalized versions of the file name
  46. foreach (array(ucfirst($class), strtolower($class)) as $class)
  47. {
  48. $subclass = APPPATH.'libraries/'.$folders.$prefix.$class.EXT;
  49. // Is this a class extension request?
  50. if (file_exists($subclass))
  51. {
  52. $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
  53. if (file_exists(FSPATH.config_item('fooStack_prefix').$class.EXT))
  54. {
  55. require(APPPATH.'libraries/fooStack/foo'.$class.EXT);
  56. $is_fooclass = TRUE;
  57. }
  58. if ( !file_exists($baseclass) )
  59. {
  60. log_message('error', "Unable to load the requested class: ".$class);
  61. show_error("Unable to load the requested class: ".$class);
  62. }
  63. //redesignme, should we load the files?
  64. //not if another controller before us loaded them
  65. $include_files = true;
  66. // Safety: Was the class already loaded by a previous call?
  67. if (in_array($subclass, $this->_ci_loaded_files))
  68. {
  69. //unittest, we have to reassign it
  70. if ( !defined('CIUnit_Version') )
  71. {
  72. $is_duplicate = TRUE;
  73. log_message('debug', $class." class already loaded. Second attempt ignored.");
  74. return;
  75. }
  76. else
  77. {
  78. $include_files = false;
  79. }
  80. }
  81. if ($include_files)
  82. {
  83. include($baseclass);
  84. include($subclass);
  85. $this->_ci_loaded_files[] = $subclass;
  86. }
  87. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  88. }
  89. // its not an extension request
  90. // Lets search for the requested library file and load it.
  91. $is_duplicate = FALSE;
  92. foreach (array(BASEPATH.'libraries/', APPPATH.'libraries/', FSPATH) as $path)
  93. {
  94. $filepath = $path.$folders.$class.EXT;
  95. // Does the file exist? No? Bummer...
  96. if ( ! file_exists($filepath))
  97. {
  98. continue;
  99. }
  100. // CI_Unit, should we load the files?
  101. // not if another controller before us loaded them
  102. $include_files = true;
  103. // Safety: Was the class already loaded by a previous call?
  104. if (in_array($filepath, $this->_ci_loaded_files))
  105. {
  106. //same thing for main classes
  107. //redesignme unittest, we have to reassign it
  108. if(!defined('CIUnit_Version'))
  109. {
  110. $is_duplicate = TRUE;
  111. log_message('debug', $class." class already loaded. Second attempt ignored.");
  112. return;
  113. }
  114. else
  115. {
  116. $include_files = false;
  117. }
  118. }
  119. if ($include_files)
  120. {
  121. include($filepath);
  122. $this->_ci_loaded_files[] = $filepath;
  123. }
  124. return $this->_ci_init_class($class, '', $params, $object_name);
  125. }
  126. } // END FOREACH
  127. // Subfolder? (@Mario "Kuroir" Ricalde)
  128. if ($folders == '')
  129. {
  130. $path = strtolower($class).'/'.$class;
  131. return $this->_ci_load_class($path, $params);
  132. }
  133. // If we got this far we were unable to find the requested class.
  134. // We do not issue errors if the load call failed due to a duplicate request
  135. if ($is_duplicate == FALSE)
  136. {
  137. log_message('error', "Unable to load the requested class: ".$class);
  138. show_error("Unable to load the requested class: ".$class);
  139. }
  140. }
  141. /**
  142. * Instantiates a class
  143. *
  144. * @access private
  145. * @param string
  146. * @param string
  147. * @return null
  148. */
  149. function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
  150. {
  151. // Is there an associated config file for this class?
  152. if ($config === NULL)
  153. {
  154. foreach(array(ucfirst($class), strtolower($class)) as $clsName) {
  155. if (file_exists(APPPATH.'config/'.$clsName.EXT))
  156. {
  157. include(APPPATH.'config/'.$clsName.EXT);
  158. }
  159. }
  160. }
  161. if ($prefix == '')
  162. {
  163. $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
  164. }
  165. else
  166. {
  167. $name = $prefix.$class;
  168. }
  169. // Set the variable name we will assign the class to
  170. $class = strtolower($class);
  171. if (is_null($object_name))
  172. {
  173. $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
  174. }
  175. else
  176. {
  177. $classvar = $object_name;
  178. }
  179. // Instantiate the class
  180. $CI =& get_instance();
  181. if ($config !== NULL)
  182. {
  183. if (!defined('CIUnit_Version'))
  184. {
  185. $CI->$classvar = new $name($config);
  186. }
  187. elseif (!isset($CI->$classvar))
  188. {
  189. //redesignme: check if we have got one already..
  190. $CI->$classvar = new $name($config);
  191. }
  192. }
  193. else
  194. {
  195. if (!defined('CIUnit_Version'))
  196. {
  197. $CI->$classvar = new $name;
  198. }
  199. elseif (!isset($CI->$classvar))
  200. {
  201. //redesignme: check if we have got one already..
  202. $CI->$classvar = new $name($config);
  203. }
  204. }
  205. $this->_ci_classes[$class] = $classvar;
  206. }
  207. /**
  208. * Database Loader
  209. *
  210. * @access public
  211. * @param string the DB credentials
  212. * @param bool whether to return the DB object
  213. * @param bool whether to enable active record (this allows us to override the config setting)
  214. * @return object
  215. */
  216. function database($params = '', $return = FALSE, $active_record = FALSE)
  217. {
  218. //redesignme, unittest check if there is a DB class already instantiated
  219. //reuse it if yes
  220. if (isset($this->_ci_db) and !$return){
  221. // Grab the super object
  222. $CI =& get_instance();
  223. $CI->db = $this->_ci_db;
  224. }else{
  225. // Do we even need to load the database class?
  226. if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE)
  227. {
  228. return FALSE;
  229. }
  230. require_once(BASEPATH.'database/DB'.EXT);
  231. // Load the DB class
  232. $db =& DB($params, $active_record);
  233. $my_driver = config_item('subclass_prefix').'DB_'.$db->dbdriver.'_driver';
  234. $my_driver_file = APPPATH.'libraries/'.$my_driver.EXT;
  235. if (file_exists($my_driver_file))
  236. {
  237. require_once($my_driver_file);
  238. $db =& new $my_driver(get_object_vars($db));
  239. }
  240. if ($return === TRUE)
  241. {
  242. return $db;
  243. }
  244. // Grab the super object
  245. $CI =& get_instance();
  246. // Initialize the db variable. Needed to prevent
  247. // reference errors with some configurations
  248. $CI->db = '';
  249. $CI->db = $db;
  250. $this->_ci_db =$CI->db;
  251. }
  252. // Assign the DB object to any existing models
  253. $this->_ci_assign_to_models();
  254. }
  255. // --------------------------------------------------------------------
  256. /**
  257. * Autoloader
  258. *
  259. * The config/autoload.php file contains an array that permits sub-systems,
  260. * libraries, plugins, and helpers to be loaded automatically.
  261. *
  262. * @access private
  263. * @param array
  264. * @return void
  265. */
  266. function _ci_autoloader()
  267. {
  268. //enable multiple autoload during tests
  269. include(APPPATH.'config/autoload'.EXT);
  270. //include_once(APPPATH.'config/autoload'.EXT);
  271. if ( ! isset($autoload))
  272. {
  273. return FALSE;
  274. }
  275. // Load any custom config file
  276. if (count($autoload['config']) > 0)
  277. {
  278. $CI =& get_instance();
  279. foreach ($autoload['config'] as $key => $val)
  280. {
  281. $CI->config->load($val);
  282. }
  283. }
  284. // Autoload plugins, helpers and languages
  285. foreach (array('helper', 'plugin', 'language') as $type)
  286. {
  287. if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
  288. {
  289. $this->$type($autoload[$type]);
  290. }
  291. }
  292. // A little tweak to remain backward compatible
  293. // The $autoload['core'] item was deprecated
  294. if ( ! isset($autoload['libraries']))
  295. {
  296. $autoload['libraries'] = $autoload['core'];
  297. }
  298. // Load libraries
  299. if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
  300. {
  301. // Load the database driver.
  302. if (in_array('database', $autoload['libraries']))
  303. {
  304. $this->database();
  305. $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
  306. }
  307. // Load scaffolding
  308. if (in_array('scaffolding', $autoload['libraries']))
  309. {
  310. $this->scaffolding();
  311. $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
  312. }
  313. // Load all other libraries
  314. foreach ($autoload['libraries'] as $item)
  315. {
  316. $this->library($item);
  317. }
  318. }
  319. // Autoload models
  320. if (isset($autoload['model']))
  321. {
  322. $this->model($autoload['model']);
  323. }
  324. }
  325. /*
  326. * Can load a view file from an absolute path and
  327. * relative to the CodeIgniter index.php file
  328. * Handy if you have views outside the usual CI views dir
  329. */
  330. function viewfile($viewfile, $vars = array(), $return = FALSE)
  331. {
  332. return $this->_ci_load(
  333. array('_ci_path' => $viewfile,
  334. '_ci_vars' => $this->_ci_object_to_array($vars),
  335. '_ci_return' => $return)
  336. );
  337. }
  338. // --------------------------------------------------------------------
  339. }
  340. ?>