Skip to content

Commit

Permalink
feat(event): Introduce a new ConsoleEventV2 modern event to replace d…
Browse files Browse the repository at this point in the history
…epreciated ConsoleEvent event

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Mar 13, 2023
1 parent ce4d24f commit 0272a3f
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 37 deletions.
5 changes: 4 additions & 1 deletion console.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
require_once __DIR__ . '/lib/versioncheck.php';

use OC\Console\Application;
use OCP\Server;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;

Expand Down Expand Up @@ -92,9 +93,11 @@ function exceptionHandler($exception) {
$application = new Application(
\OC::$server->getConfig(),
\OC::$server->getEventDispatcher(),
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),
Server::get(\OCP\Defaults::class)
);
$application->loadCommands(new ArgvInput(), new ConsoleOutput());
$application->run();
Expand Down
80 changes: 44 additions & 36 deletions lib/private/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,48 @@
*/
namespace OC\Console;

use ArgumentCountError;
use Exception;
use OC\MemoryInfo;
use OC\NeedsUpdateException;
use OC\SystemConfig;
use OC_App;
use OCP\AppFramework\QueryException;
use OCP\App\IAppManager;
use OCP\Console\ConsoleEvent;
use OCP\Console\ConsoleEventV2;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IRequest;
use OCP\Server;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class Application {
/** @var IConfig */
private $config;
private IConfig $config;
private SymfonyApplication $application;
/** @var EventDispatcherInterface */
private $dispatcher;
/** @var IRequest */
private $request;
/** @var LoggerInterface */
private $logger;
/** @var MemoryInfo */
private $memoryInfo;
private EventDispatcherInterface $legacyDispatcher;
private IEventDispatcher $dispatcher;
private IRequest $request;
private LoggerInterface $logger;
private MemoryInfo $memoryInfo;

public function __construct(IConfig $config,
EventDispatcherInterface $dispatcher,
EventDispatcherInterface $legacyDispatcher,
IEventDispatcher $dispatcher,
IRequest $request,
LoggerInterface $logger,
MemoryInfo $memoryInfo) {
$defaults = \OC::$server->getThemingDefaults();
MemoryInfo $memoryInfo, Defaults $defaults) {
$this->config = $config;
$this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString());
$this->legacyDispatcher = $legacyDispatcher;
$this->dispatcher = $dispatcher;
$this->request = $request;
$this->logger = $logger;
Expand All @@ -76,12 +81,14 @@ public function __construct(IConfig $config,
/**
* @param InputInterface $input
* @param ConsoleOutputInterface $output
* @throws \Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function loadCommands(
InputInterface $input,
ConsoleOutputInterface $output
) {
): void {
// $application is required to be defined in the register_command scripts
$application = $this->application;
$inputDefinition = $application->getDefinition();
Expand Down Expand Up @@ -119,7 +126,7 @@ public function loadCommands(
$this->writeMaintenanceModeInfo($input, $output);
} else {
OC_App::loadApps();
$appManager = \OCP\Server::get(IAppManager::class);
$appManager = Server::get(IAppManager::class);
foreach ($appManager->getInstalledApps() as $app) {
$appPath = \OC_App::getAppPath($app);
if ($appPath === false) {
Expand All @@ -136,7 +143,7 @@ public function loadCommands(
if (file_exists($file)) {
try {
require $file;
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->error($e->getMessage(), [
'exception' => $e,
]);
Expand All @@ -148,7 +155,7 @@ public function loadCommands(
$errorOutput = $output->getErrorOutput();
$errorOutput->writeln("Nextcloud is not installed - only a limited number of commands are available");
}
} catch (NeedsUpdateException $e) {
} catch (NeedsUpdateException) {
if ($input->getArgument('command') !== '_completion') {
$errorOutput = $output->getErrorOutput();
$errorOutput->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available");
Expand All @@ -157,14 +164,14 @@ public function loadCommands(
}

if ($input->getFirstArgument() !== 'check') {
$errors = \OC_Util::checkServer(\OC::$server->getSystemConfig());
$errors = \OC_Util::checkServer(Server::get(SystemConfig::class));
if (!empty($errors)) {
foreach ($errors as $error) {
$output->writeln((string)$error['error']);
$output->writeln((string)$error['hint']);
$output->writeln('');
}
throw new \Exception("Environment not properly prepared.");
throw new Exception("Environment not properly prepared.");
}
}
}
Expand All @@ -180,7 +187,7 @@ public function loadCommands(
*/
private function writeMaintenanceModeInfo(
InputInterface $input, ConsoleOutputInterface $output
) {
): void {
if ($input->getArgument('command') !== '_completion'
&& $input->getArgument('command') !== 'maintenance:mode'
&& $input->getArgument('command') !== 'status') {
Expand All @@ -197,37 +204,38 @@ private function writeMaintenanceModeInfo(
*
* @param bool $boolean Whether to automatically exit after a command execution or not
*/
public function setAutoExit($boolean) {
public function setAutoExit(bool $boolean): void {
$this->application->setAutoExit($boolean);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws \Exception
* @throws Exception
*/
public function run(InputInterface $input = null, OutputInterface $output = null) {
$this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent(
public function run(InputInterface $input = null, OutputInterface $output = null): int {
$this->legacyDispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent(
ConsoleEvent::EVENT_RUN,
$this->request->server['argv']
));
$this->dispatcher->dispatchTyped(new ConsoleEventV2($this->request->server['argv']));
return $this->application->run($input, $output);
}

private function loadCommandsFromInfoXml($commands) {
/**
* @throws Exception
*/
private function loadCommandsFromInfoXml($commands): void {
foreach ($commands as $command) {
try {
$c = \OC::$server->query($command);
} catch (QueryException $e) {
$c = Server::get($command);
} catch (ContainerExceptionInterface $e) {
if (class_exists($command)) {
try {
$c = new $command();
} catch (\ArgumentCountError $e2) {
throw new \Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e);
} catch (ArgumentCountError) {
throw new Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e);
}
} else {
throw new \Exception("Console command '$command' is unknown and could not be loaded");
throw new Exception("Console command '$command' is unknown and could not be loaded");
}
}

Expand Down
55 changes: 55 additions & 0 deletions lib/public/Console/ConsoleEventV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCP\Console;

use OCP\EventDispatcher\Event;

/**
* Class ConsoleEvent
*
* @since 27.0.0
*/
class ConsoleEventV2 extends Event {
/** @var string[] */
protected array $arguments;

/**
* DispatcherEvent constructor.
*
* @param string[] $arguments
* @since 27.0.0
*/
public function __construct(array $arguments) {
$this->arguments = $arguments;
parent::__construct();
}

/**
* @return string[]
* @since 27.0.0
*/
public function getArguments(): array {
return $this->arguments;
}
}

0 comments on commit 0272a3f

Please sign in to comment.