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 | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame^] | 9 | // include_once('Mock_Core_Loader') // Will load ./mocks/core/loader.php |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 10 | // $mock_table = new Mock_Libraries_Table(); // Will load ./mocks/libraries/table.php |
| 11 | // $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php |
| 12 | // and so on... |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 13 | function autoload($class) |
| 14 | { |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 15 | $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 16 | |
| 17 | $ci_core = array( |
| 18 | 'Benchmark', 'Config', 'Controller', |
| 19 | 'Exceptions', 'Hooks', 'Input', |
| 20 | 'Lang', 'Loader', 'Model', |
| 21 | 'Output', 'Router', 'Security', |
| 22 | 'URI', 'Utf8', |
| 23 | ); |
| 24 | |
| 25 | $ci_libraries = array( |
| 26 | 'Calendar', 'Cart', 'Driver', |
| 27 | 'Email', 'Encrypt', 'Form_validation', |
| 28 | 'Ftp', 'Image_lib', 'Javascript', |
| 29 | 'Log', 'Migration', 'Pagination', |
| 30 | 'Parser', 'Profiler', 'Session', |
| 31 | 'Table', 'Trackback', 'Typography', |
| 32 | 'Unit_test', 'Upload', 'User_agent', |
| 33 | 'Xmlrpc', 'Zip', |
| 34 | ); |
| 35 | |
| 36 | if (strpos($class, 'Mock_') === 0) |
| 37 | { |
| 38 | $class = str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class); |
| 39 | $class = strtolower($class); |
| 40 | } |
| 41 | elseif (strpos($class, 'CI_') === 0) |
| 42 | { |
| 43 | $fragments = explode('_', $class, 2); |
| 44 | $subclass = next($fragments); |
| 45 | |
| 46 | if (in_array($subclass, $ci_core)) |
| 47 | { |
| 48 | $dir = BASEPATH.'core'.DIRECTORY_SEPARATOR; |
| 49 | $class = $subclass; |
| 50 | } |
| 51 | elseif (in_array($subclass, $ci_libraries)) |
| 52 | { |
| 53 | $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR; |
| 54 | $class = $subclass; |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | $class = strtolower($class); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | $file = $dir.$class.'.php'; |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 63 | |
| 64 | if ( ! file_exists($file)) |
| 65 | { |
| 66 | $trace = debug_backtrace(); |
| 67 | |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 68 | // If the autoload call came from `class_exists` or `file_exists`, |
| 69 | // we skipped and return FALSE |
| 70 | if ($trace[2]['function'] == 'class_exists' OR $trace[2]['function'] == 'file_exists') |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 71 | { |
| 72 | return FALSE; |
| 73 | } |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 74 | var_dump($file);die; |
Taufan Aditya | ca16c4f | 2012-03-28 15:15:30 +0700 | [diff] [blame] | 75 | |
| 76 | throw new InvalidArgumentException("Unable to load $class."); |
| 77 | } |
| 78 | |
| 79 | include_once($file); |
| 80 | } |