Autoloader.php 988 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace PhpParser;
  3. /**
  4. * @codeCoverageIgnore
  5. */
  6. class Autoloader
  7. {
  8. /** @var bool Whether the autoloader has been registered. */
  9. private static $registered = false;
  10. /**
  11. * Registers PhpParser\Autoloader as an SPL autoloader.
  12. *
  13. * @param bool $prepend Whether to prepend the autoloader instead of appending
  14. */
  15. static public function register($prepend = false) {
  16. if (self::$registered === true) {
  17. return;
  18. }
  19. spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
  20. self::$registered = true;
  21. }
  22. /**
  23. * Handles autoloading of classes.
  24. *
  25. * @param string $class A class name.
  26. */
  27. static public function autoload($class) {
  28. if (0 === strpos($class, 'PhpParser\\')) {
  29. $fileName = __DIR__ . strtr(substr($class, 9), '\\', '/') . '.php';
  30. if (file_exists($fileName)) {
  31. require $fileName;
  32. }
  33. }
  34. }
  35. }