blob: 11825de2c90f3083cf8e4eb723fa70df1d812452 [file] [log] [blame]
Taufan Adityaca16c4f2012-03-28 15:15:30 +07001<?php
2
Andrey Andreev2eee8702016-12-08 12:21:40 +02003// This autoloader provide convenient way to working with mock object
Taufan Adityaca16c4f2012-03-28 15:15:30 +07004// 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(
Andrey Andreev912831f2014-02-04 17:21:37 +020017 'Benchmark',
18 'Config',
19 'Controller',
20 'Exceptions',
21 'Hooks',
22 'Input',
23 'Lang',
24 'Loader',
25 'Log',
26 'Model',
27 'Output',
28 'Router',
29 'Security',
30 'URI',
31 'Utf8'
Taufan Adityaac5373a2012-03-28 16:03:38 +070032 );
33
34 $ci_libraries = array(
Andrey Andreev912831f2014-02-04 17:21:37 +020035 'Calendar',
36 'Cart',
37 'Driver_Library',
38 'Email',
39 'Encrypt',
40 'Encryption',
41 'Form_validation',
42 'Ftp',
43 'Image_lib',
44 'Javascript',
45 'Migration',
46 'Pagination',
47 'Parser',
48 'Profiler',
49 'Table',
50 'Trackback',
51 'Typography',
52 'Unit_test',
53 'Upload',
54 'User_agent',
55 'Xmlrpc',
56 'Zip'
dchill4257486002012-07-31 09:39:53 -040057 );
58
Andrey Andreev912831f2014-02-04 17:21:37 +020059 $ci_drivers = array('Session', 'Cache');
Taufan Adityaac5373a2012-03-28 16:03:38 +070060
61 if (strpos($class, 'Mock_') === 0)
62 {
Andrey Andreeve24eed72012-11-02 23:33:45 +020063 $class = strtolower(str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class));
Taufan Adityaac5373a2012-03-28 16:03:38 +070064 }
65 elseif (strpos($class, 'CI_') === 0)
66 {
Andrey Andreeve24eed72012-11-02 23:33:45 +020067 $subclass = substr($class, 3);
Taufan Adityaac5373a2012-03-28 16:03:38 +070068
69 if (in_array($subclass, $ci_core))
70 {
dchill427ecc5cd2012-10-12 16:25:51 -040071 $dir = SYSTEM_PATH.'core'.DIRECTORY_SEPARATOR;
Taufan Adityaac5373a2012-03-28 16:03:38 +070072 $class = $subclass;
73 }
74 elseif (in_array($subclass, $ci_libraries))
75 {
dchill427ecc5cd2012-10-12 16:25:51 -040076 $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR;
Alex Bilbied6d11502012-06-02 11:12:55 +010077 $class = ($subclass === 'Driver_Library') ? 'Driver' : $subclass;
Taufan Adityaac5373a2012-03-28 16:03:38 +070078 }
dchill4257486002012-07-31 09:39:53 -040079 elseif (in_array($subclass, $ci_drivers))
80 {
dchill427ecc5cd2012-10-12 16:25:51 -040081 $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$subclass.DIRECTORY_SEPARATOR;
dchill4257486002012-07-31 09:39:53 -040082 $class = $subclass;
83 }
84 elseif (in_array(($parent = strtok($subclass, '_')), $ci_drivers)) {
dchill427ecc5cd2012-10-12 16:25:51 -040085 $dir = SYSTEM_PATH.'libraries'.DIRECTORY_SEPARATOR.$parent.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
dchill4257486002012-07-31 09:39:53 -040086 $class = $subclass;
87 }
dchill42e9435dc2012-10-14 15:44:39 -040088 elseif (preg_match('/^CI_DB_(.+)_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 4)
89 {
90 $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
91 $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR.'subdrivers'.DIRECTORY_SEPARATOR;
92 $file = $dir.$m[1].'_'.$m[2].'_'.$m[3].'.php';
93 }
Alex Bilbied6d11502012-06-02 11:12:55 +010094 elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) === 3)
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070095 {
dchill427ecc5cd2012-10-12 16:25:51 -040096 $driver_path = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +070097 $dir = $driver_path.$m[1].DIRECTORY_SEPARATOR;
98 $file = $dir.$m[1].'_'.$m[2].'.php';
99 }
100 elseif (strpos($class, 'CI_DB') === 0)
101 {
dchill427ecc5cd2012-10-12 16:25:51 -0400102 $dir = SYSTEM_PATH.'database'.DIRECTORY_SEPARATOR;
Taufan Aditya61ff0542012-04-05 01:23:56 +0700103 $file = $dir.str_replace(array('CI_DB','active_record'), array('DB', 'active_rec'), $subclass).'.php';
Taufan Adityaf4c6c9b2012-04-04 23:24:09 +0700104 }
Taufan Adityaac5373a2012-03-28 16:03:38 +0700105 else
106 {
107 $class = strtolower($class);
108 }
109 }
110
Andrey Andreeve24eed72012-11-02 23:33:45 +0200111 $file = isset($file) ? $file : $dir.$class.'.php';
Taufan Adityaca16c4f2012-03-28 15:15:30 +0700112
113 if ( ! file_exists($file))
114 {
Andrey Andreevbfbdf1e2014-02-12 18:10:15 +0200115 return FALSE;
Taufan Adityaca16c4f2012-03-28 15:15:30 +0700116 }
117
118 include_once($file);
Andrey Andreev2eee8702016-12-08 12:21:40 +0200119}