2011-03-18 07:28:11 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A class that handles loading classes and interfaces from a class manifest
|
|
|
|
* instance.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-03-18 07:28:11 +01:00
|
|
|
* @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];
|
|
|
|
}
|
2011-05-01 07:12:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this class loader has a manifest.
|
|
|
|
*/
|
|
|
|
public function hasManifest() {
|
|
|
|
return (bool)$this->manifests;
|
|
|
|
}
|
2011-03-18 07:28:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
}
|