Skip to content

Commit e0b1f77

Browse files
committed
implement proper autoloader
1 parent 3c65f9e commit e0b1f77

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

lib/WebDriver/ClassLoader.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,61 @@
2121

2222
namespace WebDriver;
2323

24-
/**
25-
* @TODO implement the class loader!
26-
*/
27-
require_once(__DIR__ . '/__init__.php');
28-
2924
/**
3025
* WebDriver\ClassLoader (autoloader) class
3126
*
3227
* @package WebDriver
3328
*/
3429
final class ClassLoader
3530
{
31+
/**
32+
* Load class
33+
*
34+
* @param string $class Class name
35+
*/
36+
public static function loadClass($class)
37+
{
38+
$file = strpos($class, '\\') !== false
39+
? str_replace('\\', DIRECTORY_SEPARATOR, $class)
40+
: str_replace('_', DIRECTORY_SEPARATOR, $class);
41+
42+
$path = dirname(__DIR__) . DIRECTORY_SEPARATOR . $file . '.php';
43+
44+
if (file_exists($path)) {
45+
include_once $path;
46+
}
47+
}
48+
49+
/**
50+
* Autoloader
51+
*
52+
* @param string $class Class name
53+
*/
54+
public static function autoload($class)
55+
{
56+
try {
57+
self::loadClass($class);
58+
} catch (Exception $e) {
59+
}
60+
}
61+
}
62+
63+
// Note: only one __autoload per PHP instance
64+
if(function_exists('spl_autoload_register'))
65+
{
66+
// use the SPL autoload stack
67+
spl_autoload_register(array('WebDriver\ClassLoader', 'autoload'));
68+
69+
// preserve any existing __autoload
70+
if(function_exists('__autoload'))
71+
{
72+
spl_autoload_register('__autoload');
73+
}
74+
}
75+
else
76+
{
77+
function __autoload($class)
78+
{
79+
ClassLoader::autoload($class);
80+
}
3681
}

0 commit comments

Comments
 (0)