Spaces:
Sleeping
Sleeping
File size: 4,169 Bytes
07c3cdd | 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | <?php
/**
* Class Autoloader
*
* Adaptation of Alchemy/Component/ClassLoader on https://github.com/eriknyk/ClassLoader for php 5.2.x
*
* ""SplClassLoader implementation that implements the technical interoperability
* standards for PHP 5.3 namespaces and class names. ""
*
* Singleton Class
*
* @version 1.0
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com>
* @link https://github.com/eriknyk/phpalchemy
* @copyright Copyright 2012 Erik Amaru Ortiz
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
class Autoloader
{
/**
* Holds singleton object
*
* @var ClassLoader
*/
protected static $instance = null;
protected $includePaths = array();
protected $includeClassPaths = array();
protected $relativeIncludePaths = array();
/**
* Creates a new SplClassLoader and installs the class on the SPL autoload stack
*
* @param string $ns The namespace to use.
*/
public function __construct()
{
defined('DS') || define('DS', DIRECTORY_SEPARATOR);
spl_autoload_register(array($this, 'loadClass'));
}
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Gets the base include path for all class files in the namespace of this class loader.
*
* @return string $includePath
*/
public function getIncludePaths()
{
return $this->includePaths;
}
/**
* register a determinated namespace and its include path to find it.
*
* @param string $namespace namespace of a class given
* @param string $includePath path where the class exists
* @param string $excludeNsPart contais a part of class to exclude from classname passed by SPL hanlder
*/
public function register($namespace, $includePath)
{
$this->includePaths[$namespace] = rtrim($includePath, DS) . DS;
}
public function registerClass($classname, $includeFile)
{
$includeFile = strpos($includeFile, '.php') !== false ? $includeFile : $includeFile . '.php';
$classname = strtolower($classname);
if (! file_exists($includeFile)) {
throw new Exception("Error, Autoloader can't register a non exists file: " . $includeFile);
}
if (strpos($classname, '*') !== false) {
$tmp = str_replace('*', '(.+)', $classname);
$this->relativeIncludePaths[$tmp] = $includeFile;
return;
}
if (array_key_exists($classname, $this->includeClassPaths)) {
throw new Exception("Error, class '%s' is already registered in Autoloader.");
}
$this->includeClassPaths[$classname] = strpos($includeFile, '.php') !== false ? $includeFile : $includeFile . '.php';
}
/**
* Uninstalls this class loader from the SPL autoloader stack.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $className The name of the class to load.
* @return void
*/
protected function loadClass($class)
{
$className = strtolower($class);
if (array_key_exists($className, $this->includeClassPaths)) {
require_once $this->includeClassPaths[$className];
return true;
} else {
foreach ($this->relativeIncludePaths as $relClassname => $includeFile) {
if ($r = preg_match('/'.$relClassname.'/', $className, $match)) {
require_once $includeFile;
return true;
}
}
}
$filename = str_replace('_', DS, $class) . '.php';
foreach ($this->includePaths as $namespace => $includePath) {
//var_dump($includePath . $filename);
if (file_exists($includePath . $filename)) {
require_once $includePath . $filename;
return true;
}
}
return false;
}
}
|