PropelAutoloader.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * Simple autoloader for Propel generated model classes.
  11. * This class implements the singleton pattern.
  12. *
  13. * @author Prancois Zaninotto
  14. * @author Fabien Potencier
  15. * @version $Revision: 1773 $
  16. * @package propel.util
  17. */
  18. class PropelAutoloader
  19. {
  20. static protected $instance = null;
  21. protected $classes = array();
  22. /**
  23. * Retrieves the singleton instance of this class.
  24. *
  25. * @return PropelAutoloader A PropelAutoloader instance.
  26. */
  27. static public function getInstance()
  28. {
  29. if (!isset(self::$instance)) {
  30. self::$instance = new PropelAutoloader();
  31. }
  32. return self::$instance;
  33. }
  34. /**
  35. * Register PropelAutoloader in spl autoloader.
  36. *
  37. * @return void
  38. */
  39. static public function register()
  40. {
  41. ini_set('unserialize_callback_func', 'spl_autoload_call');
  42. if (false === spl_autoload_register(array(self::getInstance(), 'autoload'))) {
  43. throw new Exception(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
  44. }
  45. }
  46. /**
  47. * Unregister PropelAutoloader from spl autoloader.
  48. *
  49. * @return void
  50. */
  51. static public function unregister()
  52. {
  53. spl_autoload_unregister(array(self::getInstance(), 'autoload'));
  54. }
  55. /**
  56. * Sets the path for a list of classes.
  57. *
  58. * @param array $classMap An associative array $className => $classPath
  59. */
  60. public function addClassPaths($classMap)
  61. {
  62. $this->classes = array_merge($this->classes, $classMap);
  63. }
  64. /**
  65. * Sets the path for a particular class.
  66. *
  67. * @param string $class A PHP class name
  68. * @param string $path A path (absolute or relative to the include path)
  69. */
  70. public function addClassPath($class, $path)
  71. {
  72. $this->classes[$class] = $path;
  73. }
  74. /**
  75. * Returns the path where a particular class can be found.
  76. *
  77. * @param string $class A PHP class name
  78. *
  79. * @return string|null A path (absolute or relative to the include path)
  80. */
  81. public function getClassPath($class)
  82. {
  83. return isset($this->classes[$class]) ? $this->classes[$class] : null;
  84. }
  85. /**
  86. * Handles autoloading of classes that have been registered in this instance
  87. *
  88. * @param string $class A class name.
  89. *
  90. * @return boolean Returns true if the class has been loaded
  91. */
  92. public function autoload($class)
  93. {
  94. if (isset($this->classes[$class])) {
  95. require $this->classes[$class];
  96. return true;
  97. }
  98. return false;
  99. }
  100. }