summaryrefslogtreecommitdiffstats
path: root/tests/mocks/autoloader.php
blob: 22448139e6af3626e676e773bd1d3dafc4acdc03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php

// This autoloader provide convinient way to working with mock object
// make the test looks natural. This autoloader support cascade file loading as well
// within mocks directory.
//
// Prototype :
//
// include_once('Mock_Core_Loader') 					// Will load ./mocks/core/loader.php
// $mock_table = new Mock_Libraries_Table(); 			// Will load ./mocks/libraries/table.php
// $mock_database_driver = new Mock_Database_Driver();	// Will load ./mocks/database/driver.php 
// and so on...
function autoload($class) 
{
	$dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR;

	$ci_core = array(
		'Benchmark', 'Config', 'Controller',
		'Exceptions', 'Hooks', 'Input',
		'Lang', 'Loader', 'Model',
		'Output', 'Router', 'Security',
		'URI', 'Utf8',
	);

	$ci_libraries = array(
		'Calendar', 'Cart', 'Driver',
		'Email', 'Encrypt', 'Form_validation',
		'Ftp', 'Image_lib', 'Javascript',
		'Log', 'Migration', 'Pagination',
		'Parser', 'Profiler', 'Session',
		'Table', 'Trackback', 'Typography',
		'Unit_test', 'Upload', 'User_agent',
		'Xmlrpc', 'Zip',
	);

	if (strpos($class, 'Mock_') === 0)
	{
		$class = str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class);
		$class = strtolower($class);
	}
	elseif (strpos($class, 'CI_') === 0)
	{
		$fragments = explode('_', $class, 2);
		$subclass = next($fragments);

		if (in_array($subclass, $ci_core))
		{
			$dir = BASEPATH.'core'.DIRECTORY_SEPARATOR;
			$class = $subclass;
		}
		elseif (in_array($subclass, $ci_libraries))
		{
			$dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR;
			$class = $subclass;
		}
		elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) == 3)
		{
			$driver_path = BASEPATH.'database'.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR;
			$dir = $driver_path.$m[1].DIRECTORY_SEPARATOR;
			$file = $dir.$m[1].'_'.$m[2].'.php';
		}
		elseif (strpos($class, 'CI_DB') === 0)
		{
			$dir = BASEPATH.'database'.DIRECTORY_SEPARATOR;
			$file = $dir.str_replace('CI_DB', 'DB', $subclass).'.php';
		}
		else
		{
			$class = strtolower($class);
		}
	}

	$file = (isset($file)) ? $file : $dir.$class.'.php';

	if ( ! file_exists($file))
	{
		$trace = debug_backtrace();

		// If the autoload call came from `class_exists` or `file_exists`, 
		// we skipped and return FALSE
		if ($trace[2]['function'] == 'class_exists' OR $trace[2]['function'] == 'file_exists')
		{
			return FALSE;
		}
		var_dump($file);
	    throw new InvalidArgumentException("Unable to load $class.");
	}

	include_once($file);
}