Autoloader.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ QSBif5jV3jDYkGZIgmOH1DJBH/FYrfa6x0gbADiBLmM=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.9 [rev.1.9.01]
  11. */
  12. /**
  13. * autoloader class
  14. */
  15. namespace Cube\Loader;
  16. class Autoloader
  17. {
  18. /**
  19. *
  20. * location of the framework and application libraries
  21. */
  22. const LIBRARIES_PATH = 'library';
  23. /**
  24. *
  25. * we will allow mods to override classes
  26. */
  27. const MODS_PATH = 'mods';
  28. /**
  29. *
  30. * holds the array of autoloader paths
  31. *
  32. * @var array
  33. */
  34. private $_paths = array();
  35. /**
  36. *
  37. * the extension for the files to be autoloaded
  38. *
  39. * @var string
  40. */
  41. private $_fileExtension = '.php';
  42. /**
  43. *
  44. * mods path variable
  45. *
  46. * @var string
  47. */
  48. private $_modsPath = self::MODS_PATH;
  49. /**
  50. *
  51. * holds an instance of the object
  52. *
  53. * @var \Cube\Loader\Autoloader
  54. */
  55. private static $_instance;
  56. /**
  57. * class constructor
  58. *
  59. * set the folder path for the default library
  60. */
  61. protected function __construct()
  62. {
  63. $this->addPaths(array(
  64. self::LIBRARIES_PATH,
  65. ));
  66. }
  67. /**
  68. *
  69. * returns an instance of the object and creates it if it wasnt instantiated yet
  70. *
  71. * @return \Cube\Loader\Autoloader
  72. */
  73. public static function getInstance()
  74. {
  75. if (!self::$_instance instanceof self) {
  76. self::$_instance = new self();
  77. }
  78. return self::$_instance;
  79. }
  80. /**
  81. *
  82. * get mods path variable
  83. *
  84. * @return string
  85. */
  86. public function getModsPath()
  87. {
  88. return $this->_modsPath;
  89. }
  90. /**
  91. *
  92. * set a custom mods path variable
  93. *
  94. * @param string $modsPath
  95. *
  96. * @return $this
  97. */
  98. public function setModsPath($modsPath)
  99. {
  100. $this->_modsPath = $modsPath;
  101. return $this;
  102. }
  103. /**
  104. *
  105. * get autoloader paths
  106. *
  107. * @return array
  108. */
  109. public function getPaths()
  110. {
  111. return $this->_paths;
  112. }
  113. /**
  114. *
  115. * add multiple autoloader paths
  116. *
  117. * @param array $paths
  118. *
  119. * @return $this
  120. */
  121. public function addPaths($paths = array())
  122. {
  123. if (empty($this->_paths)) {
  124. $this->_paths = (array)$paths;
  125. }
  126. else if (!empty($paths)) {
  127. foreach ($paths as $path) {
  128. $this->addPath($path);
  129. }
  130. }
  131. return $this;
  132. }
  133. /**
  134. *
  135. * add single autoloader path
  136. *
  137. * @param string $path
  138. *
  139. * @return $this
  140. */
  141. public function addPath($path)
  142. {
  143. if (!in_array($path, $this->_paths)) {
  144. array_push($this->_paths, $path);
  145. }
  146. return $this;
  147. }
  148. /**
  149. *
  150. * the method will parse the data from the 'modules' key in the configuration array, and auto load all classes from the
  151. * folders defined, plus the classes from the include path (in the include path we have included the
  152. * folder where the framework is located
  153. *
  154. * @return $this
  155. */
  156. public function register()
  157. {
  158. spl_autoload_register(array($this, 'load'));
  159. return $this;
  160. }
  161. /**
  162. *
  163. * autoloader method
  164. *
  165. * @param string $class
  166. */
  167. public function load($class)
  168. {
  169. $pathInfo = pathinfo(
  170. str_replace('\\', DIRECTORY_SEPARATOR, $class));
  171. $included = false;
  172. foreach ((array)$this->_paths as $path) {
  173. if ($included === false) {
  174. $classFile = __DIR__ . '/../../../'
  175. . $path . DIRECTORY_SEPARATOR
  176. . $pathInfo['dirname'] . DIRECTORY_SEPARATOR
  177. . $pathInfo['filename'] . $this->_fileExtension;
  178. $extendedClassFile = __DIR__ . '/../../../'
  179. . $this->getModsPath() . DIRECTORY_SEPARATOR
  180. . $path . DIRECTORY_SEPARATOR
  181. . $pathInfo['dirname'] . DIRECTORY_SEPARATOR
  182. . $pathInfo['filename'] . $this->_fileExtension;
  183. if (file_exists($extendedClassFile)) {
  184. require_once $extendedClassFile;
  185. $included = true;
  186. }
  187. else if (file_exists($classFile)) {
  188. require_once $classFile;
  189. $included = true;
  190. }
  191. }
  192. }
  193. }
  194. }