blob: f1bdb5d6f4086669767d9a2c6106027df7d33ef8 [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 }
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070056 elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) == 3)
57 {
58 $driver_path = BASEPATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
59 $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR;
60 $file = $dir.$m[1].'_'.$m[2].'.php';
61 }
62 elseif (strpos($class, 'CI_DB') === 0)
63 {
64 $dir = BASEPATH.'database'.DIRECTORY_SEPARATOR;
Taufan Aditya61ff0542012-04-05 01:23:56 +070065 $file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php';
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070066 }
Taufan Adityaac5373a2012-03-28 16:03:38 +070067 else
68 {
69 $class = strtolower($class);
70 }
71 }
72
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070073 $file = (isset($file)) ? $file : $dir.$class.'.php';
Taufan Adityaca16c4f2012-03-28 15:15:30 +070074
75 if ( ! file_exists($file))
76 {
77 $trace = debug_backtrace();
78
Taufan Adityaac5373a2012-03-28 16:03:38 +070079 // If the autoload call came from `class_exists` or `file_exists`,
80 // we skipped and return FALSE
81 if ($trace[2]['function'] == 'class_exists' OR $trace[2]['function'] == 'file_exists')
Taufan Adityaca16c4f2012-03-28 15:15:30 +070082 {
83 return FALSE;
84 }
Taufan Aditya61ff0542012-04-05 01:23:56 +070085
Taufan Adityaca16c4f2012-03-28 15:15:30 +070086 throw new InvalidArgumentException("Unable to load $class.");
87 }
88
89 include_once($file);
90}