Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Dependency Injection in register_commands.php #43203

Merged
merged 8 commits into from
Feb 1, 2024
6 changes: 5 additions & 1 deletion core/Command/Security/ListCertificates.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@
use OCP\ICertificate;
use OCP\ICertificateManager;
use OCP\IL10N;
use OCP\L10N\IFactory as IL10NFactory;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ListCertificates extends Base {
protected IL10N $l;

public function __construct(
protected ICertificateManager $certificateManager,
protected IL10N $l,
IL10NFactory $l10nFactory,
) {
parent::__construct();
$this->l = $l10nFactory->get('core');
}

protected function configure() {
Expand Down
299 changes: 131 additions & 168 deletions core/register_command.php

Large diffs are not rendered by default.

24 changes: 4 additions & 20 deletions lib/private/Encryption/DecryptAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OC\Encryption\Exceptions\DecryptionFailedException;
use OC\Files\View;
use OCP\Encryption\IEncryptionModule;
use OCP\Encryption\IManager;
use OCP\IUserManager;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -43,31 +44,14 @@ class DecryptAll {
/** @var InputInterface */
protected $input;

/** @var Manager */
protected $encryptionManager;

/** @var IUserManager */
protected $userManager;

/** @var View */
protected $rootView;

/** @var array files which couldn't be decrypted */
protected $failed;

/**
* @param Manager $encryptionManager
* @param IUserManager $userManager
* @param View $rootView
*/
public function __construct(
Manager $encryptionManager,
IUserManager $userManager,
View $rootView
protected IManager $encryptionManager,
protected IUserManager $userManager,
protected View $rootView
) {
$this->encryptionManager = $encryptionManager;
$this->userManager = $userManager;
$this->rootView = $rootView;
$this->failed = [];
}

Expand Down
37 changes: 10 additions & 27 deletions lib/private/Encryption/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,17 @@
use Psr\Log\LoggerInterface;

class Manager implements IManager {
/** @var array */
protected $encryptionModules;

/** @var IConfig */
protected $config;

protected LoggerInterface $logger;

/** @var Il10n */
protected $l;

/** @var View */
protected $rootView;

/** @var Util */
protected $util;

/** @var ArrayCache */
protected $arrayCache;

public function __construct(IConfig $config, LoggerInterface $logger, IL10N $l10n, View $rootView, Util $util, ArrayCache $arrayCache) {
protected array $encryptionModules;

public function __construct(
protected IConfig $config,
protected LoggerInterface $logger,
protected IL10N $l,
protected View $rootView,
protected Util $util,
protected ArrayCache $arrayCache,
) {
$this->encryptionModules = [];
$this->config = $config;
$this->logger = $logger;
$this->l = $l10n;
$this->rootView = $rootView;
$this->util = $util;
$this->arrayCache = $arrayCache;
}

/**
Expand Down
9 changes: 4 additions & 5 deletions lib/private/Migration/BackgroundRepair.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\TimedJob;
use OCP\EventDispatcher\IEventDispatcher;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -42,7 +41,7 @@
*/
class BackgroundRepair extends TimedJob {
public function __construct(
private IEventDispatcher $dispatcher,
private Repair $repair,
ITimeFactory $time,
private LoggerInterface $logger,
private IJobList $jobList,
Expand Down Expand Up @@ -73,9 +72,9 @@ protected function run($argument): void {
}

$step = $argument['step'];
$repair = new Repair([], $this->dispatcher, \OC::$server->get(LoggerInterface::class));
$this->repair->setRepairSteps([]);
try {
$repair->addStep($step);
$this->repair->addStep($step);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage(), [
'app' => 'migration',
Expand All @@ -88,7 +87,7 @@ protected function run($argument): void {
}

// execute the repair step
$repair->run();
$this->repair->run();

// remove the job once executed successfully
$this->jobList->remove($this, $this->argument);
Expand Down
20 changes: 8 additions & 12 deletions lib/private/Repair.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,19 @@

class Repair implements IOutput {
/** @var IRepairStep[] */
private array $repairSteps;

private IEventDispatcher $dispatcher;
private array $repairSteps = [];

private string $currentStep;

private LoggerInterface $logger;
public function __construct(
private IEventDispatcher $dispatcher,
private LoggerInterface $logger
) {
}

/**
* Creates a new repair step runner
*
* @param IRepairStep[] $repairSteps array of RepairStep instances
*/
public function __construct(array $repairSteps, IEventDispatcher $dispatcher, LoggerInterface $logger) {
/** @param IRepairStep[] $repairSteps */
public function setRepairSteps(array $repairSteps): void {
$this->repairSteps = $repairSteps;
$this->dispatcher = $dispatcher;
$this->logger = $logger;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,8 @@ public function __construct($webRoot, \OC\Config $config) {
return $backend;
});

$this->registerService('IntegrityCodeChecker', function (ContainerInterface $c) {
$this->registerDeprecatedAlias('IntegrityCodeChecker', Checker::class);
$this->registerService(Checker::class, function (ContainerInterface $c) {
// IConfig and IAppManager requires a working database. This code
// might however be called when ownCloud is not yet setup.
if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) {
Expand Down
6 changes: 4 additions & 2 deletions lib/private/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ private function doUpgrade(string $currentVersion, string $installedVersion): vo
file_put_contents($this->config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');

// pre-upgrade repairs
$repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class), \OC::$server->get(LoggerInterface::class));
$repair = \OCP\Server::get(Repair::class);
$repair->setRepairSteps(Repair::getBeforeUpgradeRepairSteps());
$repair->run();

$this->doCoreUpgrade();
Expand Down Expand Up @@ -296,7 +297,8 @@ private function doUpgrade(string $currentVersion, string $installedVersion): vo
}

// post-upgrade repairs
$repair = new Repair(Repair::getRepairSteps(), \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class), \OC::$server->get(LoggerInterface::class));
$repair = \OCP\Server::get(Repair::class);
$repair->setRepairSteps(Repair::getRepairSteps());
$repair->run();

//Invalidate update feed
Expand Down
2 changes: 1 addition & 1 deletion lib/private/legacy/OC_App.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ public static function executeRepairSteps(string $appId, array $steps) {
$dispatcher = \OC::$server->get(IEventDispatcher::class);

// load the steps
$r = new Repair([], $dispatcher, \OC::$server->get(LoggerInterface::class));
$r = \OCP\Server::get(Repair::class);
foreach ($steps as $step) {
try {
$r->addStep($step);
Expand Down
25 changes: 9 additions & 16 deletions tests/lib/Migration/BackgroundRepairTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
use OC\BackgroundJob\JobList;
use OC\Migration\BackgroundRepair;
use OC\NeedsUpdateException;
use OC\Repair;
use OC\Repair\Events\RepairStepEvent;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

Expand Down Expand Up @@ -57,20 +57,12 @@ public function run(IOutput $output) {
}

class BackgroundRepairTest extends TestCase {
/** @var JobList|MockObject */
private $jobList;

/** @var BackgroundRepair|MockObject */
private $job;

/** @var LoggerInterface|MockObject */
private $logger;

/** @var IEventDispatcher|MockObject $dispatcher */
private $dispatcher;

/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject $dispatcher */
private $time;
private JobList $jobList;
private BackgroundRepair $job;
private LoggerInterface $logger;
private IEventDispatcher $dispatcher;
private ITimeFactory $time;
private Repair $repair;

protected function setUp(): void {
parent::setUp();
Expand All @@ -85,8 +77,9 @@ protected function setUp(): void {
$this->time = $this->createMock(ITimeFactory::class);
$this->time->method('getTime')
->willReturn(999999);
$this->repair = new Repair($this->dispatcher, $this->logger);
$this->job = $this->getMockBuilder(BackgroundRepair::class)
->setConstructorArgs([$this->dispatcher, $this->time, $this->logger, $this->jobList])
->setConstructorArgs([$this->repair, $this->time, $this->logger, $this->jobList])
->setMethods(['loadApp'])
->getMock();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/RepairTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class RepairTest extends TestCase {
protected function setUp(): void {
parent::setUp();
$dispatcher = \OC::$server->get(IEventDispatcher::class);
$this->repair = new \OC\Repair([], $dispatcher, $this->createMock(LoggerInterface::class));
$this->repair = new Repair($dispatcher, $this->createMock(LoggerInterface::class));

$dispatcher->addListener(RepairWarningEvent::class, function (RepairWarningEvent $event) {
$this->outputArray[] = 'warning: ' . $event->getMessage();
Expand Down
Loading