Autoload.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Phergie
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  9. * This source file is subject to the new BSD license that is bundled
  10. * with this package in the file LICENSE.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://phergie.org/license
  13. *
  14. * @category Phergie
  15. * @package Phergie
  16. * @author Phergie Development Team <team@phergie.org>
  17. * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
  18. * @license http://phergie.org/license New BSD License
  19. * @link http://pear.phergie.org/package/Phergie
  20. */
  21. /**
  22. * Autoloader for Phergie classes.
  23. *
  24. * @category Phergie
  25. * @package Phergie
  26. * @author Phergie Development Team <team@phergie.org>
  27. * @license http://phergie.org/license New BSD License
  28. * @link http://pear.phergie.org/package/Phergie
  29. */
  30. class Phergie_Autoload
  31. {
  32. /**
  33. * Constructor to add the base Phergie path to the include_path.
  34. *
  35. * @return void
  36. */
  37. public function __construct()
  38. {
  39. $path = realpath(dirname(__FILE__) . '/..');
  40. $includePath = get_include_path();
  41. $includePathList = explode(PATH_SEPARATOR, $includePath);
  42. if (!in_array($path, $includePathList)) {
  43. self::addPath($path);
  44. }
  45. }
  46. /**
  47. * Autoload callback for loading class files.
  48. *
  49. * @param string $class Class to load
  50. *
  51. * @return void
  52. */
  53. public function load($class)
  54. {
  55. include str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
  56. }
  57. /**
  58. * Registers an instance of this class as an autoloader.
  59. *
  60. * @return void
  61. */
  62. public static function registerAutoloader()
  63. {
  64. spl_autoload_register(array(new self, 'load'));
  65. }
  66. /**
  67. * Add a path to the include path.
  68. *
  69. * @param string $path Path to add
  70. *
  71. * @return void
  72. */
  73. public static function addPath($path)
  74. {
  75. set_include_path($path . PATH_SEPARATOR . get_include_path());
  76. }
  77. }