2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-07-12 15:00:59 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Application;
|
|
|
|
|
2014-06-05 00:50:08 +02:00
|
|
|
use Exception;
|
2013-07-12 15:00:59 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
class Loader
|
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Namespace separator
|
|
|
|
*/
|
|
|
|
const NAMESPACE_SEPARATOR = '\\';
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
/**
|
2013-07-12 15:00:59 +02:00
|
|
|
* List of 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-07-15 12:16:14 +02:00
|
|
|
/**
|
|
|
|
* Detach spl autoload method from stack
|
|
|
|
*/
|
2013-07-12 15:00:59 +02:00
|
|
|
public function __destruct()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
$this->unRegister();
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-12 15:00:59 +02:00
|
|
|
* Register new namespace for directory
|
2014-04-09 14:20:05 +02:00
|
|
|
*
|
|
|
|
* @param string $namespace
|
|
|
|
* @param string $directory
|
|
|
|
*
|
|
|
|
* @throws ProgrammingError
|
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
|
|
|
if (!is_dir($directory)) {
|
2014-06-05 00:50:08 +02:00
|
|
|
throw new Exception(sprintf(
|
|
|
|
'Namespace directory "%s" for "%s" does not exist',
|
|
|
|
$namespace,
|
|
|
|
$directory
|
|
|
|
));
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2013-07-12 15:00:59 +02:00
|
|
|
|
|
|
|
$this->namespaces[$namespace] = $directory;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Test if a namespace exists
|
2014-04-09 14:20:05 +02:00
|
|
|
*
|
|
|
|
* @param string $namespace
|
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class loader
|
|
|
|
*
|
2014-04-09 14:20:05 +02:00
|
|
|
* Ignores all but classes in registered namespaces.
|
|
|
|
*
|
|
|
|
* @param string $class
|
2013-06-07 11:44:37 +02:00
|
|
|
*
|
2014-04-09 14:20:05 +02:00
|
|
|
* @return boolean
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if we have a registered namespaces for this class
|
|
|
|
*
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Effectively registers the autoloader the PHP/SPL way
|
|
|
|
*/
|
2013-07-12 15:00:59 +02:00
|
|
|
public function register()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
// Think about to add class pathes to php include path
|
|
|
|
// this could be faster (tg)
|
|
|
|
spl_autoload_register(array(&$this, 'loadClass'));
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-12 15:00:59 +02:00
|
|
|
* Detach autoloader from spl registration
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2013-07-12 15:00:59 +02:00
|
|
|
public function unRegister()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
spl_autoload_unregister(array(&$this, 'loadClass'));
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
}
|