autoloader.php 5.5 KB

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