autoloader.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2013-2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC;
  9. use OCP\App\AppPathNotFoundException;
  10. use OCP\App\IAppManager;
  11. use OCP\AutoloadNotAllowedException;
  12. use OCP\ICache;
  13. use Psr\Log\LoggerInterface;
  14. class Autoloader {
  15. /** @var bool */
  16. private $useGlobalClassPath = true;
  17. /** @var array */
  18. private $validRoots = [];
  19. /**
  20. * Optional low-latency memory cache for class to path mapping.
  21. *
  22. * @var \OC\Memcache\Cache
  23. */
  24. protected $memoryCache;
  25. /**
  26. * Autoloader constructor.
  27. *
  28. * @param string[] $validRoots
  29. */
  30. public function __construct(array $validRoots) {
  31. foreach ($validRoots as $root) {
  32. $this->validRoots[$root] = true;
  33. }
  34. }
  35. /**
  36. * Add a path to the list of valid php roots for auto loading
  37. *
  38. * @param string $root
  39. */
  40. public function addValidRoot(string $root): void {
  41. $root = stream_resolve_include_path($root);
  42. $this->validRoots[$root] = true;
  43. }
  44. /**
  45. * disable the usage of the global classpath \OC::$CLASSPATH
  46. */
  47. public function disableGlobalClassPath(): void {
  48. $this->useGlobalClassPath = false;
  49. }
  50. /**
  51. * enable the usage of the global classpath \OC::$CLASSPATH
  52. */
  53. public function enableGlobalClassPath(): void {
  54. $this->useGlobalClassPath = true;
  55. }
  56. /**
  57. * get the possible paths for a class
  58. *
  59. * @param string $class
  60. * @return array an array of possible paths
  61. */
  62. public function findClass(string $class): array {
  63. $class = trim($class, '\\');
  64. $paths = [];
  65. if ($this->useGlobalClassPath && array_key_exists($class, \OC::$CLASSPATH)) {
  66. $paths[] = \OC::$CLASSPATH[$class];
  67. /**
  68. * @TODO: Remove this when necessary
  69. * Remove "apps/" from inclusion path for smooth migration to multi app dir
  70. */
  71. if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
  72. \OCP\Server::get(LoggerInterface::class)->debug('include path for class "' . $class . '" starts with "apps/"', ['app' => 'core']);
  73. $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
  74. }
  75. } elseif (strpos($class, 'OC_') === 0) {
  76. $paths[] = \OC::$SERVERROOT . '/lib/private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  77. } elseif (strpos($class, 'OCA\\') === 0) {
  78. [, $app, $rest] = explode('\\', $class, 3);
  79. $app = strtolower($app);
  80. try {
  81. $appPath = \OCP\Server::get(IAppManager::class)->getAppPath($app);
  82. if (stream_resolve_include_path($appPath)) {
  83. $paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  84. // If not found in the root of the app directory, insert '/lib' after app id and try again.
  85. $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  86. }
  87. } catch (AppPathNotFoundException) {
  88. // App not found, ignore
  89. }
  90. } elseif ($class === 'Test\\TestCase') {
  91. // This File is considered public API, so we make sure that the class
  92. // can still be loaded, although the PSR-4 paths have not been loaded.
  93. $paths[] = \OC::$SERVERROOT . '/tests/lib/TestCase.php';
  94. }
  95. return $paths;
  96. }
  97. /**
  98. * @param string $fullPath
  99. * @return bool
  100. * @throws AutoloadNotAllowedException
  101. */
  102. protected function isValidPath(string $fullPath): bool {
  103. foreach ($this->validRoots as $root => $true) {
  104. if (substr($fullPath, 0, strlen($root) + 1) === $root . '/') {
  105. return true;
  106. }
  107. }
  108. throw new AutoloadNotAllowedException($fullPath);
  109. }
  110. /**
  111. * Load the specified class
  112. *
  113. * @param string $class
  114. * @return bool
  115. * @throws AutoloadNotAllowedException
  116. */
  117. public function load(string $class): bool {
  118. $pathsToRequire = null;
  119. if ($this->memoryCache) {
  120. $pathsToRequire = $this->memoryCache->get($class);
  121. }
  122. if (class_exists($class, false)) {
  123. return false;
  124. }
  125. if (!is_array($pathsToRequire)) {
  126. // No cache or cache miss
  127. $pathsToRequire = [];
  128. foreach ($this->findClass($class) as $path) {
  129. $fullPath = stream_resolve_include_path($path);
  130. if ($fullPath && $this->isValidPath($fullPath)) {
  131. $pathsToRequire[] = $fullPath;
  132. }
  133. }
  134. if ($this->memoryCache) {
  135. $this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
  136. }
  137. }
  138. foreach ($pathsToRequire as $fullPath) {
  139. require_once $fullPath;
  140. }
  141. return false;
  142. }
  143. /**
  144. * Sets the optional low-latency cache for class to path mapping.
  145. *
  146. * @param ICache $memoryCache Instance of memory cache.
  147. */
  148. public function setMemoryCache(?ICache $memoryCache = null): void {
  149. $this->memoryCache = $memoryCache;
  150. }
  151. }