Skip to content

Commit 3be40bf

Browse files
committed
Advanced CLI commands: user:config:list
gh-153 in progress Signed-off-by: Nicholas K. Dionysopoulos <[email protected]>
1 parent 34ba122 commit 3be40bf

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

.idea/runConfigurations/CLI_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.

.idea/runConfigurations/CLI_user_list.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/UserConfigList.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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:list',
26+
description: 'List user configuration variables',
27+
hidden: false,
28+
)]
29+
#[ConfigAssertion(true)]
30+
class UserConfigList extends AbstractCommand
31+
{
32+
use PrintFormattedArrayTrait;
33+
34+
protected function execute(InputInterface $input, OutputInterface $output): int
35+
{
36+
$container = Factory::getContainer();
37+
$ret = [];
38+
39+
/** @var Users $model */
40+
$model = $container->mvcFactory->makeTempModel('Users');
41+
$id = intval($input->getArgument('id'));
42+
43+
try
44+
{
45+
$model->findOrFail($id);
46+
}
47+
catch (\Exception $e)
48+
{
49+
$this->ioStyle->error(
50+
[
51+
sprintf('Could not find user %d', $id),
52+
$e->getMessage(),
53+
]
54+
);
55+
}
56+
57+
$config = $this->flatten((new Registry($model->parameters))->toArray());
58+
$ret = [];
59+
60+
foreach ($config as $k => $v)
61+
{
62+
$item['key'] = $k;
63+
$item['value'] = $v;
64+
65+
$ret[] = $item;
66+
}
67+
68+
// Output the information in the requested format
69+
$this->printFormattedArray(
70+
$ret,
71+
$input->getOption('format') ?: 'table'
72+
);
73+
74+
return Command::SUCCESS;
75+
}
76+
77+
protected function configure(): void
78+
{
79+
$this
80+
->addArgument('id', InputArgument::REQUIRED, 'The numeric user ID to list config values for')
81+
->addOption(
82+
'format', 'f', InputOption::VALUE_OPTIONAL, 'Output format (table, json, yaml, csv, count)', 'table'
83+
);
84+
}
85+
86+
private function flatten(array $array, $prefix = ''): array
87+
{
88+
$ret = [];
89+
90+
foreach ($array as $key => $value)
91+
{
92+
if (is_array($value))
93+
{
94+
foreach ($this->flatten($value, $prefix . $key . '.') as $k1 => $v1)
95+
{
96+
$ret[$k1] = $v1;
97+
}
98+
}
99+
else
100+
{
101+
$ret[$prefix . $key] = $value;
102+
}
103+
}
104+
105+
return $ret;
106+
}
107+
}

0 commit comments

Comments
 (0)