123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- declare(strict_types=1);
- namespace OC;
- use \OCP\AutoloadNotAllowedException;
- use OCP\ILogger;
- use OCP\ICache;
- class Autoloader {
-
- private $useGlobalClassPath = true;
-
- private $validRoots = [];
-
- protected $memoryCache;
-
- public function __construct(array $validRoots) {
- foreach ($validRoots as $root) {
- $this->validRoots[$root] = true;
- }
- }
-
- public function addValidRoot(string $root): void {
- $root = stream_resolve_include_path($root);
- $this->validRoots[$root] = true;
- }
-
- public function disableGlobalClassPath(): void {
- $this->useGlobalClassPath = false;
- }
-
- public function enableGlobalClassPath(): void {
- $this->useGlobalClassPath = true;
- }
-
- public function findClass(string $class): array {
- $class = trim($class, '\\');
- $paths = [];
- if ($this->useGlobalClassPath && array_key_exists($class, \OC::$CLASSPATH)) {
- $paths[] = \OC::$CLASSPATH[$class];
-
- if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
- \OCP\Util::writeLog('core', 'include path for class "' . $class . '" starts with "apps/"', ILogger::DEBUG);
- $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
- }
- } elseif (strpos($class, 'OC_') === 0) {
- $paths[] = \OC::$SERVERROOT . '/lib/private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
- } elseif (strpos($class, 'OCA\\') === 0) {
- [, $app, $rest] = explode('\\', $class, 3);
- $app = strtolower($app);
- $appPath = \OC_App::getAppPath($app);
- if ($appPath && stream_resolve_include_path($appPath)) {
- $paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
-
- $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
- }
- } elseif ($class === 'Test\\TestCase') {
-
-
- $paths[] = \OC::$SERVERROOT . '/tests/lib/TestCase.php';
- }
- return $paths;
- }
-
- protected function isValidPath(string $fullPath): bool {
- foreach ($this->validRoots as $root => $true) {
- if (substr($fullPath, 0, strlen($root) + 1) === $root . '/') {
- return true;
- }
- }
- throw new AutoloadNotAllowedException($fullPath);
- }
-
- public function load(string $class): bool {
- $pathsToRequire = null;
- if ($this->memoryCache) {
- $pathsToRequire = $this->memoryCache->get($class);
- }
- if (class_exists($class, false)) {
- return false;
- }
- if (!is_array($pathsToRequire)) {
-
- $pathsToRequire = [];
- foreach ($this->findClass($class) as $path) {
- $fullPath = stream_resolve_include_path($path);
- if ($fullPath && $this->isValidPath($fullPath)) {
- $pathsToRequire[] = $fullPath;
- }
- }
- if ($this->memoryCache) {
- $this->memoryCache->set($class, $pathsToRequire, 60);
- }
- }
- foreach ($pathsToRequire as $fullPath) {
- require_once $fullPath;
- }
- return false;
- }
-
- public function setMemoryCache(ICache $memoryCache = null): void {
- $this->memoryCache = $memoryCache;
- }
- }
|