2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-07-12 15:00:59 +02:00
|
|
|
|
|
|
|
namespace Icinga\Application;
|
|
|
|
|
2014-06-27 15:20:56 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2013-07-12 15:00:59 +02:00
|
|
|
|
2015-08-18 09:27:34 +02:00
|
|
|
/**
|
|
|
|
* PSR-4 class loader
|
|
|
|
*/
|
2015-08-18 09:06:58 +02:00
|
|
|
class ClassLoader
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Namespace separator
|
|
|
|
*/
|
|
|
|
const NAMESPACE_SEPARATOR = '\\';
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-18 09:27:34 +02:00
|
|
|
* Namespaces
|
2014-04-09 14:20:05 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @var array
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2013-07-12 15:00:59 +02:00
|
|
|
private $namespaces = array();
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
/**
|
2015-08-18 09:27:34 +02:00
|
|
|
* Register a base directory for a namespace prefix
|
2014-04-09 14:20:05 +02:00
|
|
|
*
|
|
|
|
* @param string $namespace
|
|
|
|
* @param string $directory
|
|
|
|
*
|
2015-08-17 13:29:15 +02:00
|
|
|
* @return $this
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2013-07-12 15:00:59 +02:00
|
|
|
public function registerNamespace($namespace, $directory)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
$this->namespaces[$namespace] = $directory;
|
2015-08-17 13:29:15 +02:00
|
|
|
|
|
|
|
return $this;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
2015-08-18 09:27:34 +02:00
|
|
|
* Test whether a namespace exists
|
2014-04-09 14:20:05 +02:00
|
|
|
*
|
2015-08-18 09:27:34 +02:00
|
|
|
* @param string $namespace
|
2014-04-09 14:20:05 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
|
|
|
public function hasNamespace($namespace)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
return array_key_exists($namespace, $this->namespaces);
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-18 09:27:34 +02:00
|
|
|
* Load the given class or interface
|
2014-04-09 14:20:05 +02:00
|
|
|
*
|
2015-08-18 09:27:34 +02:00
|
|
|
* @param string $class Name of the class or interface
|
2013-06-07 11:44:37 +02:00
|
|
|
*
|
2015-08-18 09:27:34 +02:00
|
|
|
* @return bool Whether the class or interface has been loaded
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
|
|
|
public function loadClass($class)
|
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
$namespace = $this->getNamespaceForClass($class);
|
|
|
|
|
|
|
|
if ($namespace) {
|
2014-04-09 14:20:05 +02:00
|
|
|
$file = $this->namespaces[$namespace] . preg_replace('/^' . preg_quote($namespace) . '/', '', $class);
|
|
|
|
$file = str_replace(self::NAMESPACE_SEPARATOR, '/', $file) . '.php';
|
2013-07-12 15:00:59 +02:00
|
|
|
|
|
|
|
if (@file_exists($file)) {
|
|
|
|
require_once $file;
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2013-07-12 15:00:59 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-18 09:27:34 +02:00
|
|
|
* Get the namespace for the given class
|
2013-07-12 15:00:59 +02:00
|
|
|
*
|
|
|
|
* Return is the longest match in the array found
|
|
|
|
*
|
2014-04-09 14:20:05 +02:00
|
|
|
* @param string $className
|
|
|
|
*
|
|
|
|
* @return bool|string
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
|
|
|
private function getNamespaceForClass($className)
|
|
|
|
{
|
|
|
|
$testNamespace = '';
|
|
|
|
$testLength = 0;
|
|
|
|
|
2014-04-09 14:20:05 +02:00
|
|
|
foreach (array_keys($this->namespaces) as $namespace) {
|
|
|
|
$stub = preg_replace(
|
|
|
|
'/^' . preg_quote($namespace) . '(' . preg_quote(self::NAMESPACE_SEPARATOR) . '|$)/', '', $className
|
|
|
|
);
|
2013-07-12 15:00:59 +02:00
|
|
|
$length = strlen($className) - strlen($stub);
|
|
|
|
if ($length > $testLength) {
|
|
|
|
$testLength = $length;
|
|
|
|
$testNamespace = $namespace;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-12 15:00:59 +02:00
|
|
|
|
|
|
|
if ($testLength > 0) {
|
|
|
|
return $testNamespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-18 09:27:34 +02:00
|
|
|
* Register {@link loadClass()} as an autoloader
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2013-07-12 15:00:59 +02:00
|
|
|
public function register()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2015-08-17 14:35:35 +02:00
|
|
|
spl_autoload_register(array($this, 'loadClass'));
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-18 09:27:34 +02:00
|
|
|
* Unregister {@link loadClass()} as an autoloader
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2015-08-17 14:34:39 +02:00
|
|
|
public function unregister()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2015-08-17 14:35:35 +02:00
|
|
|
spl_autoload_unregister(array($this, 'loadClass'));
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2015-08-18 09:10:23 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-18 09:27:34 +02:00
|
|
|
* Unregister this as an autloader
|
2015-08-18 09:10:23 +02:00
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$this->unregister();
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|