PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/autoloader.php

https://github.com/sezuan/core
PHP | 126 lines | 78 code | 11 blank | 37 comment | 3 complexity | d3620358924adf719710d85b31eb2e94 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  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. public function load($class) {
  104. $paths = $this->findClass($class);
  105. if (is_array($paths)) {
  106. foreach ($paths as $path) {
  107. if ($fullPath = stream_resolve_include_path($path)) {
  108. require_once $fullPath;
  109. }
  110. }
  111. }
  112. return false;
  113. }
  114. }