Compare commits

...

1 Commits

Author SHA1 Message Date
Christoph Wurst
c66df4e22f feat(occ): Emit events for command execution
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2025-04-01 14:02:25 +02:00
6 changed files with 166 additions and 1 deletions

View File

@@ -248,6 +248,8 @@ return array(
'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => $baseDir . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php',
'OCP\\Collaboration\\Resources\\ResourceException' => $baseDir . '/lib/public/Collaboration/Resources/ResourceException.php',
'OCP\\Color' => $baseDir . '/lib/public/Color.php',
'OCP\\Command\\Events\\BeforeCommandExecutedEvent' => $baseDir . '/lib/public/Command/Events/BeforeCommandExecutedEvent.php',
'OCP\\Command\\Events\\CommandExecutedEvent' => $baseDir . '/lib/public/Command/Events/CommandExecutedEvent.php',
'OCP\\Command\\IBus' => $baseDir . '/lib/public/Command/IBus.php',
'OCP\\Command\\ICommand' => $baseDir . '/lib/public/Command/ICommand.php',
'OCP\\Comments\\CommentsEntityEvent' => $baseDir . '/lib/public/Comments/CommentsEntityEvent.php',

View File

@@ -297,6 +297,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php',
'OCP\\Collaboration\\Resources\\ResourceException' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/ResourceException.php',
'OCP\\Color' => __DIR__ . '/../../..' . '/lib/public/Color.php',
'OCP\\Command\\Events\\BeforeCommandExecutedEvent' => __DIR__ . '/../../..' . '/lib/public/Command/Events/BeforeCommandExecutedEvent.php',
'OCP\\Command\\Events\\CommandExecutedEvent' => __DIR__ . '/../../..' . '/lib/public/Command/Events/CommandExecutedEvent.php',
'OCP\\Command\\IBus' => __DIR__ . '/../../..' . '/lib/public/Command/IBus.php',
'OCP\\Command\\ICommand' => __DIR__ . '/../../..' . '/lib/public/Command/ICommand.php',
'OCP\\Comments\\CommentsEntityEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/CommentsEntityEvent.php',

View File

@@ -8,11 +8,15 @@
namespace OC\Console;
use ArgumentCountError;
use OC\EventDispatcher\EventDispatcher;
use OC\MemoryInfo;
use OC\NeedsUpdateException;
use OC\SystemConfig;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\Command\Events\BeforeCommandExecutedEvent;
use OCP\Command\Events\CommandExecutedEvent;
use OCP\Command\Events\CommandFailedEvent;
use OCP\Console\ConsoleEvent;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
@@ -23,25 +27,71 @@ use OCP\ServerVersion;
use Psr\Container\ContainerExceptionInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Application {
private IEventDispatcher $dispatcher;
private SymfonyApplication $application;
public function __construct(
ServerVersion $serverVersion,
private IConfig $config,
private IEventDispatcher $dispatcher,
EventDispatcher $dispatcher,
private IRequest $request,
private LoggerInterface $logger,
private MemoryInfo $memoryInfo,
private IAppManager $appManager,
private Defaults $defaults,
) {
$this->dispatcher = $dispatcher;
$this->application = new SymfonyApplication($defaults->getName(), $serverVersion->getVersionString());
$symfonyDispatcher = $dispatcher->getSymfonyDispatcher();
$symfonyDispatcher->addListener(\Symfony\Component\Console\ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) use ($dispatcher) {
$command = $event->getCommand();
$name = $command?->getName();
if ($name === null) {
// Ignore unnamed events
return;
}
$dispatcher->dispatchTyped(
new BeforeCommandExecutedEvent(
$name,
)
);
});
$symfonyDispatcher->addListener(\Symfony\Component\Console\ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) use ($dispatcher) {
$command = $event->getCommand();
$name = $command?->getName();
if ($name === null) {
// Ignore unnamed events
return;
}
$dispatcher->dispatchTyped(
new CommandExecutedEvent(
$name,
)
);
});
$symfonyDispatcher->addListener(\Symfony\Component\Console\ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) use ($dispatcher) {
$command = $event->getCommand();
$name = $command?->getName();
if ($name === null) {
// Ignore unnamed events
return;
}
$dispatcher->dispatchTyped(
new CommandFailedEvent(
$name,
)
);
});
$this->application->setDispatcher($symfonyDispatcher);
}
/**

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCP\Command\Events;
use OCP\EventDispatcher\Event;
/**
* Dispatched before an occ command is executed
*
* @since 32.0.0
*/
class BeforeCommandExecutedEvent extends Event {
/**
* @since 32.0.0
* @internal instances are created by Nextcloud server
*/
public function __construct(
private string $command,
) {
parent::__construct();
}
/**
* @since 32.0.0
*/
public function getCommand(): string {
return $this->command;
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCP\Command\Events;
use OCP\EventDispatcher\Event;
/**
* Dispatched after an occ command is executed
*
* @since 32.0.0
*/
class CommandExecutedEvent extends Event {
/**
* @since 32.0.0
* @internal instances are created by Nextcloud server
*/
public function __construct(
private string $command,
) {
parent::__construct();
}
/**
* @since 32.0.0
*/
public function getCommand(): string {
return $this->command;
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCP\Command\Events;
use OCP\EventDispatcher\Event;
/**
* Dispatched when an occ command failed
*
* @since 32.0.0
*/
class CommandFailedEvent extends Event {
/**
* @since 32.0.0
* @internal instances are created by Nextcloud server
*/
public function __construct(
private string $command,
) {
parent::__construct();
}
/**
* @since 32.0.0
*/
public function getCommand(): string {
return $this->command;
}
}