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.

autoloader.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC;
  9. class Autoloader {
  10. private $useGlobalClassPath = true;
  11. private $prefixPaths = array();
  12. private $classPaths = array();
  13. /**
  14. * Optional low-latency memory cache for class to path mapping.
  15. * @var \OC\Memcache\Cache
  16. */
  17. protected $memoryCache;
  18. /**
  19. * Add a custom prefix to the autoloader
  20. *
  21. * @param string $prefix
  22. * @param string $path
  23. */
  24. public function registerPrefix($prefix, $path) {
  25. $this->prefixPaths[$prefix] = $path;
  26. }
  27. /**
  28. * Add a custom classpath to the autoloader
  29. *
  30. * @param string $class
  31. * @param string $path
  32. */
  33. public function registerClass($class, $path) {
  34. $this->classPaths[$class] = $path;
  35. }
  36. /**
  37. * disable the usage of the global classpath \OC::$CLASSPATH
  38. */
  39. public function disableGlobalClassPath() {
  40. $this->useGlobalClassPath = false;
  41. }
  42. /**
  43. * enable the usage of the global classpath \OC::$CLASSPATH
  44. */
  45. public function enableGlobalClassPath() {
  46. $this->useGlobalClassPath = true;
  47. }
  48. /**
  49. * get the possible paths for a class
  50. *
  51. * @param string $class
  52. * @return array|bool an array of possible paths or false if the class is not part of ownCloud
  53. */
  54. public function findClass($class) {
  55. $class = trim($class, '\\');
  56. $paths = array();
  57. if (array_key_exists($class, $this->classPaths)) {
  58. $paths[] = $this->classPaths[$class];
  59. } else if ($this->useGlobalClassPath and array_key_exists($class, \OC::$CLASSPATH)) {
  60. $paths[] = \OC::$CLASSPATH[$class];
  61. /**
  62. * @TODO: Remove this when necessary
  63. * Remove "apps/" from inclusion path for smooth migration to mutli app dir
  64. */
  65. if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
  66. \OC_Log::write('core', 'include path for class "' . $class . '" starts with "apps/"', \OC_Log::DEBUG);
  67. $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
  68. }
  69. } elseif (strpos($class, 'OC_') === 0) {
  70. // first check for legacy classes if underscores are used
  71. $paths[] = 'private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  72. $paths[] = 'private/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  73. } elseif (strpos($class, 'OC\\') === 0) {
  74. $paths[] = 'private/' . strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
  75. $paths[] = strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
  76. } elseif (strpos($class, 'OCP\\') === 0) {
  77. $paths[] = 'public/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php');
  78. } elseif (strpos($class, 'OCA\\') === 0) {
  79. list(, $app, $rest) = explode('\\', $class, 3);
  80. $app = strtolower($app);
  81. foreach (\OC::$APPSROOTS as $appDir) {
  82. if (stream_resolve_include_path($appDir['path'] . '/' . $app)) {
  83. $paths[] = $appDir['path'] . '/' . $app . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  84. // If not found in the root of the app directory, insert '/lib' after app id and try again.
  85. $paths[] = $appDir['path'] . '/' . $app . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  86. }
  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. } else {
  93. foreach ($this->prefixPaths as $prefix => $dir) {
  94. if (0 === strpos($class, $prefix)) {
  95. $path = str_replace('\\', '/', $class) . '.php';
  96. $path = str_replace('_', '/', $path);
  97. $paths[] = $dir . '/' . $path;
  98. }
  99. }
  100. }
  101. return $paths;
  102. }
  103. /**
  104. * Load the specified class
  105. *
  106. * @param string $class
  107. * @return bool
  108. */
  109. public function load($class) {
  110. $pathsToRequire = null;
  111. if ($this->memoryCache) {
  112. $pathsToRequire = $this->memoryCache->get($class);
  113. }
  114. if (!is_array($pathsToRequire)) {
  115. // No cache or cache miss
  116. $pathsToRequire = array();
  117. foreach ($this->findClass($class) as $path) {
  118. $fullPath = stream_resolve_include_path($path);
  119. if ($fullPath) {
  120. $pathsToRequire[] = $fullPath;
  121. }
  122. }
  123. if ($this->memoryCache) {
  124. $this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
  125. }
  126. }
  127. foreach ($pathsToRequire as $fullPath) {
  128. require_once $fullPath;
  129. }
  130. return false;
  131. }
  132. /**
  133. * @brief Sets the optional low-latency cache for class to path mapping.
  134. * @param \OC\Memcache\Cache $memoryCache Instance of memory cache.
  135. */
  136. public function setMemoryCache(\OC\Memcache\Cache $memoryCache = null) {
  137. $this->memoryCache = $memoryCache;
  138. }
  139. }