1
0

autoloader.php 5.4 KB

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