autoloader.php 5.6 KB

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