PageRenderTime 138ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/application/third_party/CIUnit/core/CIU_Loader.php

https://github.com/fukata/Study-GooglePlusAPI
PHP | 322 lines | 142 code | 30 blank | 150 comment | 22 complexity | e46b0b3d0a08eaa2d42e8970e57a6da7 MD5 | raw file
  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. /**
  12. * ============================================================================
  13. * Note that you will need to update the parent class if you have the class
  14. * MY_Loader.
  15. * ============================================================================
  16. */
  17. class CIU_Loader extends CI_Loader {
  18. var $_ci_loaded_files = array();
  19. /**
  20. * Load class
  21. *
  22. * This function loads the requested class.
  23. *
  24. * @access private
  25. * @param string the item that is being loaded
  26. * @param mixed any additional parameters
  27. * @return void
  28. */
  29. function _ci_load_class($class, $params = NULL, $object_name = NULL)
  30. {
  31. // Get the class name, and while we're at it trim any slashes.
  32. // The directory path can be included as part of the class name,
  33. // but we don't want a leading slash
  34. $class = str_replace(EXT, '', trim($class, '/'));
  35. // Was the path included with the class name?
  36. // We look for a slash to determine this
  37. $subdir = '';
  38. if (($last_slash = strrpos($class, '/')) !== FALSE)
  39. {
  40. // Extract the path
  41. $subdir = substr($class, 0, $last_slash + 1);
  42. // Get the filename from the path
  43. $class = substr($class, $last_slash + 1);
  44. }
  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/'.$subdir.config_item('subclass_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($baseclass))
  54. {
  55. log_message('error', "Unable to load the requested class: ".$class);
  56. show_error("Unable to load the requested class: ".$class);
  57. }
  58. // Safety: Was the class already loaded by a previous call?
  59. if (in_array($subclass, $this->_ci_loaded_files))
  60. {
  61. // Before we deem this to be a duplicate request, let's see
  62. // if a custom object name is being supplied. If so, we'll
  63. // return a new instance of the object
  64. if ( ! is_null($object_name))
  65. {
  66. $CI =& get_instance();
  67. if ( ! isset($CI->$object_name))
  68. {
  69. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  70. }
  71. }
  72. $is_duplicate = TRUE;
  73. log_message('debug', $class." class already loaded. Second attempt ignored.");
  74. return;
  75. }
  76. include_once($baseclass);
  77. include_once($subclass);
  78. $this->_ci_loaded_files[] = $subclass;
  79. return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
  80. }
  81. // Lets search for the requested library file and load it.
  82. $is_duplicate = FALSE;
  83. foreach ($this->_ci_library_paths as $path)
  84. {
  85. $filepath = $path.'libraries/'.$subdir.$class.EXT;
  86. // Does the file exist? No? Bummer...
  87. if ( ! file_exists($filepath))
  88. {
  89. continue;
  90. }
  91. // Safety: Was the class already loaded by a previous call?
  92. if (in_array($filepath, $this->_ci_loaded_files))
  93. {
  94. // Before we deem this to be a duplicate request, let's see
  95. // if a custom object name is being supplied. If so, we'll
  96. // return a new instance of the object
  97. if ( ! is_null($object_name))
  98. {
  99. $CI =& get_instance();
  100. if ( ! isset($CI->$object_name))
  101. {
  102. return $this->_ci_init_class($class, '', $params, $object_name);
  103. }
  104. }
  105. $is_duplicate = TRUE;
  106. log_message('debug', $class." class already loaded. Second attempt ignored.");
  107. return;
  108. }
  109. include_once($filepath);
  110. $this->_ci_loaded_files[] = $filepath;
  111. return $this->_ci_init_class($class, '', $params, $object_name);
  112. }
  113. } // END FOREACH
  114. // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
  115. if ($subdir == '')
  116. {
  117. $path = strtolower($class).'/'.$class;
  118. return $this->_ci_load_class($path, $params);
  119. }
  120. // If we got this far we were unable to find the requested class.
  121. // We do not issue errors if the load call failed due to a duplicate request
  122. if ($is_duplicate == FALSE)
  123. {
  124. log_message('error', "Unable to load the requested class: ".$class);
  125. show_error("Unable to load the requested class: ".$class);
  126. }
  127. }
  128. /**
  129. * Instantiates a class
  130. *
  131. * @access private
  132. * @param string
  133. * @param string
  134. * @return null
  135. */
  136. function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
  137. {
  138. // Is there an associated config file for this class?
  139. if ($config === NULL)
  140. {
  141. foreach(array(ucfirst($class), strtolower($class)) as $clsName) {
  142. if (file_exists(APPPATH.'config/'.$clsName.EXT))
  143. {
  144. include(APPPATH.'config/'.$clsName.EXT);
  145. }
  146. }
  147. }
  148. if ($prefix == '')
  149. {
  150. $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
  151. }
  152. else
  153. {
  154. $name = $prefix.$class;
  155. }
  156. // Set the variable name we will assign the class to
  157. $class = strtolower($class);
  158. if (is_null($object_name))
  159. {
  160. $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
  161. }
  162. else
  163. {
  164. $classvar = $object_name;
  165. }
  166. // Instantiate the class
  167. $CI =& get_instance();
  168. if ($config !== NULL)
  169. {
  170. if (!defined('CIUnit_Version'))
  171. {
  172. $CI->$classvar = new $name($config);
  173. }
  174. elseif (!isset($CI->$classvar))
  175. {
  176. //redesignme: check if we have got one already..
  177. $CI->$classvar = new $name($config);
  178. }
  179. }
  180. else
  181. {
  182. if (!defined('CIUnit_Version'))
  183. {
  184. $CI->$classvar = new $name;
  185. }
  186. elseif (!isset($CI->$classvar))
  187. {
  188. //redesignme: check if we have got one already..
  189. $CI->$classvar = new $name($config);
  190. }
  191. }
  192. $this->_ci_classes[$class] = $classvar;
  193. }
  194. // --------------------------------------------------------------------
  195. /**
  196. * Autoloader
  197. *
  198. * The config/autoload.php file contains an array that permits sub-systems,
  199. * libraries, plugins, and helpers to be loaded automatically.
  200. *
  201. * @access private
  202. * @param array
  203. * @return void
  204. */
  205. /*
  206. function _ci_autoloader()
  207. {
  208. //enable multiple autoload during tests
  209. include(APPPATH.'config/autoload'.EXT);
  210. //include_once(APPPATH.'config/autoload'.EXT);
  211. if ( ! isset($autoload))
  212. {
  213. return FALSE;
  214. }
  215. // Autoload packages
  216. if (isset($autoload['packages']))
  217. {
  218. foreach ($autoload['packages'] as $package_path)
  219. {
  220. $this->add_package_path($package_path);
  221. }
  222. }
  223. // Load any custom config file
  224. if (count($autoload['config']) > 0)
  225. {
  226. $CI =& get_instance();
  227. foreach ($autoload['config'] as $key => $val)
  228. {
  229. $CI->config->load($val);
  230. }
  231. }
  232. // Autoload helpers and languages
  233. foreach (array('helper', 'language') as $type)
  234. {
  235. if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
  236. {
  237. $this->$type($autoload[$type]);
  238. }
  239. }
  240. // A little tweak to remain backward compatible
  241. // The $autoload['core'] item was deprecated
  242. if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
  243. {
  244. $autoload['libraries'] = $autoload['core'];
  245. }
  246. // Load libraries
  247. if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
  248. {
  249. // Load the database driver.
  250. if (in_array('database', $autoload['libraries']))
  251. {
  252. $this->database();
  253. $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
  254. }
  255. // Load all other libraries
  256. foreach ($autoload['libraries'] as $item)
  257. {
  258. $this->library($item);
  259. }
  260. }
  261. // Autoload models
  262. if (isset($autoload['model']))
  263. {
  264. $this->model($autoload['model']);
  265. }
  266. }
  267. */
  268. /*
  269. * Can load a view file from an absolute path and
  270. * relative to the CodeIgniter index.php file
  271. * Handy if you have views outside the usual CI views dir
  272. */
  273. function viewfile($viewfile, $vars = array(), $return = FALSE)
  274. {
  275. return $this->_ci_load(
  276. array('_ci_path' => $viewfile,
  277. '_ci_vars' => $this->_ci_object_to_array($vars),
  278. '_ci_return' => $return)
  279. );
  280. }
  281. }
  282. /* End of file CIU_Loader.php */
  283. /* Location ./system/application/third_party/CIUnitTest/core/CIU_Loader.php */