autoloader.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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[] = 'legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  67. $paths[] = strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  68. } elseif (strpos($class, 'OC\\') === 0) {
  69. $paths[] = strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
  70. } elseif (strpos($class, 'OCP\\') === 0) {
  71. $paths[] = 'public/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php');
  72. } elseif (strpos($class, 'OCA\\') === 0) {
  73. list(, $app, $rest) = explode('\\', $class, 3);
  74. $app = strtolower($app);
  75. foreach (\OC::$APPSROOTS as $appDir) {
  76. if (stream_resolve_include_path($appDir['path'] . '/' . $app)) {
  77. $paths[] = $appDir['path'] . '/' . $app . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  78. // If not found in the root of the app directory, insert '/lib' after app id and try again.
  79. $paths[] = $appDir['path'] . '/' . $app . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  80. }
  81. }
  82. } elseif (strpos($class, 'Test_') === 0) {
  83. $paths[] = 'tests/lib/' . strtolower(str_replace('_', '/', substr($class, 5)) . '.php');
  84. } elseif (strpos($class, 'Test\\') === 0) {
  85. $paths[] = 'tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php');
  86. } else {
  87. foreach ($this->prefixPaths as $prefix => $dir) {
  88. if (0 === strpos($class, $prefix)) {
  89. $path = str_replace('\\', '/', $class) . '.php';
  90. $path = str_replace('_', '/', $path);
  91. $paths[] = $dir . '/' . $path;
  92. }
  93. }
  94. }
  95. return $paths;
  96. }
  97. /**
  98. * Load the specified class
  99. *
  100. * @param string $class
  101. * @return bool
  102. */
  103. protected $memoryCache = null;
  104. protected $constructingMemoryCache = true; // hack to prevent recursion
  105. public function load($class) {
  106. // Does this PHP have an in-memory cache? We cache the paths there
  107. if ($this->constructingMemoryCache && !$this->memoryCache) {
  108. $this->constructingMemoryCache = false;
  109. $this->memoryCache = \OC\Memcache\Factory::createLowLatency('Autoloader');
  110. }
  111. if ($this->memoryCache) {
  112. $pathsToRequire = $this->memoryCache->get($class);
  113. if (is_array($pathsToRequire)) {
  114. foreach ($pathsToRequire as $path) {
  115. require_once $path;
  116. }
  117. return false;
  118. }
  119. }
  120. // Use the normal class loading path
  121. $paths = $this->findClass($class);
  122. if (is_array($paths)) {
  123. $pathsToRequire = array();
  124. foreach ($paths as $path) {
  125. if ($fullPath = stream_resolve_include_path($path)) {
  126. require_once $fullPath;
  127. $pathsToRequire[] = $fullPath;
  128. }
  129. }
  130. // Save in our memory cache
  131. if ($this->memoryCache) {
  132. $this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
  133. }
  134. }
  135. return false;
  136. }
  137. }