Autoloader.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 Predis;
  11. class Autoloader
  12. {
  13. private $_baseDir;
  14. private $_prefix;
  15. public function __construct($baseDirectory = null)
  16. {
  17. $this->_baseDir = $baseDirectory ?: dirname(__FILE__);
  18. $this->_prefix = __NAMESPACE__ . '\\';
  19. }
  20. public static function register()
  21. {
  22. spl_autoload_register(array(new self, 'autoload'));
  23. }
  24. public function autoload($className)
  25. {
  26. if (0 !== strpos($className, $this->_prefix)) {
  27. return;
  28. }
  29. $relativeClassName = substr($className, strlen($this->_prefix));
  30. $classNameParts = explode('\\', $relativeClassName);
  31. $path = $this->_baseDir .
  32. DIRECTORY_SEPARATOR .
  33. implode(DIRECTORY_SEPARATOR, $classNameParts) .
  34. '.php';
  35. require_once $path;
  36. }
  37. }