blob: dd59292068ffdf4068affedda09b0d91ade1855f [file] [log] [blame]
Taufan Adityaca16c4f2012-03-28 15:15:30 +07001<?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 Adityae1dc9ea2012-03-28 16:49:49 +07009// include_once('Mock_Core_Loader') // Will load ./mocks/core/loader.php
Taufan Adityaac5373a2012-03-28 16:03:38 +070010// $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 Adityaca16c4f2012-03-28 15:15:30 +070013function autoload($class)
14{
Taufan Adityaca16c4f2012-03-28 15:15:30 +070015 $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR;
Taufan Adityaac5373a2012-03-28 16:03:38 +070016
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 Adityaca16c4f2012-03-28 15:15:30 +070063
64 if ( ! file_exists($file))
65 {
66 $trace = debug_backtrace();
67
Taufan Adityaac5373a2012-03-28 16:03:38 +070068 // 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 Adityaca16c4f2012-03-28 15:15:30 +070071 {
72 return FALSE;
73 }
Taufan Adityaca16c4f2012-03-28 15:15:30 +070074
75 throw new InvalidArgumentException("Unable to load $class.");
76 }
77
78 include_once($file);
79}