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