Skip to content

Commit

Permalink
fix: Migrate away from OC_App toward the IAppManager
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Mar 6, 2024
1 parent dc67825 commit f281499
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 94 deletions.
3 changes: 2 additions & 1 deletion console.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ function exceptionHandler($exception) {
\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();
Expand Down
3 changes: 3 additions & 0 deletions core/Command/App/Enable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down
27 changes: 19 additions & 8 deletions core/Command/App/GetPath.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand All @@ -23,12 +26,20 @@
namespace OC\Core\Command\App;

use OC\Core\Command\Base;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GetPath extends Base {
public function __construct(
protected IAppManager $appManager,
) {
parent::__construct();
}

protected function configure(): void {
parent::configure();

Expand All @@ -52,14 +63,14 @@ protected function configure(): void {
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$appName = $input->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;
}

/**
Expand All @@ -69,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
public function completeArgumentValues($argumentName, CompletionContext $context): array {
if ($argumentName === 'app') {
return \OC_App::getAllApps();
return $this->appManager->getInstalledApps();
}
return [];
}
Expand Down
28 changes: 15 additions & 13 deletions core/Command/App/Install.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -36,6 +39,13 @@
use Symfony\Component\Console\Output\OutputInterface;

class Install extends Command {
public function __construct(
protected IAppManager $appManager,
private Installer $installer,
) {
parent::__construct();
}

protected function configure(): void {
$this
->setName('app:install')
Expand Down Expand Up @@ -70,32 +80,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$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');
}

Expand Down
7 changes: 5 additions & 2 deletions core/Command/App/Remove.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2018, Patrik Kernstock <info@pkern.at>
*
Expand Down Expand Up @@ -68,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$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;
}
Expand Down Expand Up @@ -135,7 +138,7 @@ public function completeOptionValues($optionName, CompletionContext $context): a
*/
public function completeArgumentValues($argumentName, CompletionContext $context): array {
if ($argumentName === 'app-id') {
return \OC_App::getAllApps();
return $this->appManager->getInstalledApps();

Check failure

Code scanning / Psalm

UndefinedThisPropertyFetch Error

Instance property OC\Core\Command\App\Remove::$appManager is not defined

Check failure on line 141 in core/Command/App/Remove.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyFetch

core/Command/App/Remove.php:141:11: UndefinedThisPropertyFetch: Instance property OC\Core\Command\App\Remove::$appManager is not defined (see https://psalm.dev/041)
}
return [];
}
Expand Down
7 changes: 5 additions & 2 deletions core/Command/App/Update.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2018, michag86 (michag86@arcor.de)
*
Expand Down Expand Up @@ -88,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}
} elseif ($input->getOption('all') || $input->getOption('showonly')) {
$apps = \OC_App::getAllApps();
$apps = $this->manager->getInstalledApps();
} else {
$output->writeln("<error>Please specify an app to update or \"--all\" to update all updatable apps\"</error>");
return 1;
Expand Down Expand Up @@ -117,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($result === false) {
$output->writeln($appId . ' couldn\'t be updated');
$return = 1;
} elseif ($result === true) {
} else {
$output->writeln($appId . ' updated');
}
}
Expand Down
16 changes: 11 additions & 5 deletions core/Command/L10n/CreateJs.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -26,6 +29,7 @@

use DirectoryIterator;

use OCP\App\IAppManager;
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Command\Command;
Expand All @@ -35,6 +39,12 @@
use UnexpectedValueException;

class CreateJs extends Command implements CompletionAwareInterface {
public function __construct(
protected IAppManager $appManager,
) {
parent::__construct();
}

protected function configure() {
$this
->setName('l10n:createjs')
Expand All @@ -55,11 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$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);
Expand Down
18 changes: 12 additions & 6 deletions lib/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
*/
namespace OC;

use \OCP\AutoloadNotAllowedException;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\AutoloadNotAllowedException;
use OCP\ICache;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -113,11 +115,15 @@ public function findClass(string $class): array {
} 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
Expand Down
46 changes: 12 additions & 34 deletions lib/private/AppFramework/Bootstrap/Coordinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

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;
Expand All @@ -46,44 +48,21 @@
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;

/** @var string[] */
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 {
Expand All @@ -108,11 +87,10 @@ private function registerApps(array $appIds): void {
$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;
}
Expand Down
Loading

0 comments on commit f281499

Please sign in to comment.