blob: 88d016bba3969db9ea8502f363b2f7293f1d44a3 [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',
19 'Lang', 'Loader', 'Model',
20 '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',
28 'Log', 'Migration', 'Pagination',
dchill4257486002012-07-31 09:39:53 -040029 'Parser', 'Profiler', 'Table',
30 'Trackback', 'Typography', 'Unit_test',
31 'Upload', 'User_agent', 'Xmlrpc',
32 'Zip',
33 );
34
35 $ci_drivers = array(
36 'Session',
Taufan Adityaac5373a2012-03-28 16:03:38 +070037 );
38
39 if (strpos($class, 'Mock_') === 0)
40 {
41 $class = str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class);
42 $class = strtolower($class);
43 }
44 elseif (strpos($class, 'CI_') === 0)
45 {
46 $fragments = explode('_', $class, 2);
47 $subclass = next($fragments);
48
49 if (in_array($subclass, $ci_core))
50 {
51 $dir = BASEPATH.'core'.DIRECTORY_SEPARATOR;
52 $class = $subclass;
53 }
54 elseif (in_array($subclass, $ci_libraries))
55 {
56 $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR;
Alex Bilbied6d11502012-06-02 11:12:55 +010057 $class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass;
Taufan Adityaac5373a2012-03-28 16:03:38 +070058 }
dchill4257486002012-07-31 09:39:53 -040059 elseif (in_array($subclass, $ci_drivers))
60 {
61 $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR;
62 $class = $subclass;
63 }
64 elseif (in_array(($parent = strtok($subclass, '_')), $ci_drivers)) {
65 $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
66 $class = $subclass;
67 }
Alex Bilbied6d11502012-06-02 11:12:55 +010068 elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3)
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070069 {
70 $driver_path = BASEPATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
71 $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR;
72 $file = $dir.$m[1].'_'.$m[2].'.php';
73 }
74 elseif (strpos($class, 'CI_DB') === 0)
75 {
76 $dir = BASEPATH.'database'.DIRECTORY_SEPARATOR;
Taufan Aditya61ff0542012-04-05 01:23:56 +070077 $file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php';
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070078 }
Taufan Adityaac5373a2012-03-28 16:03:38 +070079 else
80 {
81 $class = strtolower($class);
82 }
83 }
84
Taufan Adityab8b7dbe2012-06-27 03:05:35 +070085 $file = (isset($file)) ? $file : $dir.$class.'.php';
Taufan Adityaca16c4f2012-03-28 15:15:30 +070086
87 if ( ! file_exists($file))
88 {
89 $trace = debug_backtrace();
90
Alex Bilbied6d11502012-06-02 11:12:55 +010091 if ($trace[2]['function'] === 'class_exists' OR $trace[2]['function'] === 'file_exists')
Taufan Adityaca16c4f2012-03-28 15:15:30 +070092 {
Taufan Adityab8b7dbe2012-06-27 03:05:35 +070093 // If the autoload call came from `class_exists` or `file_exists`,
94 // we skipped and return FALSE
95 return FALSE;
96 }
97 elseif (($autoloader = spl_autoload_functions()) && end($autoloader) !== __FUNCTION__)
98 {
99 // If there was other custom autoloader, passed away
Taufan Adityaca16c4f2012-03-28 15:15:30 +0700100 return FALSE;
101 }
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300102
Taufan Aditya8f490da2012-06-27 03:31:22 +0700103 throw new InvalidArgumentException("Unable to load {$class}.");
Taufan Adityaca16c4f2012-03-28 15:15:30 +0700104 }
105
106 include_once($file);
dchill4257486002012-07-31 09:39:53 -0400107}