silverstripe-framework/core/manifest/ClassLoader.php
Sean Harvey e097f6e1a8 MINOR Fixes to method arguments in core classes for E_STRICT support.
API CHANGE Remove abstract static function and just use static functions
in Authenticator (PHP 5.3+ doesn't support abstract static functions)
2012-03-28 22:41:42 +13:00

89 lines
1.8 KiB
PHP

<?php
/**
* A class that handles loading classes and interfaces from a class manifest
* instance.
*
* @package sapphire
* @subpackage manifest
*/
class SS_ClassLoader {
/**
* @var SS_ClassLoader
*/
private static $instance;
/**
* @var SS_ClassManifest[]
*/
protected $manifests = array();
/**
* @return SS_ClassLoader
*/
public static function instance() {
return self::$instance ? self::$instance : self::$instance = new self();
}
/**
* Returns the currently active class manifest instance that is used for
* loading classes.
*
* @return SS_ClassManifest
*/
public function getManifest() {
return $this->manifests[count($this->manifests) - 1];
}
/**
* Returns true if this class loader has a manifest.
*/
public function hasManifest() {
return (bool)$this->manifests;
}
/**
* Pushes a class manifest instance onto the top of the stack. This will
* also include any module configuration files at the same time.
*
* @param SS_ClassManifest $manifest
*/
public function pushManifest(SS_ClassManifest $manifest) {
$this->manifests[] = $manifest;
}
/**
* @return SS_ClassManifest
*/
public function popManifest() {
return array_pop($this->manifests);
}
public function registerAutoloader() {
spl_autoload_register(array($this, 'loadClass'));
}
/**
* Loads a class or interface if it is present in the currently active
* manifest.
*
* @param string $class
*/
public function loadClass($class) {
if ($path = $this->getManifest()->getItemPath($class)) {
require_once $path;
}
}
/**
* Returns true if a class or interface name exists in the manifest.
*
* @param string $class
* @return bool
*/
public function classExists($class) {
return class_exists($class, false) || $this->getManifest()->getItemPath($class);
}
}