Autoloader.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @file
  4. * Reference PSR-0 autoloader
  5. *
  6. * @link
  7. * https://gist.github.com/221634
  8. */
  9. /**
  10. * SplClassLoader implementation that implements the technical interoperability
  11. * standards for PHP 5.3 namespaces and class names.
  12. *
  13. * http://groups.google.com/group/php-standards/web/final-proposal
  14. *
  15. * // Example which loads classes for the Doctrine Common package in the
  16. * // Doctrine\Common namespace.
  17. * $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
  18. * $classLoader->register();
  19. *
  20. * @author Jonathan H. Wage <jonwage@gmail.com>
  21. * @author Roman S. Borschel <roman@code-factory.org>
  22. * @author Matthew Weier O'Phinney <matthew@zend.com>
  23. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  24. * @author Fabien Potencier <fabien.potencier@symfony-project.org>
  25. */
  26. class SplClassLoader
  27. {
  28. private $_fileExtension = '.php';
  29. private $_namespace;
  30. private $_includePath;
  31. private $_namespaceSeparator = '\\';
  32. /**
  33. * Creates a new <tt>SplClassLoader</tt> that loads classes of the
  34. * specified namespace.
  35. *
  36. * @param string $ns The namespace to use.
  37. */
  38. public function __construct($ns = null, $includePath = null)
  39. {
  40. $this->_namespace = $ns;
  41. $this->_includePath = $includePath;
  42. }
  43. /**
  44. * Sets the namespace separator used by classes in the namespace of this class loader.
  45. *
  46. * @param string $sep The separator to use.
  47. */
  48. public function setNamespaceSeparator($sep)
  49. {
  50. $this->_namespaceSeparator = $sep;
  51. }
  52. /**
  53. * Gets the namespace seperator used by classes in the namespace of this class loader.
  54. *
  55. * @return void
  56. */
  57. public function getNamespaceSeparator()
  58. {
  59. return $this->_namespaceSeparator;
  60. }
  61. /**
  62. * Sets the base include path for all class files in the namespace of this class loader.
  63. *
  64. * @param string $includePath
  65. */
  66. public function setIncludePath($includePath)
  67. {
  68. $this->_includePath = $includePath;
  69. }
  70. /**
  71. * Gets the base include path for all class files in the namespace of this class loader.
  72. *
  73. * @return string $includePath
  74. */
  75. public function getIncludePath()
  76. {
  77. return $this->_includePath;
  78. }
  79. /**
  80. * Sets the file extension of class files in the namespace of this class loader.
  81. *
  82. * @param string $fileExtension
  83. */
  84. public function setFileExtension($fileExtension)
  85. {
  86. $this->_fileExtension = $fileExtension;
  87. }
  88. /**
  89. * Gets the file extension of class files in the namespace of this class loader.
  90. *
  91. * @return string $fileExtension
  92. */
  93. public function getFileExtension()
  94. {
  95. return $this->_fileExtension;
  96. }
  97. /**
  98. * Installs this class loader on the SPL autoload stack.
  99. */
  100. public function register()
  101. {
  102. spl_autoload_register(array($this, 'loadClass'));
  103. }
  104. /**
  105. * Uninstalls this class loader from the SPL autoloader stack.
  106. */
  107. public function unregister()
  108. {
  109. spl_autoload_unregister(array($this, 'loadClass'));
  110. }
  111. /**
  112. * Loads the given class or interface.
  113. *
  114. * @param string $className The name of the class to load.
  115. * @return void
  116. */
  117. public function loadClass($className)
  118. {
  119. if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
  120. $fileName = '';
  121. $namespace = '';
  122. if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
  123. $namespace = substr($className, 0, $lastNsPos);
  124. $className = substr($className, $lastNsPos + 1);
  125. $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  126. }
  127. $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
  128. $required = ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
  129. require $required;
  130. }
  131. }
  132. }