Created
January 8, 2013 22:04
-
-
Save jtpaasch/4488413 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* An SplClassLoader implementation that implements | |
* the PHP 5.3 standards. | |
* | |
* @based on https://gist.github.com/221634 | |
*/ | |
class SplClassLoader { | |
private $file_extension = '.php'; | |
private $namespace; | |
private $include_path; | |
private $namespace_separator = '\\'; | |
private $ignore_underscores = true; | |
public function __construct($namespace = null, $include_path = null) { | |
$this->namespace = $namespace; | |
$this->include_path = $include_path; | |
} | |
public function get_or_set($property, $value=null) { | |
if ($value === null) { | |
return $this->{$property}; | |
} else { | |
return $this->{$property} = $value; | |
} | |
} | |
public function namespace_separator($separator = null) { | |
return $this->get_or_set('namespace_separator', $separator); | |
} | |
public function include_path($path = null) { | |
return $this->get_or_set('include_path', $path); | |
} | |
public function file_extension($extension = null) { | |
return $this->get_or_set('file_extension', $extension); | |
} | |
public function ignore_underscores($ignore = null) { | |
return $this->get_or_set('ignore_underscores', $ignore); | |
} | |
public function register() { | |
spl_autoload_register(array($this, 'autoload_class')); | |
} | |
public function unregister() { | |
spl_autoload_unregister(array($this, 'autoload_class')); | |
} | |
public function autoload_class($class) { | |
// See if the registered namespace is in the class name. | |
// We don't need to try and load this class if it's for | |
// some other namespace. | |
$registered_space = $this->namespace | |
. $this->namespace_separator(); | |
$requested_space = substr($class, 0, strlen($registered_space)); | |
if ($this->namespace === null | |
|| $registered_space === $requested_space) { | |
// Stub some values. | |
$file = ''; | |
$name_space = ''; | |
// If there is a namespace in here, pull it out | |
// and construct the file path from it. | |
$last_slash = strripos($class, $this->namespace_separator()); | |
if ($last_slash !== false) { | |
$namespace = substr($class, 0, $last_slash); | |
$class = substr($class, $last_slash + 1); | |
$file = str_replace( | |
$this->namespace_separator(), | |
DIRECTORY_SEPARATOR, | |
$namespace | |
); | |
$file .= DIRECTORY_SEPARATOR; | |
} | |
// If we're not ignoring underscores, turn them into | |
// directory separators (some frameworks use | |
// underscores in class names as a convention | |
// to indicate subfolders). | |
if (!$this->ignore_underscores) { | |
$file .= str_replace( | |
'_', | |
DIRECTORY_SEPARATOR, | |
$class | |
); | |
} else { | |
$file .= $class; | |
} | |
// Add the file extension. | |
$file .= $this->file_extension(); | |
// Add an include path if there is one. | |
if ($this->include_path() === null) { | |
$path = ''; | |
} else { | |
$path = $this->include_path; | |
} | |
// Require the file | |
$file = $path . DIRECTORY_SEPARATOR . $file; | |
require $file; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment