Skip to content

Commit f61d705

Browse files
committed
Getter and setter commands
1 parent ef672c4 commit f61d705

17 files changed

+733
-1
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Commands\Config;
4+
5+
use App\Concerns\DiscoversConfigurationTrait;
6+
use App\Concerns\ManagesConfigFileTrait;
7+
use LaravelZero\Framework\Commands\Command;
8+
9+
use function Laravel\Prompts\error;
10+
11+
class GetCommand extends Command
12+
{
13+
use DiscoversConfigurationTrait;
14+
use ManagesConfigFileTrait;
15+
16+
protected $signature = 'config:get {key} {--compact : Output JSON without pretty printing}';
17+
18+
protected $description = 'Get a configuration value by dot-path';
19+
20+
public function __construct()
21+
{
22+
parent::__construct();
23+
$this->bootDiscoversConfigurationTrait();
24+
}
25+
26+
public function handle(): int
27+
{
28+
try {
29+
$data = $this->readConfigData();
30+
} catch (\Throwable $e) {
31+
error($e->getMessage());
32+
33+
return self::FAILURE;
34+
}
35+
36+
$key = $this->argument('key');
37+
38+
if (! $this->hasByDotPath($data, $key)) {
39+
error("Key \"{$key}\" not found.");
40+
41+
return self::FAILURE;
42+
}
43+
44+
$value = $this->getByDotPath($data, $key);
45+
46+
if (is_array($value)) {
47+
$flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
48+
if (! $this->option('compact')) {
49+
$flags |= JSON_PRETTY_PRINT;
50+
}
51+
$this->output->writeln(json_encode($value, $flags));
52+
} else {
53+
$this->output->writeln((string) $value);
54+
}
55+
56+
return self::SUCCESS;
57+
}
58+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Commands\Config;
4+
5+
use App\Concerns\DiscoversConfigurationTrait;
6+
use App\Concerns\ManagesConfigFileTrait;
7+
use LaravelZero\Framework\Commands\Command;
8+
9+
use function Laravel\Prompts\error;
10+
11+
class HasCommand extends Command
12+
{
13+
use DiscoversConfigurationTrait;
14+
use ManagesConfigFileTrait;
15+
16+
protected $signature = 'config:has {key}';
17+
18+
protected $description = 'Check if a configuration key exists';
19+
20+
public function __construct()
21+
{
22+
parent::__construct();
23+
$this->bootDiscoversConfigurationTrait();
24+
}
25+
26+
public function handle(): int
27+
{
28+
try {
29+
$data = $this->readConfigData();
30+
} catch (\Throwable $e) {
31+
error($e->getMessage());
32+
33+
return self::FAILURE;
34+
}
35+
36+
$key = $this->argument('key');
37+
$exists = $this->hasByDotPath($data, $key);
38+
39+
$this->output->writeln($exists ? 'true' : 'false');
40+
41+
return $exists ? self::SUCCESS : self::FAILURE;
42+
}
43+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace App\Commands\Config;
4+
5+
use App\Concerns\DiscoversConfigurationTrait;
6+
use App\Concerns\ManagesConfigFileTrait;
7+
use KDuma\PhpCA\ConfigManager\Adapter\AdapterConfigurationFactory;
8+
use KDuma\PhpCA\ConfigManager\CaConfigurationLoader;
9+
use LaravelZero\Framework\Commands\Command;
10+
11+
use function Laravel\Prompts\error;
12+
use function Laravel\Prompts\info;
13+
use function Laravel\Prompts\select;
14+
use function Laravel\Prompts\text;
15+
use function Laravel\Prompts\warning;
16+
17+
class InitCommand extends Command
18+
{
19+
use ManagesConfigFileTrait {
20+
ManagesConfigFileTrait::validateConfig as traitValidateConfig;
21+
}
22+
use DiscoversConfigurationTrait;
23+
24+
protected $signature = 'config:init {--force : Overwrite existing config file}';
25+
26+
protected $description = 'Create a new configuration file';
27+
28+
public function __construct()
29+
{
30+
parent::__construct();
31+
$this->bootDiscoversConfigurationTrait();
32+
}
33+
34+
public function handle(): int
35+
{
36+
$path = getcwd() . '/' . self::CONFIG_FILE_NAME;
37+
38+
if (file_exists($path) && ! $this->option('force')) {
39+
error("Configuration file already exists at {$path}. Use --force to overwrite.");
40+
41+
return self::FAILURE;
42+
}
43+
44+
$adapterFactory = new AdapterConfigurationFactory();
45+
$adapterTypes = array_keys($adapterFactory->getAdapterTypes());
46+
47+
$type = select(
48+
label: 'Select adapter type',
49+
options: $adapterTypes,
50+
default: 'directory',
51+
);
52+
53+
$defaultPath = match ($type) {
54+
'directory' => './data',
55+
'sqlite' => './data.sqlite',
56+
'zip' => './data.zip',
57+
default => './data',
58+
};
59+
60+
$adapterPath = text(
61+
label: 'Adapter path',
62+
default: $defaultPath,
63+
required: true,
64+
);
65+
66+
$data = [
67+
'adapter' => [
68+
'type' => $type,
69+
'path' => $adapterPath,
70+
],
71+
];
72+
73+
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
74+
file_put_contents($path, $json);
75+
76+
info("Configuration file created at {$path}");
77+
78+
try {
79+
$loader = new CaConfigurationLoader();
80+
$loader->load($data, dirname($path));
81+
} catch (\Throwable $e) {
82+
warning("Warning: {$e->getMessage()}");
83+
}
84+
85+
return self::SUCCESS;
86+
}
87+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Commands\Config;
4+
5+
use App\Concerns\DiscoversConfigurationTrait;
6+
use App\Concerns\ManagesConfigFileTrait;
7+
use LaravelZero\Framework\Commands\Command;
8+
9+
use function Laravel\Prompts\error;
10+
11+
class PrintCommand extends Command
12+
{
13+
use DiscoversConfigurationTrait;
14+
use ManagesConfigFileTrait;
15+
16+
protected $signature = 'config:print';
17+
18+
protected $description = 'Print the full configuration file';
19+
20+
public function __construct()
21+
{
22+
parent::__construct();
23+
$this->bootDiscoversConfigurationTrait();
24+
}
25+
26+
public function handle(): int
27+
{
28+
try {
29+
$data = $this->readConfigData();
30+
} catch (\Throwable $e) {
31+
error($e->getMessage());
32+
33+
return self::FAILURE;
34+
}
35+
36+
$this->output->writeln(
37+
json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
38+
);
39+
40+
$this->validateConfig($data);
41+
42+
return self::SUCCESS;
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Commands\Config;
4+
5+
use App\Concerns\DiscoversConfigurationTrait;
6+
use App\Concerns\ManagesConfigFileTrait;
7+
use LaravelZero\Framework\Commands\Command;
8+
9+
use function Laravel\Prompts\error;
10+
11+
class SetCommand extends Command
12+
{
13+
use DiscoversConfigurationTrait;
14+
use ManagesConfigFileTrait;
15+
16+
protected $signature = 'config:set {key} {value}';
17+
18+
protected $description = 'Set a configuration value by dot-path';
19+
20+
public function __construct()
21+
{
22+
parent::__construct();
23+
$this->bootDiscoversConfigurationTrait();
24+
}
25+
26+
public function handle(): int
27+
{
28+
try {
29+
$data = $this->readConfigData();
30+
} catch (\Throwable $e) {
31+
error($e->getMessage());
32+
33+
return self::FAILURE;
34+
}
35+
36+
$key = $this->argument('key');
37+
$value = $this->parseValue($this->argument('value'));
38+
39+
$this->setByDotPath($data, $key, $value);
40+
$this->writeConfigData($data);
41+
$this->validateConfig($data);
42+
43+
return self::SUCCESS;
44+
}
45+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Commands\Config;
4+
5+
use App\Concerns\DiscoversConfigurationTrait;
6+
use App\Concerns\ManagesConfigFileTrait;
7+
use LaravelZero\Framework\Commands\Command;
8+
9+
use function Laravel\Prompts\error;
10+
11+
class UnsetCommand extends Command
12+
{
13+
use DiscoversConfigurationTrait;
14+
use ManagesConfigFileTrait;
15+
16+
protected $signature = 'config:unset {key}';
17+
18+
protected $description = 'Remove a configuration value by dot-path';
19+
20+
public function __construct()
21+
{
22+
parent::__construct();
23+
$this->bootDiscoversConfigurationTrait();
24+
}
25+
26+
public function handle(): int
27+
{
28+
try {
29+
$data = $this->readConfigData();
30+
} catch (\Throwable $e) {
31+
error($e->getMessage());
32+
33+
return self::FAILURE;
34+
}
35+
36+
$key = $this->argument('key');
37+
38+
if (! $this->unsetByDotPath($data, $key)) {
39+
error("Key \"{$key}\" not found.");
40+
41+
return self::FAILURE;
42+
}
43+
44+
$this->writeConfigData($data);
45+
$this->validateConfig($data);
46+
47+
return self::SUCCESS;
48+
}
49+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\Commands\Config;
4+
5+
use App\Concerns\DiscoversConfigurationTrait;
6+
use App\Concerns\ManagesConfigFileTrait;
7+
use KDuma\PhpCA\ConfigManager\CaConfigurationLoader;
8+
use LaravelZero\Framework\Commands\Command;
9+
10+
use function Laravel\Prompts\error;
11+
use function Laravel\Prompts\info;
12+
13+
class ValidateCommand extends Command
14+
{
15+
use DiscoversConfigurationTrait;
16+
use ManagesConfigFileTrait;
17+
18+
protected $signature = 'config:validate';
19+
20+
protected $description = 'Validate the configuration file';
21+
22+
public function __construct()
23+
{
24+
parent::__construct();
25+
$this->bootDiscoversConfigurationTrait();
26+
}
27+
28+
public function handle(): int
29+
{
30+
try {
31+
$data = $this->readConfigData();
32+
} catch (\Throwable $e) {
33+
error($e->getMessage());
34+
35+
return self::FAILURE;
36+
}
37+
38+
try {
39+
$loader = new CaConfigurationLoader();
40+
$loader->load($data, dirname($this->getCaConfigPath()));
41+
} catch (\Throwable $e) {
42+
error("Validation failed: {$e->getMessage()}");
43+
44+
return self::FAILURE;
45+
}
46+
47+
info('Configuration is valid.');
48+
49+
return self::SUCCESS;
50+
}
51+
}

0 commit comments

Comments
 (0)