autoloader.php 4.6 KB

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. $appPath = \OC_App::getAppPath($app);
  82. if ($appPath && stream_resolve_include_path($appPath)) {
  83. $paths[] = $appPath . '/' . 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[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  86. }
  87. } elseif (strpos($class, 'Test_') === 0) {
  88. $paths[] = 'tests/lib/' . strtolower(str_replace('_', '/', substr($class, 5)) . '.php');
  89. } elseif (strpos($class, 'Test\\') === 0) {
  90. $paths[] = 'tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php');
  91. } else {
  92. foreach ($this->prefixPaths as $prefix => $dir) {
  93. if (0 === strpos($class, $prefix)) {
  94. $path = str_replace('\\', '/', $class) . '.php';
  95. $path = str_replace('_', '/', $path);
  96. $paths[] = $dir . '/' . $path;
  97. }
  98. }
  99. }
  100. return $paths;
  101. }
  102. /**
  103. * Load the specified class
  104. *
  105. * @param string $class
  106. * @return bool
  107. */
  108. public function load($class) {
  109. $pathsToRequire = null;
  110. if ($this->memoryCache) {
  111. $pathsToRequire = $this->memoryCache->get($class);
  112. }
  113. if (!is_array($pathsToRequire)) {
  114. // No cache or cache miss
  115. $pathsToRequire = array();
  116. foreach ($this->findClass($class) as $path) {
  117. $fullPath = stream_resolve_include_path($path);
  118. if ($fullPath) {
  119. $pathsToRequire[] = $fullPath;
  120. }
  121. }
  122. if ($this->memoryCache) {
  123. $this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
  124. }
  125. }
  126. foreach ($pathsToRequire as $fullPath) {
  127. require_once $fullPath;
  128. }
  129. return false;
  130. }
  131. /**
  132. * Sets the optional low-latency cache for class to path mapping.
  133. * @param \OC\Memcache\Cache $memoryCache Instance of memory cache.
  134. */
  135. public function setMemoryCache(\OC\Memcache\Cache $memoryCache = null) {
  136. $this->memoryCache = $memoryCache;
  137. }
  138. }