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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| <?php
namespace MyApp\Common;
class ClassLoader {
/**
* @var array $_directories Folder list where to find the class
*/
protected $_directories = array();
/**
* @var string $_namespace Namespace name
*/
protected $_namespace = '';
/**
* @var string $_namespaceSeparator Namespace separator
*/
protected $_namespaceSeparator = '\\';
/**
* @var string $_extension File extension
*/
protected $_extension = '.php';
/**
* Class constructor
*
* @access public
* @param string $namespace
* @param string|array $directories
*/
public function __construct($namespace = '', $directories = array()) {
$this->setNamespace($namespace);
$this->addDirectories($directories);
}
/**
* Sets the list of include path
*
* @access public
* @param string|array $directories
*/
public function addDirectories($directories) {
if (!is_array($directories)) {
$directories = array($directories);
}
$this->_directories = array_merge($this->_directories, $directories);
}
/**
* Sets the namespace used by classes
* of this ClassLoader
*
* @access public
* @param string $namespace
*/
public function setNamespace($namespace) {
$this->_namespace = trim((string) $namespace);
return $this;
}
/**
* Gets the namespace used by classes
* of this ClassLoader
*
* @access public
* @return string
*/
public function getNamespace() {
return $this->_namespace;
}
/**
* Sets the namespace separator used by classes
* in the namespace of this ClassLoader.
*
* @access public
* @param string $separator
*/
public function setNamespaceSeparator($separator) {
if (!empty($separator) && is_string($separator)) {
$this->_namespaceSeparator = $separator;
}
}
/**
* Gets the namespace separator used by classes
* in the namespace of this ClassLoader.
*
* @access public
* @return string
*/
public function getNamespaceSeparator() {
return $this->_namespaceSeparator;
}
/**
* Sets the file extension of class files
* in the namespace of this ClassLoader.
*
* @access public
* @param string $extension
* @throws \InvalidArgumentException
*/
public function setExtension($extension) {
if (!empty($extension) && is_string($extension)) {
$this->_extension = $extension;
}
}
/**
* Gets the file extension of class files
* in the namespace of this ClassLoader.
*
* @access public
* @return string
*/
public function getExtension() {
return $this->_extension;
}
/**
* Register ClassLoader function
* as __autoload() implementation
*
* @access public
* @param boolean $throw Specifies whether spl_autoload_register() should throw exceptions when the autoload_function cannot be registered.
* @param boolean $prepend If true, spl_autoload_register() will prepend the autoloader on the autoload stack instead of appending it
* @return void
*/
public function register($throw = TRUE, $prepend = FALSE) {
spl_autoload_register(array($this, 'loadClass'), $throw, $prepend);
}
/**
* Unregister ClassLoader function
* fromn the __autoload() stack
*
* @access public
* @return void
*/
public function unregister() {
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @access public
* @param string $classname
* @return boolean
*/
public function loadClass($classname) {
if (class_exists($classname, FALSE) || interface_exists($classname, FALSE)) {
return TRUE;
} else if (!$this->canLoadClass($classname)) {
return FALSE;
}
$filename = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $classname) . $this->_extension;
if (!isset($this->_directories[0])) {
@include_once $filename;
} else {
foreach(array_reverse($this->_directories) as $directory) {
if (file_exists($directory . DIRECTORY_SEPARATOR . $filename)) {
@include_once $directory . DIRECTORY_SEPARATOR . $filename;
break;
}
}
}
return TRUE;
}
/**
* Return TRUE if this ClassLoader can load the class
*
* @access public
* @param string $classname
* @return boolean
*/
public function canLoadClass($classname) {
return (bool) ($this->_namespace == '' || substr($classname, 0, strlen($this->_namespace)) == $this->_namespace);
}
}
?> |
Partager