autoloader.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. class Autoloader {
  29. private $useGlobalClassPath = true;
  30. private $prefixPaths = array();
  31. private $classPaths = array();
  32. /**
  33. * Optional low-latency memory cache for class to path mapping.
  34. * @var \OC\Memcache\Cache
  35. */
  36. protected $memoryCache;
  37. /**
  38. * disable the usage of the global classpath \OC::$CLASSPATH
  39. */
  40. public function disableGlobalClassPath() {
  41. $this->useGlobalClassPath = false;
  42. }
  43. /**
  44. * enable the usage of the global classpath \OC::$CLASSPATH
  45. */
  46. public function enableGlobalClassPath() {
  47. $this->useGlobalClassPath = true;
  48. }
  49. /**
  50. * get the possible paths for a class
  51. *
  52. * @param string $class
  53. * @return array|bool an array of possible paths or false if the class is not part of ownCloud
  54. */
  55. public function findClass($class) {
  56. $class = trim($class, '\\');
  57. $paths = array();
  58. if (array_key_exists($class, $this->classPaths)) {
  59. $paths[] = $this->classPaths[$class];
  60. } else if ($this->useGlobalClassPath and array_key_exists($class, \OC::$CLASSPATH)) {
  61. $paths[] = \OC::$CLASSPATH[$class];
  62. /**
  63. * @TODO: Remove this when necessary
  64. * Remove "apps/" from inclusion path for smooth migration to mutli app dir
  65. */
  66. if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
  67. \OC_Log::write('core', 'include path for class "' . $class . '" starts with "apps/"', \OC_Log::DEBUG);
  68. $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
  69. }
  70. } elseif (strpos($class, 'OC_') === 0) {
  71. // first check for legacy classes if underscores are used
  72. $paths[] = 'private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  73. $paths[] = 'private/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  74. } elseif (strpos($class, 'OC\\') === 0) {
  75. $paths[] = 'private/' . strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
  76. $paths[] = strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
  77. } elseif (strpos($class, 'OCP\\') === 0) {
  78. $paths[] = 'public/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php');
  79. } elseif (strpos($class, 'OCA\\') === 0) {
  80. list(, $app, $rest) = explode('\\', $class, 3);
  81. $app = strtolower($app);
  82. $appPath = \OC_App::getAppPath($app);
  83. if ($appPath && stream_resolve_include_path($appPath)) {
  84. $paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  85. // If not found in the root of the app directory, insert '/lib' after app id and try again.
  86. $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  87. }
  88. } elseif (strpos($class, 'Test_') === 0) {
  89. $paths[] = 'tests/lib/' . strtolower(str_replace('_', '/', substr($class, 5)) . '.php');
  90. } elseif (strpos($class, 'Test\\') === 0) {
  91. $paths[] = 'tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php');
  92. }
  93. return $paths;
  94. }
  95. /**
  96. * Load the specified class
  97. *
  98. * @param string $class
  99. * @return bool
  100. */
  101. public function load($class) {
  102. $pathsToRequire = null;
  103. if ($this->memoryCache) {
  104. $pathsToRequire = $this->memoryCache->get($class);
  105. }
  106. if (!is_array($pathsToRequire)) {
  107. // No cache or cache miss
  108. $pathsToRequire = array();
  109. foreach ($this->findClass($class) as $path) {
  110. $fullPath = stream_resolve_include_path($path);
  111. if ($fullPath) {
  112. $pathsToRequire[] = $fullPath;
  113. }
  114. }
  115. if ($this->memoryCache) {
  116. $this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
  117. }
  118. }
  119. foreach ($pathsToRequire as $fullPath) {
  120. require_once $fullPath;
  121. }
  122. return false;
  123. }
  124. /**
  125. * Sets the optional low-latency cache for class to path mapping.
  126. * @param \OC\Memcache\Cache $memoryCache Instance of memory cache.
  127. */
  128. public function setMemoryCache(\OC\Memcache\Cache $memoryCache = null) {
  129. $this->memoryCache = $memoryCache;
  130. }
  131. }