Make Predis\Autoloader faster.

This commit is contained in:
Daniele Alessandri
2012-03-18 18:00:00 +01:00
parent 608c48146f
commit b37d072b68
+9 -10
View File
@@ -15,19 +15,22 @@ namespace Predis;
* Implements a lightweight PSR-0 compliant autoloader.
*
* @author Eric Naeseth <eric@thumbtack.com>
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class Autoloader
{
private $baseDir;
private $directory;
private $prefix;
private $prefixLength;
/**
* @param string $baseDirectory Base directory where the source files are located.
*/
public function __construct($baseDirectory = null)
public function __construct($baseDirectory = __DIR__)
{
$this->baseDir = $baseDirectory ?: dirname(__FILE__);
$this->directory = $baseDirectory;
$this->prefix = __NAMESPACE__ . '\\';
$this->prefixLength = strlen($this->prefix);
}
/**
@@ -45,13 +48,9 @@ class Autoloader
*/
public function autoload($className)
{
if (0 !== strpos($className, $this->prefix)) {
return;
if (0 === strpos($className, $this->prefix)) {
$parts = explode('\\', substr($className, $this->prefixLength));
require($this->directory.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts).'.php');
}
$relativeClassName = substr($className, strlen($this->prefix));
$classNameParts = explode('\\', $relativeClassName);
require_once $this->baseDir.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $classNameParts).'.php';
}
}