Autoloader.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace OAuth2;
  3. /**
  4. * Autoloads OAuth2 classes
  5. *
  6. * @author Brent Shaffer <bshafs at gmail dot com>
  7. * @license MIT License
  8. */
  9. class Autoloader
  10. {
  11. /**
  12. * @var string
  13. */
  14. private $dir;
  15. /**
  16. * @param string $dir
  17. */
  18. public function __construct($dir = null)
  19. {
  20. if (is_null($dir)) {
  21. $dir = dirname(__FILE__).'/..';
  22. }
  23. $this->dir = $dir;
  24. }
  25. /**
  26. * Registers OAuth2\Autoloader as an SPL autoloader.
  27. */
  28. public static function register($dir = null)
  29. {
  30. ini_set('unserialize_callback_func', 'spl_autoload_call');
  31. spl_autoload_register(array(new self($dir), 'autoload'));
  32. }
  33. /**
  34. * Handles autoloading of classes.
  35. *
  36. * @param string $class - A class name.
  37. * @return boolean - Returns true if the class has been loaded
  38. */
  39. public function autoload($class)
  40. {
  41. if (0 !== strpos($class, 'OAuth2')) {
  42. return;
  43. }
  44. if (file_exists($file = $this->dir.'/'.str_replace('\\', '/', $class).'.php')) {
  45. require $file;
  46. }
  47. }
  48. }