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 | // |
| 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_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php |
| 12 | function autoload($class) |
| 13 | { |
| 14 | $class = (strpos($class, 'Mock_') === 0) ? str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class) : $class; |
| 15 | $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; |
| 16 | $file = $dir.strtolower($class).'.php'; |
| 17 | |
| 18 | if ( ! file_exists($file)) |
| 19 | { |
| 20 | $trace = debug_backtrace(); |
| 21 | |
| 22 | // If the autoload call came from `class_exists`, we skipped |
| 23 | // and return FALSE |
| 24 | if ($trace[2]['function'] == 'class_exists') |
| 25 | { |
| 26 | return FALSE; |
| 27 | } |
| 28 | |
| 29 | throw new InvalidArgumentException("Unable to load $class."); |
| 30 | } |
| 31 | |
| 32 | include_once($file); |
| 33 | } |