1
0

autoloader.php 5.4 KB

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