Skip to content

Commit 55a5fd8

Browse files
authored
Merge pull request #708 from nextcloud/fix/noid/oc-core-command
fix: migrate commands to OC\Core\Command\Base class
2 parents a72b4fb + 47524c3 commit 55a5fd8

8 files changed

Lines changed: 57 additions & 10 deletions

File tree

REUSE.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ precedence = "aggregate"
1717
SPDX-FileCopyrightText = "2023 Nextcloud GmbH and Nextcloud contributors"
1818
SPDX-License-Identifier = "AGPL-3.0-or-later"
1919

20+
[[annotations]]
21+
path = ["tests/stubs/oc_core_command_base.php"]
22+
precedence = "aggregate"
23+
SPDX-FileCopyrightText = "2026 Nextcloud GmbH and Nextcloud contributors"
24+
SPDX-License-Identifier = "AGPL-3.0-or-later"
25+
2026
[[annotations]]
2127
path = ["img/app.svg", "img/app-dark.svg"]
2228
precedence = "aggregate"

lib/Command/Log.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
namespace OCA\NotifyPush\Command;
1010

11+
use OC\Core\Command\Base;
1112
use OCA\NotifyPush\Queue\IQueue;
12-
use Symfony\Component\Console\Command\Command;
1313
use Symfony\Component\Console\Input\InputArgument;
1414
use Symfony\Component\Console\Input\InputInterface;
1515
use Symfony\Component\Console\Input\InputOption;
1616
use Symfony\Component\Console\Output\OutputInterface;
1717

18-
class Log extends Command {
18+
class Log extends Base {
1919
private $queue;
2020

2121
public function __construct(

lib/Command/Metrics.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
namespace OCA\NotifyPush\Command;
1010

11+
use OC\Core\Command\Base;
1112
use OCA\NotifyPush\Queue\IQueue;
1213
use OCA\NotifyPush\Queue\RedisQueue;
13-
use Symfony\Component\Console\Command\Command;
1414
use Symfony\Component\Console\Input\InputInterface;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616

17-
class Metrics extends Command {
17+
class Metrics extends Base {
1818
private $queue;
1919

2020
public function __construct(
@@ -51,6 +51,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5151
$output->writeln('<error>Invalid metrics received from push server</error>');
5252
return 1;
5353
}
54+
55+
// Output in the requested format if different from plain
56+
if ($input->getOption('output') !== self::OUTPUT_FORMAT_PLAIN) {
57+
$this->writeArrayInOutputFormat($input, $output, $metrics);
58+
return 0;
59+
}
60+
5461
$output->writeln('Active connection count: ' . $metrics['active_connection_count']);
5562
$output->writeln('Active user count: ' . $metrics['active_user_count']);
5663
$output->writeln('Total connection count: ' . $metrics['total_connection_count']);

lib/Command/Reset.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
namespace OCA\NotifyPush\Command;
1010

11+
use OC\Core\Command\Base;
1112
use OCA\NotifyPush\Queue\IQueue;
12-
use Symfony\Component\Console\Command\Command;
1313
use Symfony\Component\Console\Input\InputInterface;
1414
use Symfony\Component\Console\Output\OutputInterface;
1515

16-
class Reset extends Command {
16+
class Reset extends Base {
1717
private $queue;
1818

1919
public function __construct(

lib/Command/SelfTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
namespace OCA\NotifyPush\Command;
1010

11+
use OC\Core\Command\Base;
1112
use OCP\IConfig;
12-
use Symfony\Component\Console\Command\Command;
1313
use Symfony\Component\Console\Input\InputInterface;
1414
use Symfony\Component\Console\Output\OutputInterface;
1515

16-
class SelfTest extends Command {
16+
class SelfTest extends Base {
1717
private $test;
1818
private $config;
1919

lib/Command/Setup.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
namespace OCA\NotifyPush\Command;
1010

11+
use OC\Core\Command\Base;
1112
use OCA\NotifyPush\SetupWizard;
1213
use OCP\IConfig;
13-
use Symfony\Component\Console\Command\Command;
1414
use Symfony\Component\Console\Input\InputArgument;
1515
use Symfony\Component\Console\Input\InputInterface;
1616
use Symfony\Component\Console\Output\OutputInterface;
1717

18-
class Setup extends Command {
18+
class Setup extends Base {
1919
private $test;
2020
private $config;
2121
private $setupWizard;

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</extraFiles>
2222
<stubs>
2323
<file name="tests/stub.phpstub" preloadClasses="true"/>
24+
<file name="tests/stubs/oc_core_command_base.php" />
2425
<file name="tests/stubs/symfony_component_console_command_command.php" />
2526
<file name="tests/stubs/symfony_component_console_helper_table.php" />
2627
<file name="tests/stubs/symfony_component_console_input_inputargument.php" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace OC\Core\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
/**
10+
* Base class for Nextcloud commands with output formatting support
11+
*/
12+
class Base extends Command {
13+
public const OUTPUT_FORMAT_PLAIN = 'plain';
14+
public const OUTPUT_FORMAT_JSON = 'json';
15+
public const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty';
16+
17+
protected string $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN;
18+
19+
protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, iterable $items, string $prefix = ' - '): void {
20+
}
21+
22+
protected function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void {
23+
}
24+
25+
protected function writeStreamingTableInOutputFormat(InputInterface $input, OutputInterface $output, \Iterator $items, int $tableGroupSize): void {
26+
}
27+
28+
protected function writeStreamingJsonArray(InputInterface $input, OutputInterface $output, \Iterator $items): void {
29+
}
30+
31+
public function chunkIterator(\Iterator $iterator, int $count): \Iterator {
32+
}
33+
}

0 commit comments

Comments
 (0)