autoloader.php 3.9 KB

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