autoload.php 5.0 KB

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