2014-02-06 14:44:16 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PhpParser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
class Autoloader
|
|
|
|
{
|
2015-03-22 15:33:18 +01:00
|
|
|
/** @var bool Whether the autoloader has been registered. */
|
|
|
|
private static $registered = false;
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
/**
|
2014-02-12 14:06:32 +01:00
|
|
|
* Registers PhpParser\Autoloader as an SPL autoloader.
|
|
|
|
*
|
|
|
|
* @param bool $prepend Whether to prepend the autoloader instead of appending
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
static public function register(bool $prepend = false) {
|
2015-03-22 15:33:18 +01:00
|
|
|
if (self::$registered === true) {
|
2015-03-13 23:54:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-03-22 15:33:18 +01:00
|
|
|
|
2017-08-13 14:06:08 +02:00
|
|
|
spl_autoload_register([__CLASS__, 'autoload'], true, $prepend);
|
2015-03-22 15:33:18 +01:00
|
|
|
self::$registered = true;
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-12 14:06:32 +01:00
|
|
|
* Handles autoloading of classes.
|
|
|
|
*
|
|
|
|
* @param string $class A class name.
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
static public function autoload(string $class) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if (0 === strpos($class, 'PhpParser\\')) {
|
2016-04-15 15:07:30 +02:00
|
|
|
$fileName = __DIR__ . strtr(substr($class, 9), '\\', '/') . '.php';
|
2014-03-27 15:40:08 +01:00
|
|
|
if (file_exists($fileName)) {
|
|
|
|
require $fileName;
|
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|