/com.piece_framework.makegood.stagehand_testrunner/resources/php/PEAR/Stagehand/Autoload/Loader.php

https://github.com/rsky/makegood · PHP · 229 lines · 68 code · 39 blank · 122 comment · 8 complexity · 9f8d3019c3ffce297fc00f7d6aa76ff0 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4. * PHP version 5
  5. *
  6. * Copyright (c) 2009-2010 KUBO Atsuhiro <kubo@iteman.jp>,
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * @package Stagehand_Autoload
  31. * @copyright 2009-2010 KUBO Atsuhiro <kubo@iteman.jp>
  32. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  33. * @version Release: 1.1.0
  34. * @since File available since Release 0.2.0
  35. */
  36. // {{{ Stagehand_Autoload_Loader
  37. /**
  38. * @package Stagehand_Autoload
  39. * @copyright 2009-2010 KUBO Atsuhiro <kubo@iteman.jp>
  40. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  41. * @version Release: 1.1.0
  42. * @since File available since Release 0.2.0
  43. */
  44. abstract class Stagehand_Autoload_Loader
  45. {
  46. // {{{ properties
  47. /**#@+
  48. * @access public
  49. */
  50. /**#@-*/
  51. /**#@+
  52. * @access protected
  53. */
  54. protected $namespaceSeparator;
  55. protected $namespaces = array();
  56. /**#@-*/
  57. /**#@+
  58. * @access private
  59. */
  60. /**#@-*/
  61. /**#@+
  62. * @access public
  63. */
  64. // }}}
  65. // {{{ load()
  66. /**
  67. * Loads an appropriate class.
  68. *
  69. * @param string $class
  70. * @return boolean
  71. */
  72. public function load($class)
  73. {
  74. if (strpos($class, '.') !== false) {
  75. return false;
  76. }
  77. $class = $this->normalizeClassName($class);
  78. if (class_exists($class, false)) {
  79. return true;
  80. }
  81. if (!$this->inNamespaces($class)) {
  82. return false;
  83. }
  84. $file = str_replace($this->namespaceSeparator, '/', $class) . '.php';
  85. $oldErrorReportingLevel = error_reporting(error_reporting() & ~E_WARNING);
  86. $result = $this->loadFile($file);
  87. error_reporting($oldErrorReportingLevel);
  88. if ($result === false) {
  89. return false;
  90. }
  91. if (!class_exists($class, false) && !interface_exists($class, false)) {
  92. trigger_error(
  93. 'Class ' .
  94. $class .
  95. ' was not present in ' .
  96. $file .
  97. ', (include_path="' .
  98. get_include_path() .
  99. '")',
  100. E_USER_WARNING
  101. );
  102. return false;
  103. }
  104. return true;
  105. }
  106. // }}}
  107. // {{{ addNamespace()
  108. /**
  109. * Adds a namespace to the targets for autoloading.
  110. *
  111. * @param string $namespace
  112. */
  113. public function addNamespace($namespace)
  114. {
  115. if (in_array($namespace, $this->namespaces)) {
  116. return;
  117. }
  118. $this->namespaces[] = $namespace;
  119. }
  120. /**#@-*/
  121. /**#@+
  122. * @access protected
  123. */
  124. // }}}
  125. // {{{ loadFile()
  126. /**
  127. * @param string $file
  128. * @return boolean
  129. */
  130. protected function loadFile($file)
  131. {
  132. return include $file;
  133. }
  134. // }}}
  135. // {{{ normalizeClassName()
  136. /**
  137. * @param string $class
  138. * @return string
  139. * @since Method available since Release 0.5.0
  140. */
  141. protected function normalizeClassName($class)
  142. {
  143. return $class;
  144. }
  145. // }}}
  146. // {{{ inNamespaces()
  147. /**
  148. * @param string $class
  149. * @return boolean
  150. * @since Method available since Release 0.5.0
  151. */
  152. protected function inNamespaces($class)
  153. {
  154. foreach ($this->namespaces as $namespace) {
  155. if ($this->matchNamespace($class, $namespace)) {
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. // }}}
  162. // {{{ matchNamespace()
  163. /**
  164. * @param string $class
  165. * @param string $namespace
  166. * @return boolean
  167. * @since Method available since Release 0.5.0
  168. */
  169. protected function matchNamespace($class, $namespace)
  170. {
  171. return (boolean)preg_match('/^' . preg_quote($namespace . $this->namespaceSeparator) . '/', $class);
  172. }
  173. /**#@-*/
  174. /**#@+
  175. * @access private
  176. */
  177. /**#@-*/
  178. // }}}
  179. }
  180. // }}}
  181. /*
  182. * Local Variables:
  183. * mode: php
  184. * coding: iso-8859-1
  185. * tab-width: 4
  186. * c-basic-offset: 4
  187. * c-hanging-comment-ender-p: nil
  188. * indent-tabs-mode: nil
  189. * End:
  190. */