blob: be1c2220c4146cb54595667d0d2a2037fd1282e3 [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',
29 'Parser', 'Profiler', 'Session',
30 'Table', 'Trackback', 'Typography',
31 'Unit_test', 'Upload', 'User_agent',
32 'Xmlrpc', 'Zip',
33 );
34
35 if (strpos($class, 'Mock_') === 0)
36 {
37 $class = str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class);
38 $class = strtolower($class);
39 }
40 elseif (strpos($class, 'CI_') === 0)
41 {
42 $fragments = explode('_', $class, 2);
43 $subclass = next($fragments);
44
45 if (in_array($subclass, $ci_core))
46 {
47 $dir = BASEPATH.'core'.DIRECTORY_SEPARATOR;
48 $class = $subclass;
49 }
50 elseif (in_array($subclass, $ci_libraries))
51 {
52 $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR;
Alex Bilbied6d11502012-06-02 11:12:55 +010053 $class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass;
Taufan Adityaac5373a2012-03-28 16:03:38 +070054 }
Alex Bilbied6d11502012-06-02 11:12:55 +010055 elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3)
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070056 {
57 $driver_path = BASEPATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
58 $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR;
59 $file = $dir.$m[1].'_'.$m[2].'.php';
60 }
61 elseif (strpos($class, 'CI_DB') === 0)
62 {
63 $dir = BASEPATH.'database'.DIRECTORY_SEPARATOR;
Taufan Aditya61ff0542012-04-05 01:23:56 +070064 $file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php';
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070065 }
Taufan Adityaac5373a2012-03-28 16:03:38 +070066 else
67 {
68 $class = strtolower($class);
69 }
70 }
71
Taufan Adityab8b7dbe2012-06-27 03:05:35 +070072 $file = (isset($file)) ? $file : $dir.$class.'.php';
Taufan Adityaca16c4f2012-03-28 15:15:30 +070073
74 if ( ! file_exists($file))
75 {
76 $trace = debug_backtrace();
77
Alex Bilbied6d11502012-06-02 11:12:55 +010078 if ($trace[2]['function'] === 'class_exists' OR $trace[2]['function'] === 'file_exists')
Taufan Adityaca16c4f2012-03-28 15:15:30 +070079 {
Taufan Adityab8b7dbe2012-06-27 03:05:35 +070080 // If the autoload call came from `class_exists` or `file_exists`,
81 // we skipped and return FALSE
82 return FALSE;
83 }
84 elseif (($autoloader = spl_autoload_functions()) && end($autoloader) !== __FUNCTION__)
85 {
86 // If there was other custom autoloader, passed away
Taufan Adityaca16c4f2012-03-28 15:15:30 +070087 return FALSE;
88 }
Andrey Andreevfc11dcc2012-06-04 16:39:19 +030089
Taufan Aditya8f490da2012-06-27 03:31:22 +070090 throw new InvalidArgumentException("Unable to load {$class}.");
Taufan Adityaca16c4f2012-03-28 15:15:30 +070091 }
92
93 include_once($file);
94}