1
0

autoloader.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Andreas Fischer <bantu@owncloud.com>
  7. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author J0WI <J0WI@users.noreply.github.com>
  11. * @author Joas Schilling <coding@schilljs.com>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Markus Goetz <markus@woboq.com>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Robin Appelman <robin@icewind.nl>
  16. * @author Robin McCorkell <robin@mccorkell.me.uk>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Vincent Petry <vincent@nextcloud.com>
  19. *
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. namespace OC;
  36. use OCP\App\AppPathNotFoundException;
  37. use OCP\App\IAppManager;
  38. use OCP\AutoloadNotAllowedException;
  39. use OCP\ICache;
  40. use Psr\Log\LoggerInterface;
  41. class Autoloader {
  42. /** @var bool */
  43. private $useGlobalClassPath = true;
  44. /** @var array */
  45. private $validRoots = [];
  46. /**
  47. * Optional low-latency memory cache for class to path mapping.
  48. *
  49. * @var \OC\Memcache\Cache
  50. */
  51. protected $memoryCache;
  52. /**
  53. * Autoloader constructor.
  54. *
  55. * @param string[] $validRoots
  56. */
  57. public function __construct(array $validRoots) {
  58. foreach ($validRoots as $root) {
  59. $this->validRoots[$root] = true;
  60. }
  61. }
  62. /**
  63. * Add a path to the list of valid php roots for auto loading
  64. *
  65. * @param string $root
  66. */
  67. public function addValidRoot(string $root): void {
  68. $root = stream_resolve_include_path($root);
  69. $this->validRoots[$root] = true;
  70. }
  71. /**
  72. * disable the usage of the global classpath \OC::$CLASSPATH
  73. */
  74. public function disableGlobalClassPath(): void {
  75. $this->useGlobalClassPath = false;
  76. }
  77. /**
  78. * enable the usage of the global classpath \OC::$CLASSPATH
  79. */
  80. public function enableGlobalClassPath(): void {
  81. $this->useGlobalClassPath = true;
  82. }
  83. /**
  84. * get the possible paths for a class
  85. *
  86. * @param string $class
  87. * @return array an array of possible paths
  88. */
  89. public function findClass(string $class): array {
  90. $class = trim($class, '\\');
  91. $paths = [];
  92. if ($this->useGlobalClassPath && array_key_exists($class, \OC::$CLASSPATH)) {
  93. $paths[] = \OC::$CLASSPATH[$class];
  94. /**
  95. * @TODO: Remove this when necessary
  96. * Remove "apps/" from inclusion path for smooth migration to multi app dir
  97. */
  98. if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
  99. \OCP\Server::get(LoggerInterface::class)->debug('include path for class "' . $class . '" starts with "apps/"', ['app' => 'core']);
  100. $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
  101. }
  102. } elseif (strpos($class, 'OC_') === 0) {
  103. $paths[] = \OC::$SERVERROOT . '/lib/private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  104. } elseif (strpos($class, 'OCA\\') === 0) {
  105. [, $app, $rest] = explode('\\', $class, 3);
  106. $app = strtolower($app);
  107. try {
  108. $appPath = \OCP\Server::get(IAppManager::class)->getAppPath($app);
  109. if (stream_resolve_include_path($appPath)) {
  110. $paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  111. // If not found in the root of the app directory, insert '/lib' after app id and try again.
  112. $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  113. }
  114. } catch (AppPathNotFoundException) {
  115. // App not found, ignore
  116. }
  117. } elseif ($class === 'Test\\TestCase') {
  118. // This File is considered public API, so we make sure that the class
  119. // can still be loaded, although the PSR-4 paths have not been loaded.
  120. $paths[] = \OC::$SERVERROOT . '/tests/lib/TestCase.php';
  121. }
  122. return $paths;
  123. }
  124. /**
  125. * @param string $fullPath
  126. * @return bool
  127. * @throws AutoloadNotAllowedException
  128. */
  129. protected function isValidPath(string $fullPath): bool {
  130. foreach ($this->validRoots as $root => $true) {
  131. if (substr($fullPath, 0, strlen($root) + 1) === $root . '/') {
  132. return true;
  133. }
  134. }
  135. throw new AutoloadNotAllowedException($fullPath);
  136. }
  137. /**
  138. * Load the specified class
  139. *
  140. * @param string $class
  141. * @return bool
  142. * @throws AutoloadNotAllowedException
  143. */
  144. public function load(string $class): bool {
  145. $pathsToRequire = null;
  146. if ($this->memoryCache) {
  147. $pathsToRequire = $this->memoryCache->get($class);
  148. }
  149. if (class_exists($class, false)) {
  150. return false;
  151. }
  152. if (!is_array($pathsToRequire)) {
  153. // No cache or cache miss
  154. $pathsToRequire = [];
  155. foreach ($this->findClass($class) as $path) {
  156. $fullPath = stream_resolve_include_path($path);
  157. if ($fullPath && $this->isValidPath($fullPath)) {
  158. $pathsToRequire[] = $fullPath;
  159. }
  160. }
  161. if ($this->memoryCache) {
  162. $this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
  163. }
  164. }
  165. foreach ($pathsToRequire as $fullPath) {
  166. require_once $fullPath;
  167. }
  168. return false;
  169. }
  170. /**
  171. * Sets the optional low-latency cache for class to path mapping.
  172. *
  173. * @param ICache $memoryCache Instance of memory cache.
  174. */
  175. public function setMemoryCache(?ICache $memoryCache = null): void {
  176. $this->memoryCache = $memoryCache;
  177. }
  178. }