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