Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 1 | <?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 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( |
| 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 Aditya | 6c7526c | 2012-05-27 13:51:27 +0700 | [diff] [blame] | 25 | 'Calendar', 'Cart', 'Driver_Library', |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 26 | 'Email', 'Encrypt', 'Form_validation', |
| 27 | 'Ftp', 'Image_lib', 'Javascript', |
| 28 | 'Log', 'Migration', 'Pagination', |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 29 | 'Parser', 'Profiler', 'Table', |
| 30 | 'Trackback', 'Typography', 'Unit_test', |
| 31 | 'Upload', 'User_agent', 'Xmlrpc', |
| 32 | 'Zip', |
| 33 | ); |
| 34 | |
| 35 | $ci_drivers = array( |
| 36 | 'Session', |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 37 | ); |
| 38 | |
| 39 | if (strpos($class, 'Mock_') === 0) |
| 40 | { |
| 41 | $class = str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class); |
| 42 | $class = strtolower($class); |
| 43 | } |
| 44 | elseif (strpos($class, 'CI_') === 0) |
| 45 | { |
| 46 | $fragments = explode('_', $class, 2); |
| 47 | $subclass = next($fragments); |
| 48 | |
| 49 | if (in_array($subclass, $ci_core)) |
| 50 | { |
| 51 | $dir = BASEPATH.'core'.DIRECTORY_SEPARATOR; |
| 52 | $class = $subclass; |
| 53 | } |
| 54 | elseif (in_array($subclass, $ci_libraries)) |
| 55 | { |
| 56 | $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR; |
Alex Bilbie | d6d1150 | 2012-06-02 11:12:55 +0100 | [diff] [blame] | 57 | $class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass; |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 58 | } |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 59 | elseif (in_array($subclass, $ci_drivers)) |
| 60 | { |
| 61 | $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR; |
| 62 | $class = $subclass; |
| 63 | } |
| 64 | elseif (in_array(($parent = strtok($subclass, '_')), $ci_drivers)) { |
| 65 | $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; |
| 66 | $class = $subclass; |
| 67 | } |
Alex Bilbie | d6d1150 | 2012-06-02 11:12:55 +0100 | [diff] [blame] | 68 | 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] | 69 | { |
| 70 | $driver_path = BASEPATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR; |
| 71 | $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR; |
| 72 | $file = $dir.$m[1].'_'.$m[2].'.php'; |
| 73 | } |
| 74 | elseif (strpos($class, 'CI_DB') === 0) |
| 75 | { |
| 76 | $dir = BASEPATH.'database'.DIRECTORY_SEPARATOR; |
Taufan Aditya | 61ff054 | 2012-04-05 01:23:56 +0700 | [diff] [blame] | 77 | $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] | 78 | } |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 79 | else |
| 80 | { |
| 81 | $class = strtolower($class); |
| 82 | } |
| 83 | } |
| 84 | |
Taufan Aditya | b8b7dbe | 2012-06-27 03:05:35 +0700 | [diff] [blame] | 85 | $file = (isset($file)) ? $file : $dir.$class.'.php'; |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 86 | |
| 87 | if ( ! file_exists($file)) |
| 88 | { |
| 89 | $trace = debug_backtrace(); |
| 90 | |
Alex Bilbie | d6d1150 | 2012-06-02 11:12:55 +0100 | [diff] [blame] | 91 | if ($trace[2]['function'] === 'class_exists' OR $trace[2]['function'] === 'file_exists') |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 92 | { |
Taufan Aditya | b8b7dbe | 2012-06-27 03:05:35 +0700 | [diff] [blame] | 93 | // If the autoload call came from `class_exists` or `file_exists`, |
| 94 | // we skipped and return FALSE |
| 95 | return FALSE; |
| 96 | } |
| 97 | elseif (($autoloader = spl_autoload_functions()) && end($autoloader) !== __FUNCTION__) |
| 98 | { |
| 99 | // If there was other custom autoloader, passed away |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 100 | return FALSE; |
| 101 | } |
Andrey Andreev | fc11dcc | 2012-06-04 16:39:19 +0300 | [diff] [blame] | 102 | |
Taufan Aditya | 8f490da | 2012-06-27 03:31:22 +0700 | [diff] [blame] | 103 | throw new InvalidArgumentException("Unable to load {$class}."); |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | include_once($file); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 107 | } |