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;
|
|
|
|
|
|
|
|
/** @var bool Whether we're running on PHP 7. */
|
|
|
|
private static $runningOnPhp7;
|
2015-03-13 23:54:32 +01:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
static public function register($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
|
|
|
|
2014-02-12 14:06:32 +01:00
|
|
|
spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
|
2015-03-22 15:33:18 +01:00
|
|
|
self::$registered = true;
|
|
|
|
self::$runningOnPhp7 = version_compare(PHP_VERSION, '7.0-dev', '>=');
|
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.
|
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
static public function autoload($class) {
|
|
|
|
if (0 === strpos($class, 'PhpParser\\')) {
|
2015-03-24 20:04:50 +01:00
|
|
|
if (isset(self::$php7AliasesOldToNew[$class])) {
|
2015-04-03 22:22:39 +02:00
|
|
|
if (self::$runningOnPhp7) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:30:30 +02:00
|
|
|
// Load the new class, alias will be registered afterwards
|
|
|
|
$class = self::$php7AliasesOldToNew[$class];
|
2015-03-20 21:47:20 +01:00
|
|
|
}
|
|
|
|
|
2014-04-02 09:44:45 +02:00
|
|
|
$fileName = dirname(__DIR__) . '/' . strtr($class, '\\', '/') . '.php';
|
2014-03-27 15:40:08 +01:00
|
|
|
if (file_exists($fileName)) {
|
|
|
|
require $fileName;
|
|
|
|
}
|
2015-03-24 20:04:50 +01:00
|
|
|
|
|
|
|
if (isset(self::$php7AliasesNewToOld[$class])) {
|
|
|
|
// New class name was used, register alias for old one, otherwise
|
|
|
|
// it won't be usable in "instanceof" and other non-autoloading places.
|
2015-04-03 22:22:39 +02:00
|
|
|
if (!self::$runningOnPhp7) {
|
|
|
|
class_alias($class, self::$php7AliasesNewToOld[$class]);
|
|
|
|
}
|
2015-03-24 20:04:50 +01:00
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 20:04:50 +01:00
|
|
|
private static $php7AliasesOldToNew = array(
|
2015-03-20 21:47:20 +01:00
|
|
|
'PhpParser\Node\Expr\Cast\Bool' => 'PhpParser\Node\Expr\Cast\Bool_',
|
|
|
|
'PhpParser\Node\Expr\Cast\Int' => 'PhpParser\Node\Expr\Cast\Int_',
|
2015-03-21 18:57:55 +01:00
|
|
|
'PhpParser\Node\Expr\Cast\Object' => 'PhpParser\Node\Expr\Cast\Object_',
|
2015-03-20 21:47:20 +01:00
|
|
|
'PhpParser\Node\Expr\Cast\String' => 'PhpParser\Node\Expr\Cast\String_',
|
|
|
|
'PhpParser\Node\Scalar\String' => 'PhpParser\Node\Scalar\String_',
|
|
|
|
);
|
|
|
|
|
2015-03-24 20:04:50 +01:00
|
|
|
private static $php7AliasesNewToOld = array(
|
|
|
|
'PhpParser\Node\Expr\Cast\Bool_' => 'PhpParser\Node\Expr\Cast\Bool',
|
|
|
|
'PhpParser\Node\Expr\Cast\Int_' => 'PhpParser\Node\Expr\Cast\Int',
|
|
|
|
'PhpParser\Node\Expr\Cast\Object_' => 'PhpParser\Node\Expr\Cast\Object',
|
|
|
|
'PhpParser\Node\Expr\Cast\String_' => 'PhpParser\Node\Expr\Cast\String',
|
|
|
|
'PhpParser\Node\Scalar\String_' => 'PhpParser\Node\Scalar\String',
|
|
|
|
);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|