123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- declare(strict_types=1);
- namespace OC;
- use OCP\App\AppPathNotFoundException;
- use OCP\App\IAppManager;
- use OCP\AutoloadNotAllowedException;
- use OCP\ICache;
- use Psr\Log\LoggerInterface;
- 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\Server::get(LoggerInterface::class)->debug('include path for class "' . $class . '" starts with "apps/"', ['app' => 'core']);
- $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);
- try {
- $appPath = \OCP\Server::get(IAppManager::class)->getAppPath($app);
- if (stream_resolve_include_path($appPath)) {
- $paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
-
- $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
- }
- } catch (AppPathNotFoundException) {
-
- }
- } 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;
- }
- }
|