ClassLoader.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Core;
  15. /**
  16. * ClassLoader
  17. */
  18. class ClassLoader
  19. {
  20. /**
  21. * An associative array where the key is a namespace prefix and the value
  22. * is an array of base directories for classes in that namespace.
  23. *
  24. * @var array
  25. */
  26. protected $_prefixes = [];
  27. /**
  28. * Register loader with SPL autoloader stack.
  29. *
  30. * @return void
  31. */
  32. public function register()
  33. {
  34. spl_autoload_register([$this, 'loadClass']);
  35. }
  36. /**
  37. * Adds a base directory for a namespace prefix.
  38. *
  39. * @param string $prefix The namespace prefix.
  40. * @param string $baseDir A base directory for class files in the
  41. * namespace.
  42. * @param bool $prepend If true, prepend the base directory to the stack
  43. * instead of appending it; this causes it to be searched first rather
  44. * than last.
  45. * @return void
  46. */
  47. public function addNamespace($prefix, $baseDir, $prepend = false)
  48. {
  49. $prefix = trim($prefix, '\\') . '\\';
  50. $baseDir = rtrim($baseDir, '/') . DIRECTORY_SEPARATOR;
  51. $baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR) . '/';
  52. if (!isset($this->_prefixes[$prefix])) {
  53. $this->_prefixes[$prefix] = [];
  54. }
  55. if ($prepend) {
  56. array_unshift($this->_prefixes[$prefix], $baseDir);
  57. } else {
  58. $this->_prefixes[$prefix][] = $baseDir;
  59. }
  60. }
  61. /**
  62. * Loads the class file for a given class name.
  63. *
  64. * @param string $class The fully-qualified class name.
  65. * @return string|false The mapped file name on success, or boolean false on
  66. * failure.
  67. */
  68. public function loadClass($class)
  69. {
  70. $prefix = $class;
  71. while (($pos = strrpos($prefix, '\\')) !== false) {
  72. $prefix = substr($class, 0, $pos + 1);
  73. $relativeClass = substr($class, $pos + 1);
  74. $mappedFile = $this->_loadMappedFile($prefix, $relativeClass);
  75. if ($mappedFile) {
  76. return $mappedFile;
  77. }
  78. $prefix = rtrim($prefix, '\\');
  79. }
  80. return false;
  81. }
  82. /**
  83. * Load the mapped file for a namespace prefix and relative class.
  84. *
  85. * @param string $prefix The namespace prefix.
  86. * @param string $relativeClass The relative class name.
  87. * @return mixed Boolean false if no mapped file can be loaded, or the
  88. * name of the mapped file that was loaded.
  89. */
  90. protected function _loadMappedFile($prefix, $relativeClass)
  91. {
  92. if (!isset($this->_prefixes[$prefix])) {
  93. return false;
  94. }
  95. foreach ($this->_prefixes[$prefix] as $baseDir) {
  96. $file = $baseDir . str_replace('\\', DIRECTORY_SEPARATOR, $relativeClass) . '.php';
  97. if ($this->_requireFile($file)) {
  98. return $file;
  99. }
  100. }
  101. return false;
  102. }
  103. /**
  104. * If a file exists, require it from the file system.
  105. *
  106. * @param string $file The file to require.
  107. * @return bool True if the file exists, false if not.
  108. */
  109. protected function _requireFile($file)
  110. {
  111. if (file_exists($file)) {
  112. require $file;
  113. return true;
  114. }
  115. return false;
  116. }
  117. }