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.

183 lines
5.5 KiB

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