autoloader.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\AutoloadNotAllowedException;
  37. use OCP\ILogger;
  38. class Autoloader {
  39. /** @var bool */
  40. private $useGlobalClassPath = true;
  41. /** @var array */
  42. private $validRoots = [];
  43. /**
  44. * Optional low-latency memory cache for class to path mapping.
  45. *
  46. * @var \OC\Memcache\Cache
  47. */
  48. protected $memoryCache;
  49. /**
  50. * Autoloader constructor.
  51. *
  52. * @param string[] $validRoots
  53. */
  54. public function __construct(array $validRoots) {
  55. foreach ($validRoots as $root) {
  56. $this->validRoots[$root] = true;
  57. }
  58. }
  59. /**
  60. * Add a path to the list of valid php roots for auto loading
  61. *
  62. * @param string $root
  63. */
  64. public function addValidRoot(string $root): void {
  65. $root = stream_resolve_include_path($root);
  66. $this->validRoots[$root] = true;
  67. }
  68. /**
  69. * disable the usage of the global classpath \OC::$CLASSPATH
  70. */
  71. public function disableGlobalClassPath(): void {
  72. $this->useGlobalClassPath = false;
  73. }
  74. /**
  75. * enable the usage of the global classpath \OC::$CLASSPATH
  76. */
  77. public function enableGlobalClassPath(): void {
  78. $this->useGlobalClassPath = true;
  79. }
  80. /**
  81. * get the possible paths for a class
  82. *
  83. * @param string $class
  84. * @return array an array of possible paths
  85. */
  86. public function findClass(string $class): array {
  87. $class = trim($class, '\\');
  88. $paths = [];
  89. if ($this->useGlobalClassPath && array_key_exists($class, \OC::$CLASSPATH)) {
  90. $paths[] = \OC::$CLASSPATH[$class];
  91. /**
  92. * @TODO: Remove this when necessary
  93. * Remove "apps/" from inclusion path for smooth migration to multi app dir
  94. */
  95. if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
  96. \OCP\Util::writeLog('core', 'include path for class "' . $class . '" starts with "apps/"', ILogger::DEBUG);
  97. $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
  98. }
  99. } elseif (strpos($class, 'OC_') === 0) {
  100. $paths[] = \OC::$SERVERROOT . '/lib/private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  101. } elseif (strpos($class, 'OCA\\') === 0) {
  102. [, $app, $rest] = explode('\\', $class, 3);
  103. $app = strtolower($app);
  104. $appPath = \OC_App::getAppPath($app);
  105. if ($appPath && stream_resolve_include_path($appPath)) {
  106. $paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  107. // If not found in the root of the app directory, insert '/lib' after app id and try again.
  108. $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  109. }
  110. } elseif ($class === 'Test\\TestCase') {
  111. // This File is considered public API, so we make sure that the class
  112. // can still be loaded, although the PSR-4 paths have not been loaded.
  113. $paths[] = \OC::$SERVERROOT . '/tests/lib/TestCase.php';
  114. }
  115. return $paths;
  116. }
  117. /**
  118. * @param string $fullPath
  119. * @return bool
  120. * @throws AutoloadNotAllowedException
  121. */
  122. protected function isValidPath(string $fullPath): bool {
  123. foreach ($this->validRoots as $root => $true) {
  124. if (substr($fullPath, 0, strlen($root) + 1) === $root . '/') {
  125. return true;
  126. }
  127. }
  128. throw new AutoloadNotAllowedException($fullPath);
  129. }
  130. /**
  131. * Load the specified class
  132. *
  133. * @param string $class
  134. * @return bool
  135. * @throws AutoloadNotAllowedException
  136. */
  137. public function load(string $class): bool {
  138. $pathsToRequire = null;
  139. if ($this->memoryCache) {
  140. $pathsToRequire = $this->memoryCache->get($class);
  141. }
  142. if (class_exists($class, false)) {
  143. return false;
  144. }
  145. if (!is_array($pathsToRequire)) {
  146. // No cache or cache miss
  147. $pathsToRequire = [];
  148. foreach ($this->findClass($class) as $path) {
  149. $fullPath = stream_resolve_include_path($path);
  150. if ($fullPath && $this->isValidPath($fullPath)) {
  151. $pathsToRequire[] = $fullPath;
  152. }
  153. }
  154. if ($this->memoryCache) {
  155. $this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
  156. }
  157. }
  158. foreach ($pathsToRequire as $fullPath) {
  159. require_once $fullPath;
  160. }
  161. return false;
  162. }
  163. /**
  164. * Sets the optional low-latency cache for class to path mapping.
  165. *
  166. * @param \OC\Memcache\Cache $memoryCache Instance of memory cache.
  167. */
  168. public function setMemoryCache(\OC\Memcache\Cache $memoryCache = null): void {
  169. $this->memoryCache = $memoryCache;
  170. }
  171. }