autoload.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. /**
  20. * SplClassLoader implementation that implements the technical interoperability
  21. * standards for PHP 5.3 namespaces and class names.
  22. *
  23. * http://groups.google.com/group/php-standards/web/psr-0-final-proposal?pli=1
  24. *
  25. * // Example which loads classes for the Doctrine Common package in the
  26. * // Doctrine\Common namespace.
  27. * $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
  28. * $classLoader->register();
  29. *
  30. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  31. * @author Jonathan H. Wage <jonwage@gmail.com>
  32. * @author Roman S. Borschel <roman@code-factory.org>
  33. * @author Matthew Weier O'Phinney <matthew@zend.com>
  34. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  35. * @author Fabien Potencier <fabien.potencier@symfony-project.org>
  36. */
  37. class SplClassLoader {
  38. private $_fileExtension = '.php';
  39. private $_namespace;
  40. private $_includePath;
  41. private $_namespaceSeparator = '\\';
  42. /**
  43. * Creates a new <tt>SplClassLoader</tt> that loads classes of the
  44. * specified namespace.
  45. *
  46. * @param string $ns The namespace to use.
  47. * @param string $includePath The include path to search
  48. */
  49. public function __construct($ns = null, $includePath = null) {
  50. $this->_namespace = $ns;
  51. $this->_includePath = $includePath;
  52. }
  53. /**
  54. * Sets the namespace separator used by classes in the namespace of this class loader.
  55. *
  56. * @param string $sep The separator to use.
  57. */
  58. public function setNamespaceSeparator($sep) {
  59. $this->_namespaceSeparator = $sep;
  60. }
  61. /**
  62. * Gets the namespace separator used by classes in the namespace of this class loader.
  63. *
  64. * @return string The separator to use.
  65. */
  66. public function getNamespaceSeparator() {
  67. return $this->_namespaceSeparator;
  68. }
  69. /**
  70. * Sets the base include path for all class files in the namespace of this class loader.
  71. *
  72. * @param string $includePath
  73. */
  74. public function setIncludePath($includePath) {
  75. $this->_includePath = $includePath;
  76. }
  77. /**
  78. * Gets the base include path for all class files in the namespace of this class loader.
  79. *
  80. * @return string $includePath
  81. */
  82. public function getIncludePath() {
  83. return $this->_includePath;
  84. }
  85. /**
  86. * Sets the file extension of class files in the namespace of this class loader.
  87. *
  88. * @param string $fileExtension
  89. */
  90. public function setFileExtension($fileExtension) {
  91. $this->_fileExtension = $fileExtension;
  92. }
  93. /**
  94. * Gets the file extension of class files in the namespace of this class loader.
  95. *
  96. * @return string $fileExtension
  97. */
  98. public function getFileExtension() {
  99. return $this->_fileExtension;
  100. }
  101. /**
  102. * Installs this class loader on the SPL autoload stack.
  103. */
  104. public function register() {
  105. spl_autoload_register(array($this, 'loadClass'));
  106. }
  107. /**
  108. * Uninstalls this class loader from the SPL autoloader stack.
  109. */
  110. public function unregister() {
  111. spl_autoload_unregister(array($this, 'loadClass'));
  112. }
  113. /**
  114. * Loads the given class or interface.
  115. *
  116. * @param string $className The name of the class to load.
  117. * @return void
  118. */
  119. public function loadClass($className) {
  120. if (null === $this->_namespace || $this->_namespace . $this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace . $this->_namespaceSeparator))) {
  121. $fileName = '';
  122. $namespace = '';
  123. if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
  124. $namespace = substr($className, 0, $lastNsPos);
  125. $className = substr($className, $lastNsPos + 1);
  126. $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  127. }
  128. $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
  129. require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
  130. }
  131. }
  132. }
  133. $twilioClassLoader = new SplClassLoader('Twilio', realpath(__DIR__ . DIRECTORY_SEPARATOR . '..'));
  134. $twilioClassLoader->register();