autoloader.php 4.8 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. * Add a custom prefix to the autoloader
  15. *
  16. * @param string $prefix
  17. * @param string $path
  18. */
  19. public function registerPrefix($prefix, $path) {
  20. $this->prefixPaths[$prefix] = $path;
  21. }
  22. /**
  23. * Add a custom classpath to the autoloader
  24. *
  25. * @param string $class
  26. * @param string $path
  27. */
  28. public function registerClass($class, $path) {
  29. $this->classPaths[$class] = $path;
  30. }
  31. /**
  32. * disable the usage of the global classpath \OC::$CLASSPATH
  33. */
  34. public function disableGlobalClassPath() {
  35. $this->useGlobalClassPath = false;
  36. }
  37. /**
  38. * enable the usage of the global classpath \OC::$CLASSPATH
  39. */
  40. public function enableGlobalClassPath() {
  41. $this->useGlobalClassPath = true;
  42. }
  43. /**
  44. * get the possible paths for a class
  45. *
  46. * @param string $class
  47. * @return array|bool an array of possible paths or false if the class is not part of ownCloud
  48. */
  49. public function findClass($class) {
  50. $class = trim($class, '\\');
  51. $paths = array();
  52. if (array_key_exists($class, $this->classPaths)) {
  53. $paths[] = $this->classPaths[$class];
  54. } else if ($this->useGlobalClassPath and array_key_exists($class, \OC::$CLASSPATH)) {
  55. $paths[] = \OC::$CLASSPATH[$class];
  56. /**
  57. * @TODO: Remove this when necessary
  58. * Remove "apps/" from inclusion path for smooth migration to mutli app dir
  59. */
  60. if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
  61. \OC_Log::write('core', 'include path for class "' . $class . '" starts with "apps/"', \OC_Log::DEBUG);
  62. $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
  63. }
  64. } elseif (strpos($class, 'OC_') === 0) {
  65. // first check for legacy classes if underscores are used
  66. $paths[] = 'private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  67. $paths[] = 'private/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  68. } elseif (strpos($class, 'OC\\') === 0) {
  69. $paths[] = 'private/' . strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
  70. $paths[] = strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
  71. } elseif (strpos($class, 'OCP\\') === 0) {
  72. $paths[] = 'public/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php');
  73. } elseif (strpos($class, 'OCA\\') === 0) {
  74. list(, $app, $rest) = explode('\\', $class, 3);
  75. $app = strtolower($app);
  76. foreach (\OC::$APPSROOTS as $appDir) {
  77. if (stream_resolve_include_path($appDir['path'] . '/' . $app)) {
  78. $paths[] = $appDir['path'] . '/' . $app . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  79. // If not found in the root of the app directory, insert '/lib' after app id and try again.
  80. $paths[] = $appDir['path'] . '/' . $app . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  81. }
  82. }
  83. } elseif (strpos($class, 'Test_') === 0) {
  84. $paths[] = 'tests/lib/' . strtolower(str_replace('_', '/', substr($class, 5)) . '.php');
  85. } elseif (strpos($class, 'Test\\') === 0) {
  86. $paths[] = 'tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php');
  87. } else {
  88. foreach ($this->prefixPaths as $prefix => $dir) {
  89. if (0 === strpos($class, $prefix)) {
  90. $path = str_replace('\\', '/', $class) . '.php';
  91. $path = str_replace('_', '/', $path);
  92. $paths[] = $dir . '/' . $path;
  93. }
  94. }
  95. }
  96. return $paths;
  97. }
  98. /**
  99. * Load the specified class
  100. *
  101. * @param string $class
  102. * @return bool
  103. */
  104. protected $memoryCache = null;
  105. protected $constructingMemoryCache = true; // hack to prevent recursion
  106. public function load($class) {
  107. // Does this PHP have an in-memory cache? We cache the paths there
  108. if ($this->constructingMemoryCache && !$this->memoryCache) {
  109. $this->constructingMemoryCache = false;
  110. try {
  111. $this->memoryCache = \OC\Memcache\Factory::createLowLatency('Autoloader');
  112. } catch(\Exception $ex) {
  113. // no caching then - fine with me
  114. }
  115. }
  116. if ($this->memoryCache) {
  117. $pathsToRequire = $this->memoryCache->get($class);
  118. if (is_array($pathsToRequire)) {
  119. foreach ($pathsToRequire as $path) {
  120. require_once $path;
  121. }
  122. return false;
  123. }
  124. }
  125. // Use the normal class loading path
  126. $paths = $this->findClass($class);
  127. if (is_array($paths)) {
  128. $pathsToRequire = array();
  129. foreach ($paths as $path) {
  130. if ($fullPath = stream_resolve_include_path($path)) {
  131. require_once $fullPath;
  132. $pathsToRequire[] = $fullPath;
  133. }
  134. }
  135. // Save in our memory cache
  136. if ($this->memoryCache) {
  137. $this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
  138. }
  139. }
  140. return false;
  141. }
  142. }