From 644036ab4e3d1d6f175a485ed7ae4883c668ff48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Wed, 6 Mar 2024 11:54:58 +0100 Subject: [PATCH] fix: Migrate away from OC_App toward the IAppManager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- console.php | 3 +- core/Command/App/Enable.php | 3 ++ core/Command/App/GetPath.php | 27 +++++++---- core/Command/App/Install.php | 28 +++++------ core/Command/App/Remove.php | 7 ++- core/Command/App/Update.php | 7 ++- core/Command/L10n/CreateJs.php | 16 +++++-- lib/autoloader.php | 16 +++++-- .../AppFramework/Bootstrap/Coordinator.php | 46 +++++-------------- lib/private/Console/Application.php | 37 ++++++--------- lib/private/legacy/OC_App.php | 2 +- 11 files changed, 99 insertions(+), 93 deletions(-) diff --git a/console.php b/console.php index 68647a874c4..afa8b1b6be1 100644 --- a/console.php +++ b/console.php @@ -107,7 +107,8 @@ try { \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class), \OC::$server->getRequest(), \OC::$server->get(\Psr\Log\LoggerInterface::class), - \OC::$server->query(\OC\MemoryInfo::class) + \OC::$server->query(\OC\MemoryInfo::class), + \OCP\Server::get(\OCP\App\IAppManager::class), ); $application->loadCommands(new ArgvInput(), new ConsoleOutput()); $application->run(); diff --git a/core/Command/App/Enable.php b/core/Command/App/Enable.php index ed29f2fed92..579432a1dd0 100644 --- a/core/Command/App/Enable.php +++ b/core/Command/App/Enable.php @@ -1,4 +1,7 @@ getArgument('app'); - $path = \OC_App::getAppPath($appName); - if ($path !== false) { - $output->writeln($path); - return 0; + try { + $path = $this->appManager->getAppPath($appName); + } catch (AppPathNotFoundException) { + // App not found, exit with non-zero + return self::FAILURE; } - - // App not found, exit with non-zero - return 1; + $output->writeln($path); + return self::SUCCESS; } /** @@ -69,7 +80,7 @@ class GetPath extends Base { */ public function completeArgumentValues($argumentName, CompletionContext $context): array { if ($argumentName === 'app') { - return \OC_App::getAllApps(); + return $this->appManager->getInstalledApps(); } return []; } diff --git a/core/Command/App/Install.php b/core/Command/App/Install.php index 2d02fff4dbf..a5657e8397d 100644 --- a/core/Command/App/Install.php +++ b/core/Command/App/Install.php @@ -1,4 +1,7 @@ setName('app:install') @@ -70,32 +80,24 @@ class Install extends Command { $appId = $input->getArgument('app-id'); $forceEnable = (bool) $input->getOption('force'); - if (\OC_App::getAppPath($appId)) { + if ($this->appManager->isInstalled($appId)) { $output->writeln($appId . ' already installed'); return 1; } try { - /** @var Installer $installer */ - $installer = \OC::$server->query(Installer::class); - $installer->downloadApp($appId, $input->getOption('allow-unstable')); - $result = $installer->installApp($appId, $forceEnable); + $this->installer->downloadApp($appId, $input->getOption('allow-unstable')); + $result = $this->installer->installApp($appId, $forceEnable); } catch (\Exception $e) { $output->writeln('Error: ' . $e->getMessage()); return 1; } - if ($result === false) { - $output->writeln($appId . ' couldn\'t be installed'); - return 1; - } - - $appVersion = \OCP\Server::get(IAppManager::class)->getAppVersion($appId); + $appVersion = $this->appManager->getAppVersion($appId); $output->writeln($appId . ' ' . $appVersion . ' installed'); if (!$input->getOption('keep-disabled')) { - $appClass = new \OC_App(); - $appClass->enable($appId); + $this->appManager->enableApp($appId); $output->writeln($appId . ' enabled'); } diff --git a/core/Command/App/Remove.php b/core/Command/App/Remove.php index 5fa05079bd8..d017a95c681 100644 --- a/core/Command/App/Remove.php +++ b/core/Command/App/Remove.php @@ -1,4 +1,7 @@ * @@ -68,7 +71,7 @@ class Remove extends Command implements CompletionAwareInterface { $appId = $input->getArgument('app-id'); // Check if the app is installed - if (!\OC_App::getAppPath($appId)) { + if (!$this->manager->isInstalled($appId)) { $output->writeln($appId . ' is not installed'); return 1; } @@ -135,7 +138,7 @@ class Remove extends Command implements CompletionAwareInterface { */ public function completeArgumentValues($argumentName, CompletionContext $context): array { if ($argumentName === 'app-id') { - return \OC_App::getAllApps(); + return $this->appManager->getInstalledApps(); } return []; } diff --git a/core/Command/App/Update.php b/core/Command/App/Update.php index c8e62cb5b71..6047ac6b843 100644 --- a/core/Command/App/Update.php +++ b/core/Command/App/Update.php @@ -1,4 +1,7 @@ getOption('all') || $input->getOption('showonly')) { - $apps = \OC_App::getAllApps(); + $apps = $this->manager->getInstalledApps(); } else { $output->writeln("Please specify an app to update or \"--all\" to update all updatable apps\""); return 1; @@ -117,7 +120,7 @@ class Update extends Command { if ($result === false) { $output->writeln($appId . ' couldn\'t be updated'); $return = 1; - } elseif ($result === true) { + } else { $output->writeln($appId . ' updated'); } } diff --git a/core/Command/L10n/CreateJs.php b/core/Command/L10n/CreateJs.php index 6472e7aec1c..724e94f28d5 100644 --- a/core/Command/L10n/CreateJs.php +++ b/core/Command/L10n/CreateJs.php @@ -1,4 +1,7 @@ setName('l10n:createjs') @@ -55,11 +65,7 @@ class CreateJs extends Command implements CompletionAwareInterface { $app = $input->getArgument('app'); $lang = $input->getArgument('lang'); - $path = \OC_App::getAppPath($app); - if ($path === false) { - $output->writeln("The app <$app> is unknown."); - return 1; - } + $path = $this->appManager->getAppPath($app); $languages = $lang; if (empty($lang)) { $languages = $this->getAllLanguages($path); diff --git a/lib/autoloader.php b/lib/autoloader.php index 0f8a2ad9687..773813a32cf 100644 --- a/lib/autoloader.php +++ b/lib/autoloader.php @@ -36,6 +36,8 @@ declare(strict_types=1); */ namespace OC; +use OCP\App\AppPathNotFoundException; +use OCP\App\IAppManager; use OCP\AutoloadNotAllowedException; use OCP\ICache; use Psr\Log\LoggerInterface; @@ -113,11 +115,15 @@ class Autoloader { } 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'); - // If not found in the root of the app directory, insert '/lib' after app id and try again. - $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php'); + try { + $appPath = \OCP\Server::get(IAppManager::class)->getAppPath($app); + if (stream_resolve_include_path($appPath)) { + $paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php'); + // If not found in the root of the app directory, insert '/lib' after app id and try again. + $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php'); + } + } catch (AppPathNotFoundException) { + // App not found, ignore } } elseif ($class === 'Test\\TestCase') { // This File is considered public API, so we make sure that the class diff --git a/lib/private/AppFramework/Bootstrap/Coordinator.php b/lib/private/AppFramework/Bootstrap/Coordinator.php index 8526a3dc1a1..6c49e7dd943 100644 --- a/lib/private/AppFramework/Bootstrap/Coordinator.php +++ b/lib/private/AppFramework/Bootstrap/Coordinator.php @@ -32,6 +32,8 @@ namespace OC\AppFramework\Bootstrap; use OC\Support\CrashReport\Registry; use OC_App; +use OCP\App\AppPathNotFoundException; +use OCP\App\IAppManager; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\QueryException; @@ -46,24 +48,6 @@ use function class_implements; use function in_array; class Coordinator { - /** @var IServerContainer */ - private $serverContainer; - - /** @var Registry */ - private $registry; - - /** @var IManager */ - private $dashboardManager; - - /** @var IEventDispatcher */ - private $eventDispatcher; - - /** @var IEventLogger */ - private $eventLogger; - - /** @var LoggerInterface */ - private $logger; - /** @var RegistrationContext|null */ private $registrationContext; @@ -71,19 +55,14 @@ class Coordinator { private $bootedApps = []; public function __construct( - IServerContainer $container, - Registry $registry, - IManager $dashboardManager, - IEventDispatcher $eventListener, - IEventLogger $eventLogger, - LoggerInterface $logger + private IServerContainer $serverContainer, + private Registry $registry, + private IManager $dashboardManager, + private IEventDispatcher $eventDispatcher, + private IEventLogger $eventLogger, + private IAppManager $appManager, + private LoggerInterface $logger, ) { - $this->serverContainer = $container; - $this->registry = $registry; - $this->dashboardManager = $dashboardManager; - $this->eventDispatcher = $eventListener; - $this->eventLogger = $eventLogger; - $this->logger = $logger; } public function runInitialRegistration(): void { @@ -108,11 +87,10 @@ class Coordinator { $this->eventLogger->start("bootstrap:register_app:$appId:autoloader", "Setup autoloader for $appId"); /* * First, we have to enable the app's autoloader - * - * @todo use $this->appManager->getAppPath($appId) here */ - $path = OC_App::getAppPath($appId); - if ($path === false) { + try { + $path = $this->appManager->getAppPath($appId); + } catch (AppPathNotFoundException) { // Ignore continue; } diff --git a/lib/private/Console/Application.php b/lib/private/Console/Application.php index ec8d5900f36..ae8d7c2a33c 100644 --- a/lib/private/Console/Application.php +++ b/lib/private/Console/Application.php @@ -32,7 +32,7 @@ namespace OC\Console; use OC\MemoryInfo; use OC\NeedsUpdateException; -use OC_App; +use OCP\App\AppPathNotFoundException; use OCP\App\IAppManager; use OCP\Console\ConsoleEvent; use OCP\EventDispatcher\IEventDispatcher; @@ -47,25 +47,18 @@ use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; class Application { - private IConfig $config; private SymfonyApplication $application; - private IEventDispatcher $dispatcher; - private IRequest $request; - private LoggerInterface $logger; - private MemoryInfo $memoryInfo; - public function __construct(IConfig $config, - IEventDispatcher $dispatcher, - IRequest $request, - LoggerInterface $logger, - MemoryInfo $memoryInfo) { + public function __construct( + private IConfig $config, + private IEventDispatcher $dispatcher, + private IRequest $request, + private LoggerInterface $logger, + private MemoryInfo $memoryInfo, + private IAppManager $appManager, + ) { $defaults = \OC::$server->get('ThemingDefaults'); - $this->config = $config; $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString()); - $this->dispatcher = $dispatcher; - $this->request = $request; - $this->logger = $logger; - $this->memoryInfo = $memoryInfo; } /** @@ -111,15 +104,15 @@ class Application { } elseif ($this->config->getSystemValueBool('maintenance')) { $this->writeMaintenanceModeInfo($input, $output); } else { - OC_App::loadApps(); - $appManager = \OCP\Server::get(IAppManager::class); - foreach ($appManager->getInstalledApps() as $app) { - $appPath = \OC_App::getAppPath($app); - if ($appPath === false) { + $this->appManager->loadApps(); + foreach ($this->appManager->getInstalledApps() as $app) { + try { + $appPath = $this->appManager->getAppPath($app); + } catch (AppPathNotFoundException) { continue; } // load commands using info.xml - $info = $appManager->getAppInfo($app); + $info = $this->appManager->getAppInfo($app); if (isset($info['commands'])) { try { $this->loadCommandsFromInfoXml($info['commands']); diff --git a/lib/private/legacy/OC_App.php b/lib/private/legacy/OC_App.php index c652fcfb9ba..ea79cc3ea76 100644 --- a/lib/private/legacy/OC_App.php +++ b/lib/private/legacy/OC_App.php @@ -354,7 +354,7 @@ class OC_App { * @param string $appId * @param bool $refreshAppPath should be set to true only during install/upgrade * @return string|false - * @deprecated 11.0.0 use \OC::$server->getAppManager()->getAppPath() + * @deprecated 11.0.0 use \OCP\Server::get(IAppManager)->getAppPath() */ public static function getAppPath(string $appId, bool $refreshAppPath = false) { if ($appId === null || trim($appId) === '') {