silverstripe-framework/src/Core/Manifest/ClassLoader.php

128 lines
3.0 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Core\Manifest;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Dev\Deprecation;
/**
* A class that handles loading classes and interfaces from a class manifest
* instance.
*/
2016-11-29 00:31:16 +01:00
class ClassLoader
{
2016-11-29 00:31:16 +01:00
/**
* @internal
2016-11-29 00:31:16 +01:00
* @var ClassLoader
*/
private static $instance;
2016-11-29 00:31:16 +01:00
/**
* @var array Map of 'instance' (ClassManifest) and other options.
*/
protected $manifests = array();
2016-11-29 00:31:16 +01:00
/**
* @return ClassLoader
*/
public static function inst()
2016-11-29 00:31:16 +01:00
{
return self::$instance ? self::$instance : self::$instance = new self();
}
2016-11-29 00:31:16 +01:00
/**
* Returns the currently active class manifest instance that is used for
* loading classes.
*
* @return ClassManifest
*/
public function getManifest()
{
return $this->manifests[count($this->manifests) - 1]['instance'];
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Returns true if this class loader has a manifest.
*/
public function hasManifest()
{
return (bool)$this->manifests;
}
2016-11-29 00:31:16 +01:00
/**
* Pushes a class manifest instance onto the top of the stack.
*
* @param ClassManifest $manifest
* @param bool $exclusive Marks the manifest as exclusive. If set to FALSE, will
* look for classes in earlier manifests as well.
*/
public function pushManifest(ClassManifest $manifest, $exclusive = true)
{
$this->manifests[] = array('exclusive' => $exclusive, 'instance' => $manifest);
}
2016-11-29 00:31:16 +01:00
/**
* @return ClassManifest
*/
public function popManifest()
{
$manifest = array_pop($this->manifests);
return $manifest['instance'];
}
2016-11-29 00:31:16 +01:00
public function registerAutoloader()
{
spl_autoload_register(array($this, 'loadClass'));
}
2016-11-29 00:31:16 +01:00
/**
* Loads a class or interface if it is present in the currently active
* manifest.
*
* @param string $class
* @return String
*/
public function loadClass($class)
{
if ($path = $this->getItemPath($class)) {
require_once $path;
}
return $path;
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Returns the path for a class or interface in the currently active manifest,
* or any previous ones if later manifests aren't set to "exclusive".
*
* @param string $class
* @return string|false
*/
public function getItemPath($class)
{
foreach (array_reverse($this->manifests) as $manifest) {
/** @var ClassManifest $manifestInst */
$manifestInst = $manifest['instance'];
if ($path = $manifestInst->getItemPath($class)) {
return $path;
}
if ($manifest['exclusive']) {
break;
}
}
return false;
}
2016-11-29 00:31:16 +01:00
/**
* Returns true if a class or interface name exists in the manifest.
*
* @param string $class
* @return bool
*/
public function classExists($class)
{
Deprecation::notice('4.0', 'Use ClassInfo::exists.');
return ClassInfo::exists($class);
}
}