blob: 88070e508928bfc81c1a6ef76718f5b5fd1d48f8 [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// include_once('Mock_Core_Common') // Will load ./mocks/core/common.php
10// $mock_loader = new Mock_Core_Loader(); // Will load ./mocks/core/loader.php
11// $mock_table = new Mock_Libraries_Table(); // Will load ./mocks/libraries/table.php
12// $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php
13// and so on...
Taufan Adityaca16c4f2012-03-28 15:15:30 +070014function autoload($class)
15{
Taufan Adityaca16c4f2012-03-28 15:15:30 +070016 $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR;
Taufan Adityaac5373a2012-03-28 16:03:38 +070017
18 $ci_core = array(
19 'Benchmark', 'Config', 'Controller',
20 'Exceptions', 'Hooks', 'Input',
21 'Lang', 'Loader', 'Model',
22 'Output', 'Router', 'Security',
23 'URI', 'Utf8',
24 );
25
26 $ci_libraries = array(
27 'Calendar', 'Cart', 'Driver',
28 'Email', 'Encrypt', 'Form_validation',
29 'Ftp', 'Image_lib', 'Javascript',
30 'Log', 'Migration', 'Pagination',
31 'Parser', 'Profiler', 'Session',
32 'Table', 'Trackback', 'Typography',
33 'Unit_test', 'Upload', 'User_agent',
34 'Xmlrpc', 'Zip',
35 );
36
37 if (strpos($class, 'Mock_') === 0)
38 {
39 $class = str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class);
40 $class = strtolower($class);
41 }
42 elseif (strpos($class, 'CI_') === 0)
43 {
44 $fragments = explode('_', $class, 2);
45 $subclass = next($fragments);
46
47 if (in_array($subclass, $ci_core))
48 {
49 $dir = BASEPATH.'core'.DIRECTORY_SEPARATOR;
50 $class = $subclass;
51 }
52 elseif (in_array($subclass, $ci_libraries))
53 {
54 $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR;
55 $class = $subclass;
56 }
57 else
58 {
59 $class = strtolower($class);
60 }
61 }
62
63 $file = $dir.$class.'.php';
Taufan Adityaca16c4f2012-03-28 15:15:30 +070064
65 if ( ! file_exists($file))
66 {
67 $trace = debug_backtrace();
68
Taufan Adityaac5373a2012-03-28 16:03:38 +070069 // If the autoload call came from `class_exists` or `file_exists`,
70 // we skipped and return FALSE
71 if ($trace[2]['function'] == 'class_exists' OR $trace[2]['function'] == 'file_exists')
Taufan Adityaca16c4f2012-03-28 15:15:30 +070072 {
73 return FALSE;
74 }
Taufan Adityaac5373a2012-03-28 16:03:38 +070075 var_dump($file);die;
Taufan Adityaca16c4f2012-03-28 15:15:30 +070076
77 throw new InvalidArgumentException("Unable to load $class.");
78 }
79
80 include_once($file);
81}