blob: 3d216da1fc208fa67b940dfe95a3eabf321dfd15 [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 Adityaac5373a2012-03-28 16:03:38 +07009// $mock_table = new Mock_Libraries_Table(); // Will load ./mocks/libraries/table.php
Andrey Andreevfc11dcc2012-06-04 16:39:19 +030010// $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php
Taufan Adityaac5373a2012-03-28 16:03:38 +070011// and so on...
Andrey Andreevfc11dcc2012-06-04 16:39:19 +030012function autoload($class)
Taufan Adityaca16c4f2012-03-28 15:15:30 +070013{
Taufan Adityaca16c4f2012-03-28 15:15:30 +070014 $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR;
Taufan Adityaac5373a2012-03-28 16:03:38 +070015
16 $ci_core = array(
17 'Benchmark', 'Config', 'Controller',
18 'Exceptions', 'Hooks', 'Input',
Andrey Andreev1f884d62012-11-09 19:57:14 +020019 'Lang', 'Loader', 'Log', 'Model',
Taufan Adityaac5373a2012-03-28 16:03:38 +070020 'Output', 'Router', 'Security',
21 'URI', 'Utf8',
22 );
23
24 $ci_libraries = array(
Taufan Aditya6c7526c2012-05-27 13:51:27 +070025 'Calendar', 'Cart', 'Driver_Library',
Taufan Adityaac5373a2012-03-28 16:03:38 +070026 'Email', 'Encrypt', 'Form_validation',
27 'Ftp', 'Image_lib', 'Javascript',
Andrey Andreev1f884d62012-11-09 19:57:14 +020028 'Migration', 'Pagination', 'Parser',
29 'Profiler', 'Table', 'Trackback',
30 'Typography', 'Unit_test', 'Upload',
31 'User_agent', 'Xmlrpc', 'Zip'
dchill4257486002012-07-31 09:39:53 -040032 );
33
34 $ci_drivers = array(
35 'Session',
Taufan Adityaac5373a2012-03-28 16:03:38 +070036 );
37
38 if (strpos($class, 'Mock_') === 0)
39 {
Andrey Andreeve24eed72012-11-02 23:33:45 +020040 $class = strtolower(str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class));
Taufan Adityaac5373a2012-03-28 16:03:38 +070041 }
42 elseif (strpos($class, 'CI_') === 0)
43 {
Andrey Andreeve24eed72012-11-02 23:33:45 +020044 $subclass = substr($class, 3);
Taufan Adityaac5373a2012-03-28 16:03:38 +070045
46 if (in_array($subclass, $ci_core))
47 {
dchill427ecc5cd2012-10-12 16:25:51 -040048 $dir = SYSTEM_PATH.'core'.DIRECTORY_SEPARATOR;
Taufan Adityaac5373a2012-03-28 16:03:38 +070049 $class = $subclass;
50 }
51 elseif (in_array($subclass, $ci_libraries))
52 {
dchill427ecc5cd2012-10-12 16:25:51 -040053 $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR;
Alex Bilbied6d11502012-06-02 11:12:55 +010054 $class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass;
Taufan Adityaac5373a2012-03-28 16:03:38 +070055 }
dchill4257486002012-07-31 09:39:53 -040056 elseif (in_array($subclass, $ci_drivers))
57 {
dchill427ecc5cd2012-10-12 16:25:51 -040058 $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR;
dchill4257486002012-07-31 09:39:53 -040059 $class = $subclass;
60 }
61 elseif (in_array(($parent = strtok($subclass, '_')), $ci_drivers)) {
dchill427ecc5cd2012-10-12 16:25:51 -040062 $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
dchill4257486002012-07-31 09:39:53 -040063 $class = $subclass;
64 }
dchill42e9435dc2012-10-14 15:44:39 -040065 elseif (preg_match('/^CI_DB_(.+)_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 4)
66 {
67 $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
68 $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR.'subdrivers'.DIRECTORY_SEPARATOR;
69 $file = $dir.$m[1].'_'.$m[2].'_'.$m[3].'.php';
70 }
Alex Bilbied6d11502012-06-02 11:12:55 +010071 elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3)
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070072 {
dchill427ecc5cd2012-10-12 16:25:51 -040073 $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070074 $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR;
75 $file = $dir.$m[1].'_'.$m[2].'.php';
76 }
77 elseif (strpos($class, 'CI_DB') === 0)
78 {
dchill427ecc5cd2012-10-12 16:25:51 -040079 $dir = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR;
Taufan Aditya61ff0542012-04-05 01:23:56 +070080 $file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php';
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070081 }
Taufan Adityaac5373a2012-03-28 16:03:38 +070082 else
83 {
84 $class = strtolower($class);
85 }
86 }
87
Andrey Andreeve24eed72012-11-02 23:33:45 +020088 $file = isset($file) ? $file : $dir.$class.'.php';
Taufan Adityaca16c4f2012-03-28 15:15:30 +070089
90 if ( ! file_exists($file))
91 {
92 $trace = debug_backtrace();
93
Alex Bilbied6d11502012-06-02 11:12:55 +010094 if ($trace[2]['function'] === 'class_exists' OR $trace[2]['function'] === 'file_exists')
Taufan Adityaca16c4f2012-03-28 15:15:30 +070095 {
Taufan Adityab8b7dbe2012-06-27 03:05:35 +070096 // If the autoload call came from `class_exists` or `file_exists`,
97 // we skipped and return FALSE
98 return FALSE;
99 }
100 elseif (($autoloader = spl_autoload_functions()) && end($autoloader) !== __FUNCTION__)
101 {
102 // If there was other custom autoloader, passed away
Taufan Adityaca16c4f2012-03-28 15:15:30 +0700103 return FALSE;
104 }
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300105
Taufan Aditya8f490da2012-06-27 03:31:22 +0700106 throw new InvalidArgumentException("Unable to load {$class}.");
Taufan Adityaca16c4f2012-03-28 15:15:30 +0700107 }
108
109 include_once($file);
dchill42e9435dc2012-10-14 15:44:39 -0400110}