Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions core/Command/Config/System/CastHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Command\Config\System;

class CastHelper {
/**
* @return array{value: mixed, readable-value: string}
*/
public function castValue(?string $value, string $type): array {
switch ($type) {
case 'integer':
case 'int':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (int)$value,
'readable-value' => 'integer ' . (int)$value,
];

case 'double':
case 'float':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (float)$value,
'readable-value' => 'double ' . (float)$value,
];

case 'boolean':
case 'bool':
$value = strtolower($value);
return match ($value) {
'true' => [
'value' => true,
'readable-value' => 'boolean ' . $value,
],
'false' => [
'value' => false,
'readable-value' => 'boolean ' . $value,
],
default => throw new \InvalidArgumentException('Unable to parse value as boolean'),
};

case 'null':
return [
'value' => null,
'readable-value' => 'null',
];

case 'string':
$value = (string)$value;
return [
'value' => $value,
'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
];

case 'json':
$value = json_decode($value, true);
return [
'value' => $value,
'readable-value' => 'json ' . json_encode($value),
];

default:
throw new \InvalidArgumentException('Invalid type');
}
}
}
77 changes: 2 additions & 75 deletions core/Command/Config/System/SetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class SetConfig extends Base {
public function __construct(
SystemConfig $systemConfig,
private CastHelper $castHelper,
) {
parent::__construct($systemConfig);
}
Expand Down Expand Up @@ -57,7 +58,7 @@ protected function configure() {
protected function execute(InputInterface $input, OutputInterface $output): int {
$configNames = $input->getArgument('name');
$configName = $configNames[0];
$configValue = $this->castValue($input->getOption('value'), $input->getOption('type'));
$configValue = $this->castHelper->castValue($input->getOption('value'), $input->getOption('type'));
$updateOnly = $input->getOption('update-only');

if (count($configNames) > 1) {
Expand All @@ -80,80 +81,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

/**
* @param string $value
* @param string $type
* @return mixed
* @throws \InvalidArgumentException
*/
protected function castValue($value, $type) {
switch ($type) {
case 'integer':
case 'int':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (int)$value,
'readable-value' => 'integer ' . (int)$value,
];

case 'double':
case 'float':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (float)$value,
'readable-value' => 'double ' . (float)$value,
];

case 'boolean':
case 'bool':
$value = strtolower($value);
switch ($value) {
case 'true':
return [
'value' => true,
'readable-value' => 'boolean ' . $value,
];

case 'false':
return [
'value' => false,
'readable-value' => 'boolean ' . $value,
];

default:
throw new \InvalidArgumentException('Unable to parse value as boolean');
}

// no break
case 'null':
return [
'value' => null,
'readable-value' => 'null',
];

case 'string':
$value = (string)$value;
return [
'value' => $value,
'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
];

case 'json':
$value = json_decode($value, true);
return [
'value' => $value,
'readable-value' => 'json ' . json_encode($value),
];

default:
throw new \InvalidArgumentException('Invalid type');
}
}

/**
* @param array $configNames
* @param mixed $existingValues
Expand Down
47 changes: 47 additions & 0 deletions core/Command/Memcache/DistributedClear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Command\Memcache;

use OC\Core\Command\Base;
use OCP\ICacheFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class DistributedClear extends Base {
public function __construct(
protected ICacheFactory $cacheFactory,
) {
parent::__construct();
}

protected function configure(): void {
$this
->setName('memcache:distributed:clear')
->setDescription('Clear values from the distributed memcache')
->addOption('prefix', null, InputOption::VALUE_REQUIRED, 'Only remove keys matching the prefix');
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$cache = $this->cacheFactory->createDistributed();
$prefix = $input->getOption('prefix');
if ($cache->clear($prefix)) {
if ($prefix) {
$output->writeln('<info>Distributed cache matching prefix ' . $prefix . ' cleared</info>');
} else {
$output->writeln('<info>Distributed cache cleared</info>');
}
return 0;
} else {
$output->writeln('<error>Failed to clear cache</error>');
return 1;
}
}
}
43 changes: 43 additions & 0 deletions core/Command/Memcache/DistributedDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Command\Memcache;

use OC\Core\Command\Base;
use OCP\ICacheFactory;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DistributedDelete extends Base {
public function __construct(
protected ICacheFactory $cacheFactory,
) {
parent::__construct();
}

protected function configure(): void {
$this
->setName('memcache:distributed:delete')
->setDescription('Delete a value in the distributed memcache')
->addArgument('key', InputArgument::REQUIRED, 'The key to delete');
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$cache = $this->cacheFactory->createDistributed();
$key = $input->getArgument('key');
if ($cache->remove($key)) {
$output->writeln('<info>Distributed cache key <info>' . $key . '</info> deleted</info>');
return 0;
} else {
$output->writeln('<error>Failed to delete cache key ' . $key . '</error>');
return 1;
}
}
}
40 changes: 40 additions & 0 deletions core/Command/Memcache/DistributedGet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Command\Memcache;

use OC\Core\Command\Base;
use OCP\ICacheFactory;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DistributedGet extends Base {
public function __construct(
protected ICacheFactory $cacheFactory,
) {
parent::__construct();
}

protected function configure(): void {
$this
->setName('memcache:distributed:get')
->setDescription('Get a value from the distributed memcache')
->addArgument('key', InputArgument::REQUIRED, 'The key to retrieve');
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$cache = $this->cacheFactory->createDistributed();
$key = $input->getArgument('key');

$value = $cache->get($key);
$this->writeMixedInOutputFormat($input, $output, $value);
return 0;
}
}
57 changes: 57 additions & 0 deletions core/Command/Memcache/DistributedSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Command\Memcache;

use OC\Core\Command\Base;
use OC\Core\Command\Config\System\CastHelper;
use OCP\ICacheFactory;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class DistributedSet extends Base {
public function __construct(
protected ICacheFactory $cacheFactory,
private CastHelper $castHelper,
) {
parent::__construct();
}

protected function configure(): void {
$this
->setName('memcache:distributed:set')
->setDescription('Set a value in the distributed memcache')
->addArgument('key', InputArgument::REQUIRED, 'The key to set')
->addArgument('value', InputArgument::REQUIRED, 'The value to set')
->addOption(
'type',
null,
InputOption::VALUE_REQUIRED,
'Value type [string, integer, float, boolean, json, null]',
'string'
);
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$cache = $this->cacheFactory->createDistributed();
$key = $input->getArgument('key');
$value = $input->getArgument('value');
$type = $input->getOption('type');
['value' => $value, 'readable-value' => $readable] = $this->castHelper->castValue($value, $type);
if ($cache->set($key, $value)) {
$output->writeln('Distributed cache key <info>' . $key . '</info> set to <info>' . $readable . '</info>');
return 0;
} else {
$output->writeln('<error>Failed to set cache key ' . $key . '</error>');
return 1;
}
}
}
8 changes: 8 additions & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
use OC\Core\Command\Maintenance\RepairShareOwnership;
use OC\Core\Command\Maintenance\UpdateHtaccess;
use OC\Core\Command\Maintenance\UpdateTheme;
use OC\Core\Command\Memcache\DistributedClear;
use OC\Core\Command\Memcache\DistributedDelete;
use OC\Core\Command\Memcache\DistributedGet;
use OC\Core\Command\Memcache\DistributedSet;
use OC\Core\Command\Memcache\RedisCommand;
use OC\Core\Command\Preview\Generate;
use OC\Core\Command\Preview\ResetRenderedTexts;
Expand Down Expand Up @@ -245,6 +249,10 @@
$application->add(Server::get(Statistics::class));

$application->add(Server::get(RedisCommand::class));
$application->add(Server::get(DistributedClear::class));
$application->add(Server::get(DistributedDelete::class));
$application->add(Server::get(DistributedGet::class));
$application->add(Server::get(DistributedSet::class));
} else {
$application->add(Server::get(Command\Maintenance\Install::class));
}
Loading
Loading