Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 1 | <?php |
| 2 | |
Andrey Andreev | 2eee870 | 2016-12-08 12:21:40 +0200 | [diff] [blame] | 3 | // This autoloader provide convenient way to working with mock object |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 4 | // make the test looks natural. This autoloader support cascade file loading as well |
| 5 | // within mocks directory. |
| 6 | // |
| 7 | // Prototype : |
| 8 | // |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 9 | // $mock_table = new Mock_Libraries_Table(); // Will load ./mocks/libraries/table.php |
Andrey Andreev | fc11dcc | 2012-06-04 16:39:19 +0300 | [diff] [blame] | 10 | // $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 11 | // and so on... |
Andrey Andreev | fc11dcc | 2012-06-04 16:39:19 +0300 | [diff] [blame] | 12 | function autoload($class) |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 13 | { |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 14 | $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 15 | |
| 16 | $ci_core = array( |
Andrey Andreev | 912831f | 2014-02-04 17:21:37 +0200 | [diff] [blame] | 17 | 'Benchmark', |
| 18 | 'Config', |
| 19 | 'Controller', |
| 20 | 'Exceptions', |
| 21 | 'Hooks', |
| 22 | 'Input', |
| 23 | 'Lang', |
| 24 | 'Loader', |
| 25 | 'Log', |
| 26 | 'Model', |
| 27 | 'Output', |
| 28 | 'Router', |
| 29 | 'Security', |
| 30 | 'URI', |
| 31 | 'Utf8' |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 32 | ); |
| 33 | |
| 34 | $ci_libraries = array( |
Andrey Andreev | 912831f | 2014-02-04 17:21:37 +0200 | [diff] [blame] | 35 | 'Calendar', |
| 36 | 'Cart', |
| 37 | 'Driver_Library', |
| 38 | 'Email', |
| 39 | 'Encrypt', |
| 40 | 'Encryption', |
| 41 | 'Form_validation', |
| 42 | 'Ftp', |
| 43 | 'Image_lib', |
| 44 | 'Javascript', |
| 45 | 'Migration', |
| 46 | 'Pagination', |
| 47 | 'Parser', |
| 48 | 'Profiler', |
| 49 | 'Table', |
| 50 | 'Trackback', |
| 51 | 'Typography', |
| 52 | 'Unit_test', |
| 53 | 'Upload', |
| 54 | 'User_agent', |
| 55 | 'Xmlrpc', |
| 56 | 'Zip' |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 57 | ); |
| 58 | |
Andrey Andreev | 912831f | 2014-02-04 17:21:37 +0200 | [diff] [blame] | 59 | $ci_drivers = array('Session', 'Cache'); |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 60 | |
| 61 | if (strpos($class, 'Mock_') === 0) |
| 62 | { |
Andrey Andreev | e24eed7 | 2012-11-02 23:33:45 +0200 | [diff] [blame] | 63 | $class = strtolower(str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class)); |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 64 | } |
| 65 | elseif (strpos($class, 'CI_') === 0) |
| 66 | { |
Andrey Andreev | e24eed7 | 2012-11-02 23:33:45 +0200 | [diff] [blame] | 67 | $subclass = substr($class, 3); |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 68 | |
| 69 | if (in_array($subclass, $ci_core)) |
| 70 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 71 | $dir = SYSTEM_PATH.'core'.DIRECTORY_SEPARATOR; |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 72 | $class = $subclass; |
| 73 | } |
| 74 | elseif (in_array($subclass, $ci_libraries)) |
| 75 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 76 | $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR; |
Alex Bilbie | d6d1150 | 2012-06-02 11:12:55 +0100 | [diff] [blame] | 77 | $class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass; |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 78 | } |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 79 | elseif (in_array($subclass, $ci_drivers)) |
| 80 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 81 | $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR; |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 82 | $class = $subclass; |
| 83 | } |
| 84 | elseif (in_array(($parent = strtok($subclass, '_')), $ci_drivers)) { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 85 | $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 86 | $class = $subclass; |
| 87 | } |
dchill42 | e9435dc | 2012-10-14 15:44:39 -0400 | [diff] [blame] | 88 | elseif (preg_match('/^CI_DB_(.+)_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 4) |
| 89 | { |
| 90 | $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; |
| 91 | $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR.'subdrivers'.DIRECTORY_SEPARATOR; |
| 92 | $file = $dir.$m[1].'_'.$m[2].'_'.$m[3].'.php'; |
| 93 | } |
Alex Bilbie | d6d1150 | 2012-06-02 11:12:55 +0100 | [diff] [blame] | 94 | elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3) |
Taufan Aditya | f4c6c9b | 2012-04-04 23:24:09 +0700 | [diff] [blame] | 95 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 96 | $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; |
Taufan Aditya | f4c6c9b | 2012-04-04 23:24:09 +0700 | [diff] [blame] | 97 | $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR; |
| 98 | $file = $dir.$m[1].'_'.$m[2].'.php'; |
| 99 | } |
| 100 | elseif (strpos($class, 'CI_DB') === 0) |
| 101 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 102 | $dir = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR; |
Taufan Aditya | 61ff054 | 2012-04-05 01:23:56 +0700 | [diff] [blame] | 103 | $file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php'; |
Taufan Aditya | f4c6c9b | 2012-04-04 23:24:09 +0700 | [diff] [blame] | 104 | } |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 105 | else |
| 106 | { |
| 107 | $class = strtolower($class); |
| 108 | } |
| 109 | } |
| 110 | |
Andrey Andreev | e24eed7 | 2012-11-02 23:33:45 +0200 | [diff] [blame] | 111 | $file = isset($file) ? $file : $dir.$class.'.php'; |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 112 | |
| 113 | if ( ! file_exists($file)) |
| 114 | { |
Andrey Andreev | bfbdf1e | 2014-02-12 18:10:15 +0200 | [diff] [blame] | 115 | return FALSE; |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | include_once($file); |
Andrey Andreev | 2eee870 | 2016-12-08 12:21:40 +0200 | [diff] [blame] | 119 | } |