blob: 4fc9c63b65a94287927f0435b3d1419ecc7a1379 [file] [log] [blame]
Taufan Adityaca16c4f2012-03-28 15:15:30 +07001<?php
2
3// This autoloader provide convinient way to working with mock object
4// make the test looks natural. This autoloader support cascade file loading as well
5// within mocks directory.
6//
7// Prototype :
8//
Taufan Adityaac5373a2012-03-28 16:03:38 +07009// $mock_table = new Mock_Libraries_Table(); // Will load ./mocks/libraries/table.php
Andrey Andreevfc11dcc2012-06-04 16:39:19 +030010// $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php
Taufan Adityaac5373a2012-03-28 16:03:38 +070011// and so on...
Andrey Andreevfc11dcc2012-06-04 16:39:19 +030012function autoload($class)
Taufan Adityaca16c4f2012-03-28 15:15:30 +070013{
Taufan Adityaca16c4f2012-03-28 15:15:30 +070014 $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR;
Taufan Adityaac5373a2012-03-28 16:03:38 +070015
16 $ci_core = array(
17 'Benchmark', 'Config', 'Controller',
18 'Exceptions', 'Hooks', 'Input',
19 'Lang', 'Loader', 'Model',
20 'Output', 'Router', 'Security',
21 'URI', 'Utf8',
22 );
23
24 $ci_libraries = array(
Taufan Aditya6c7526c2012-05-27 13:51:27 +070025 'Calendar', 'Cart', 'Driver_Library',
Taufan Adityaac5373a2012-03-28 16:03:38 +070026 'Email', 'Encrypt', 'Form_validation',
27 'Ftp', 'Image_lib', 'Javascript',
28 'Log', 'Migration', 'Pagination',
dchill4257486002012-07-31 09:39:53 -040029 'Parser', 'Profiler', 'Table',
30 'Trackback', 'Typography', 'Unit_test',
31 'Upload', 'User_agent', 'Xmlrpc',
32 'Zip',
33 );
34
35 $ci_drivers = array(
36 'Session',
Taufan Adityaac5373a2012-03-28 16:03:38 +070037 );
38
39 if (strpos($class, 'Mock_') === 0)
40 {
Andrey Andreeve24eed72012-11-02 23:33:45 +020041 $class = strtolower(str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class));
Taufan Adityaac5373a2012-03-28 16:03:38 +070042 }
43 elseif (strpos($class, 'CI_') === 0)
44 {
Andrey Andreeve24eed72012-11-02 23:33:45 +020045 $subclass = substr($class, 3);
Taufan Adityaac5373a2012-03-28 16:03:38 +070046
47 if (in_array($subclass, $ci_core))
48 {
dchill427ecc5cd2012-10-12 16:25:51 -040049 $dir = SYSTEM_PATH.'core'.DIRECTORY_SEPARATOR;
Taufan Adityaac5373a2012-03-28 16:03:38 +070050 $class = $subclass;
51 }
52 elseif (in_array($subclass, $ci_libraries))
53 {
dchill427ecc5cd2012-10-12 16:25:51 -040054 $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR;
Alex Bilbied6d11502012-06-02 11:12:55 +010055 $class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass;
Taufan Adityaac5373a2012-03-28 16:03:38 +070056 }
dchill4257486002012-07-31 09:39:53 -040057 elseif (in_array($subclass, $ci_drivers))
58 {
dchill427ecc5cd2012-10-12 16:25:51 -040059 $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR;
dchill4257486002012-07-31 09:39:53 -040060 $class = $subclass;
61 }
62 elseif (in_array(($parent = strtok($subclass, '_')), $ci_drivers)) {
dchill427ecc5cd2012-10-12 16:25:51 -040063 $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
dchill4257486002012-07-31 09:39:53 -040064 $class = $subclass;
65 }
dchill42e9435dc2012-10-14 15:44:39 -040066 elseif (preg_match('/^CI_DB_(.+)_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 4)
67 {
68 $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
69 $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR.'subdrivers'.DIRECTORY_SEPARATOR;
70 $file = $dir.$m[1].'_'.$m[2].'_'.$m[3].'.php';
71 }
Alex Bilbied6d11502012-06-02 11:12:55 +010072 elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3)
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070073 {
dchill427ecc5cd2012-10-12 16:25:51 -040074 $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070075 $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR;
76 $file = $dir.$m[1].'_'.$m[2].'.php';
77 }
78 elseif (strpos($class, 'CI_DB') === 0)
79 {
dchill427ecc5cd2012-10-12 16:25:51 -040080 $dir = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR;
Taufan Aditya61ff0542012-04-05 01:23:56 +070081 $file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php';
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070082 }
Taufan Adityaac5373a2012-03-28 16:03:38 +070083 else
84 {
85 $class = strtolower($class);
86 }
87 }
88
Andrey Andreeve24eed72012-11-02 23:33:45 +020089 $file = isset($file) ? $file : $dir.$class.'.php';
Taufan Adityaca16c4f2012-03-28 15:15:30 +070090
91 if ( ! file_exists($file))
92 {
93 $trace = debug_backtrace();
94
Alex Bilbied6d11502012-06-02 11:12:55 +010095 if ($trace[2]['function'] === 'class_exists' OR $trace[2]['function'] === 'file_exists')
Taufan Adityaca16c4f2012-03-28 15:15:30 +070096 {
Taufan Adityab8b7dbe2012-06-27 03:05:35 +070097 // If the autoload call came from `class_exists` or `file_exists`,
98 // we skipped and return FALSE
99 return FALSE;
100 }
101 elseif (($autoloader = spl_autoload_functions()) && end($autoloader) !== __FUNCTION__)
102 {
103 // If there was other custom autoloader, passed away
Taufan Adityaca16c4f2012-03-28 15:15:30 +0700104 return FALSE;
105 }
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300106
Taufan Aditya8f490da2012-06-27 03:31:22 +0700107 throw new InvalidArgumentException("Unable to load {$class}.");
Taufan Adityaca16c4f2012-03-28 15:15:30 +0700108 }
109
110 include_once($file);
dchill42e9435dc2012-10-14 15:44:39 -0400111}