Autoloader.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. private $dir;
  12. public function __construct($dir = null)
  13. {
  14. if (is_null($dir)) {
  15. $dir = dirname(__FILE__).'/..';
  16. }
  17. $this->dir = $dir;
  18. }
  19. /**
  20. * Registers OAuth2\Autoloader as an SPL autoloader.
  21. */
  22. public static function register($dir = null)
  23. {
  24. ini_set('unserialize_callback_func', 'spl_autoload_call');
  25. spl_autoload_register(array(new self($dir), 'autoload'));
  26. }
  27. /**
  28. * Handles autoloading of classes.
  29. *
  30. * @param string $class A class name.
  31. *
  32. * @return boolean Returns true if the class has been loaded
  33. */
  34. public function autoload($class)
  35. {
  36. if (0 !== strpos($class, 'OAuth2')) {
  37. return;
  38. }
  39. if (file_exists($file = $this->dir.'/'.str_replace('\\', '/', $class).'.php')) {
  40. require $file;
  41. }
  42. }
  43. }