autoloader.php 5.3 KB

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