/SplClassLoader.php

https://bitbucket.org/fsilva/slick-framework · PHP · 167 lines · 62 code · 12 blank · 93 comment · 4 complexity · 2528f1ec7121bc29dc8973563e09886b MD5 · raw file

  1. <?php
  2. /**
  3. * SplClassLoader
  4. *
  5. * PHP version 5
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @package Slick
  20. * @subpackage Core
  21. * @author Filipe Silva <silvam.filipe@gmail.com>
  22. * @copyright Filipe Silva 2013
  23. * @license Apache License, Version 2.0 (the "License")
  24. * @since Version 1.0.0
  25. */
  26. /**
  27. * SplClassLoader implementation that implements the technical interoperability
  28. * standards for PHP 5.3 namespaces and class names.
  29. *
  30. * http://groups.google.com/group/php-standards/web/final-proposal
  31. *
  32. * // Example which loads classes for the Doctrine Common package in the
  33. * // Doctrine\Common namespace.
  34. * $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
  35. * $classLoader->register();
  36. *
  37. * @package Slick
  38. * @subpackage Core
  39. * @author Jonathan H. Wage <jonwage@gmail.com>
  40. * @author Roman S. Borschel <roman@code-factory.org>
  41. * @author Matthew Weier O'Phinney <matthew@zend.com>
  42. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  43. * @author Fabien Potencier <fabien.potencier@symfony-project.org>
  44. */
  45. class SplClassLoader
  46. {
  47. private $_fileExtension = '.php';
  48. private $_namespace;
  49. private $_includePath;
  50. private $_namespaceSeparator = '\\';
  51. /**
  52. * Creates a new <tt>SplClassLoader</tt> that loads classes of the
  53. * specified namespace.
  54. *
  55. * @param string $ns The namespace to use.
  56. */
  57. public function __construct($ns = null, $includePath = null)
  58. {
  59. $this->_namespace = $ns;
  60. $this->_includePath = $includePath;
  61. }
  62. /**
  63. * Sets the namespace separator used by classes in the namespace of this class loader.
  64. *
  65. * @param string $sep The separator to use.
  66. */
  67. public function setNamespaceSeparator($sep)
  68. {
  69. $this->_namespaceSeparator = $sep;
  70. }
  71. /**
  72. * Gets the namespace seperator used by classes in the namespace of this class loader.
  73. *
  74. * @return void
  75. */
  76. public function getNamespaceSeparator()
  77. {
  78. return $this->_namespaceSeparator;
  79. }
  80. /**
  81. * Sets the base include path for all class files in the namespace of this class loader.
  82. *
  83. * @param string $includePath
  84. */
  85. public function setIncludePath($includePath)
  86. {
  87. $this->_includePath = $includePath;
  88. }
  89. /**
  90. * Gets the base include path for all class files in the namespace of this class loader.
  91. *
  92. * @return string $includePath
  93. */
  94. public function getIncludePath()
  95. {
  96. return $this->_includePath;
  97. }
  98. /**
  99. * Sets the file extension of class files in the namespace of this class loader.
  100. *
  101. * @param string $fileExtension
  102. */
  103. public function setFileExtension($fileExtension)
  104. {
  105. $this->_fileExtension = $fileExtension;
  106. }
  107. /**
  108. * Gets the file extension of class files in the namespace of this class loader.
  109. *
  110. * @return string $fileExtension
  111. */
  112. public function getFileExtension()
  113. {
  114. return $this->_fileExtension;
  115. }
  116. /**
  117. * Installs this class loader on the SPL autoload stack.
  118. */
  119. public function register()
  120. {
  121. spl_autoload_register(array($this, 'loadClass'));
  122. }
  123. /**
  124. * Uninstalls this class loader from the SPL autoloader stack.
  125. */
  126. public function unregister()
  127. {
  128. spl_autoload_unregister(array($this, 'loadClass'));
  129. }
  130. /**
  131. * Loads the given class or interface.
  132. *
  133. * @param string $className The name of the class to load.
  134. * @return void
  135. */
  136. public function loadClass($className)
  137. {
  138. // @codingStandardsIgnoreStart
  139. if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
  140. $fileName = '';
  141. $namespace = '';
  142. if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
  143. $namespace = substr($className, 0, $lastNsPos);
  144. $className = substr($className, $lastNsPos + 1);
  145. $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  146. }
  147. $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
  148. $combined = ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
  149. if (is_file($combined)) {
  150. include($combined);
  151. }
  152. }
  153. // @codingStandardsIgnoreEnd
  154. }
  155. }