Created
August 18, 2009 08:34
-
-
Save lox/169617 to your computer and use it in GitHub Desktop.
Generic SPL class loader for PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generic SPL class loader | |
* @license http://opensource.org/licenses/mit-license.php MIT License | |
* @author Lachlan Donald <[email protected]> | |
*/ | |
class ClassLoader | |
{ | |
private $_paths = array(); | |
/** | |
* Registers this class as an SPL class loader. | |
*/ | |
public function register() | |
{ | |
spl_autoload_register(array($this, 'loadClass')); | |
return $this; | |
} | |
/** | |
* Unregisters this class as an SPL class loader, does not attempt to | |
* unregister include_path entries. | |
*/ | |
public function unregister() | |
{ | |
spl_autoload_unregister(array($this, 'loadClass')); | |
return $this; | |
} | |
/** | |
* SPL autoload function, loads a class file based on the class name. | |
* | |
* @param string | |
*/ | |
public function loadClass($className) | |
{ | |
if (class_exists($className, false) || interface_exists($className, false)) | |
{ | |
return false; | |
} | |
$classFile = preg_replace('#_#', '/', $className).'.php'; | |
foreach ($this->_paths as $path) | |
{ | |
$classPath = "$path/$classFile"; | |
if (file_exists($classPath)) { | |
require $classPath; | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Prepends one or more items to the include path of the class loader and | |
* the php include path. | |
* @param mixed $items Path or paths as string or array | |
*/ | |
public function includePaths($path) | |
{ | |
$paths = is_array($path) ? $path : array($path); | |
$this->_paths = array_merge($paths,$this->_paths); | |
return $this; | |
} | |
/** | |
* Exports the classloader path into the PHP system include path | |
*/ | |
public function export() | |
{ | |
$systemPaths = explode(PATH_SEPARATOR, get_include_path()); | |
set_include_path(implode(PATH_SEPARATOR, | |
array_merge($systemPaths,$this->_paths))); | |
return $this; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment