Skip to content

Commit 16b518c

Browse files
committed
Advanced CLI commands: user:config:get
gh-153 in progress Signed-off-by: Nicholas K. Dionysopoulos <[email protected]>
1 parent 1a7620a commit 16b518c

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

.idea/runConfigurations/CLI_user_config_get.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CliCommand/UserConfigGet.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* @package panopticon
4+
* @copyright Copyright (c)2023-2024 Nicholas K. Dionysopoulos / Akeeba Ltd
5+
* @license https://www.gnu.org/licenses/agpl-3.0.txt GNU Affero General Public License, version 3 or later
6+
*/
7+
8+
namespace Akeeba\Panopticon\CliCommand;
9+
10+
defined('AKEEBA') || die;
11+
12+
use Akeeba\Panopticon\CliCommand\Attribute\ConfigAssertion;
13+
use Akeeba\Panopticon\CliCommand\Trait\PrintFormattedArrayTrait;
14+
use Akeeba\Panopticon\Factory;
15+
use Akeeba\Panopticon\Model\Users;
16+
use Awf\Registry\Registry;
17+
use Symfony\Component\Console\Attribute\AsCommand;
18+
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Input\InputOption;
22+
use Symfony\Component\Console\Output\OutputInterface;
23+
24+
#[AsCommand(
25+
name: 'user:config:get',
26+
description: 'Get the value of a user configuration variable',
27+
hidden: false,
28+
)]
29+
#[ConfigAssertion(true)]
30+
class UserConfigGet extends AbstractCommand
31+
{
32+
use PrintFormattedArrayTrait;
33+
34+
protected function execute(InputInterface $input, OutputInterface $output): int
35+
{
36+
$container = Factory::getContainer();
37+
/** @var Users $model */
38+
$model = $container->mvcFactory->makeTempModel('Users');
39+
$id = intval($input->getArgument('id'));
40+
41+
try
42+
{
43+
$model->findOrFail($id);
44+
}
45+
catch (\Exception $e)
46+
{
47+
$this->ioStyle->error(
48+
[
49+
sprintf('Could not find user %d', $id),
50+
$e->getMessage(),
51+
]
52+
);
53+
54+
return Command::FAILURE;
55+
}
56+
57+
$config = new Registry($model->parameters);
58+
59+
// Output the information in the requested format
60+
$value = $config->get($input->getArgument('key'));
61+
62+
if (is_object($value))
63+
{
64+
$value = $this->flatten((new Registry($value))->toArray());
65+
}
66+
67+
$this->printFormattedScalar(
68+
$value,
69+
$input->getOption('format') ?: 'table'
70+
);
71+
72+
return Command::SUCCESS;
73+
}
74+
75+
protected function configure(): void
76+
{
77+
$this
78+
->addArgument('id', InputArgument::REQUIRED, 'The numeric user ID to list config values for')
79+
->addArgument('key', InputArgument::REQUIRED, 'The configuration key to get the value for')
80+
->addOption(
81+
'format', 'f', InputOption::VALUE_OPTIONAL, 'Output format (table, json, yaml, csv, count)', 'table'
82+
);
83+
}
84+
85+
private function flatten(array $array, $prefix = ''): array
86+
{
87+
$ret = [];
88+
89+
foreach ($array as $key => $value)
90+
{
91+
if (is_array($value))
92+
{
93+
foreach ($this->flatten($value, $prefix . $key . '.') as $k1 => $v1)
94+
{
95+
$ret[$k1] = $v1;
96+
}
97+
}
98+
else
99+
{
100+
$ret[$prefix . $key] = $value;
101+
}
102+
}
103+
104+
return $ret;
105+
}
106+
}

0 commit comments

Comments
 (0)