You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autoloader.php 5.3KB

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