mirror of
https://github.com/predis/predis.git
synced 2026-08-26 06:39:54 +00:00
94a2ddeca9
This makes it easier to use Predis in scripts and projects that do not already include as PSR-0 autoloader. Signed-off-by: Eric Naeseth <eric@thumbtack.com>
34 lines
847 B
PHP
34 lines
847 B
PHP
<?php
|
|
|
|
namespace Predis;
|
|
|
|
class Autoloader {
|
|
private $base_directory;
|
|
private $prefix;
|
|
|
|
public function __construct($base_directory=NULL) {
|
|
$this->base_directory = $base_directory ?: dirname(__FILE__);
|
|
$this->prefix = __NAMESPACE__ . '\\';
|
|
}
|
|
|
|
public static function register() {
|
|
spl_autoload_register(array(new self, 'autoload'));
|
|
}
|
|
|
|
public function autoload($class_name) {
|
|
if (0 !== strpos($class_name, $this->prefix)) {
|
|
return;
|
|
}
|
|
|
|
$relative_class_name = substr($class_name, strlen($this->prefix));
|
|
$class_name_parts = explode('\\', $relative_class_name);
|
|
|
|
$path = $this->base_directory .
|
|
DIRECTORY_SEPARATOR .
|
|
implode(DIRECTORY_SEPARATOR, $class_name_parts) .
|
|
'.php';
|
|
|
|
require_once $path;
|
|
}
|
|
}
|