Autoloader.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace RNCryptor;
  11. /**
  12. * Implements a lightweight PSR-0 compliant autoloader.
  13. *
  14. * @author Eric Naeseth <eric@thumbtack.com>
  15. * @author Daniele Alessandri <suppakilla@gmail.com>
  16. */
  17. class Autoloader
  18. {
  19. private $directory;
  20. private $prefix;
  21. private $prefixLength;
  22. /**
  23. * @param string $baseDirectory Base directory where the source files are located.
  24. */
  25. public function __construct($baseDirectory = __DIR__)
  26. {
  27. $this->directory = $baseDirectory;
  28. $this->prefix = __NAMESPACE__ . '\\';
  29. $this->prefixLength = strlen($this->prefix);
  30. }
  31. /**
  32. * Registers the autoloader class with the PHP SPL autoloader.
  33. *
  34. * @param bool $prepend Prepend the autoloader on the stack instead of appending it.
  35. */
  36. public static function register($prepend = false)
  37. {
  38. spl_autoload_register(array(new self, 'autoload'), true, $prepend);
  39. }
  40. /**
  41. * Loads a class from a file using its fully qualified name.
  42. *
  43. * @param string $className Fully qualified name of a class.
  44. */
  45. public function autoload($className)
  46. {
  47. if (0 === strpos($className, $this->prefix)) {
  48. $parts = explode('\\', substr($className, $this->prefixLength));
  49. $filepath = $this->directory.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts).'.php';
  50. if (is_file($filepath)) {
  51. require($filepath);
  52. }
  53. }
  54. }
  55. }