PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Bluz/Loader.php

http://github.com/AntonShevchuk/Bluz
PHP | 180 lines | 70 code | 10 blank | 100 comment | 11 complexity | 71167cf810eb11dbb21c6a330302f3ae MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2012 by Bluz PHP Team
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. /**
  24. * @namespace
  25. */
  26. namespace Bluz;
  27. /**
  28. * Loader
  29. *
  30. * @category Bluz
  31. * @package Loader
  32. *
  33. * <pre>
  34. * <code>
  35. * // configuration example
  36. * return array(
  37. * "loader" => array(
  38. * "namespaces" => array(
  39. * 'Bluz' => PATH_LIBRARY,
  40. * 'Application' => PATH_APPLICATION .'/models',
  41. * 'Staffload' => PATH_APPLICATION .'/models'
  42. * ),
  43. * "prefixes" => array(
  44. *
  45. * ),
  46. * ),
  47. *
  48. * </code>
  49. * </pre>
  50. *
  51. * @author Anton Shevchuk
  52. * @created 06.07.11 16:07
  53. */
  54. class Loader
  55. {
  56. /**
  57. * Array of namespaces
  58. * for 5.3 libraries
  59. *
  60. * @var array
  61. */
  62. private $namespaces = array();
  63. /**
  64. * Array of prefixes
  65. * for old libraries
  66. *
  67. * @var array
  68. */
  69. private $prefixes = array();
  70. /**
  71. * <code>
  72. *
  73. * </code>
  74. *
  75. * @param string $namespace
  76. * @param mixed $paths
  77. * @return Loader
  78. */
  79. public function registerNamespace($namespace, $paths)
  80. {
  81. $this->namespaces[$namespace] = (array) $paths;
  82. return $this;
  83. }
  84. /**
  85. * <code>
  86. *
  87. * </code>
  88. *
  89. * @param string $prefix
  90. * @param mixed $paths
  91. * @return Loader
  92. */
  93. public function registerPrefix($prefix, $paths)
  94. {
  95. $this->prefixes[$prefix] = (array) $paths;
  96. return $this;
  97. }
  98. /**
  99. * Register our autoload method
  100. *
  101. * @return bool
  102. */
  103. public function register()
  104. {
  105. return spl_autoload_register(array($this, 'load'), true, false);
  106. }
  107. /**
  108. * Autoloader
  109. *
  110. * @param string $class
  111. * @throws Exception
  112. * @return void
  113. */
  114. public function load($class)
  115. {
  116. if (class_exists($class, false)
  117. || interface_exists($class, false)
  118. || trait_exists($class, false)) {
  119. return;
  120. }
  121. if ($file = $this->find($class)) {
  122. require_once $file;
  123. }
  124. if (!class_exists($class, false)
  125. && !interface_exists($class, false)
  126. && !trait_exists($class, false)) {
  127. throw new Exception("Class '$class' was not found");
  128. }
  129. }
  130. /**
  131. * Try to find class file
  132. *
  133. * @param string $class
  134. * @return bool|string
  135. */
  136. protected function find($class)
  137. {
  138. if ('\\' == $class[0]) {
  139. $class = substr($class, 1);
  140. }
  141. if (false !== $pos = strrpos($class, '\\')) {
  142. // it's namespace
  143. $namespace = substr($class, 0, $pos);
  144. foreach ($this->namespaces as $ns => $dirs) {
  145. foreach ($dirs as $dir) {
  146. if (0 === strpos($namespace, $ns)) {
  147. $className = substr($class, $pos + 1);
  148. $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
  149. if (file_exists($file)) {
  150. return $file;
  151. }
  152. }
  153. }
  154. }
  155. } else {
  156. // it's plain class
  157. foreach ($this->prefixes as $prefix => $dirs) {
  158. foreach ($dirs as $dir) {
  159. if (0 === strpos($class, $prefix)) {
  160. $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  161. if (file_exists($file)) {
  162. return $file;
  163. }
  164. }
  165. }
  166. }
  167. }
  168. return false;
  169. }
  170. }