Skip to content

Commit 23e9dba

Browse files
committed
feat(console): add about command to give environment informations
1 parent de00388 commit 23e9dba

File tree

3 files changed

+759
-0
lines changed

3 files changed

+759
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Console\Commands;
6+
7+
use Tempest\Console\ConsoleArgument;
8+
use Tempest\Console\ConsoleCommand;
9+
use Tempest\Console\ExitCode;
10+
use Tempest\Console\HasConsole;
11+
use Tempest\Container\Container;
12+
13+
final readonly class AboutCommand
14+
{
15+
use HasConsole;
16+
17+
private Container $container;
18+
private SystemInfoProvider $infoProvider;
19+
20+
public function __construct(Container $container)
21+
{
22+
$this->container = $container;
23+
$this->infoProvider = $container->get(SystemInfoProvider::class);
24+
}
25+
26+
#[ConsoleCommand(
27+
name: 'about',
28+
description: 'View a summary of information about your Tempest project',
29+
aliases: ['a'],
30+
)]
31+
public function __invoke(
32+
#[ConsoleArgument(description: 'Format to json', aliases: ['-j', '--json'])]
33+
?bool $json = null,
34+
): ExitCode {
35+
// Collect all information into a structured array
36+
$data = $this->infoProvider->gatherInformation();
37+
38+
// Check if user chose JSON option
39+
if ($json) {
40+
// Convert array keys to snake_case
41+
$snakeCaseData = $this->arrayKeysToSnakeCase($data);
42+
// Encode the data as JSON
43+
$jsonOutput = json_encode($snakeCaseData, JSON_PRETTY_PRINT);
44+
// Check if json_encode failed
45+
if ($jsonOutput === false) {
46+
$this->console->error('Failed to encode JSON: ' . json_last_error_msg());
47+
return ExitCode::ERROR;
48+
}
49+
$this->console->writeln($jsonOutput);
50+
} else {
51+
// Otherwise, display the data in the standard formatted way
52+
$this->displayNormalOutput($data);
53+
}
54+
55+
return ExitCode::SUCCESS;
56+
}
57+
58+
/**
59+
* Displays the gathered information in the standard console format.
60+
*
61+
* @param array $data The structured information to display
62+
*/
63+
private function displayNormalOutput(array $data): void
64+
{
65+
// Display Environment section
66+
$this->displaySection('Environment', $data['environment']);
67+
68+
// Display Database section
69+
$this->displaySection('Database', $data['database']);
70+
71+
// Display Cache section with styled statuses
72+
$this->console->header('Cache');
73+
foreach ($data['cache'] as $key => $value) {
74+
$this->displayCacheStatus($key, $value);
75+
}
76+
77+
// Display TailwindCSS section
78+
$this->displaySection('TailwindCSS', $data['tailwindcss']);
79+
}
80+
81+
/**
82+
* Displays a standard section with a header and key-value pairs.
83+
*
84+
* @param string $sectionName The name of the section to display
85+
* @param array $sectionData The data for the section
86+
*/
87+
private function displaySection(string $sectionName, array $sectionData): void
88+
{
89+
$this->console->header($sectionName);
90+
foreach ($sectionData as $key => $value) {
91+
$this->console->keyValue($key, $value);
92+
}
93+
}
94+
95+
/**
96+
* Displays the status of a cache in an appropriate style.
97+
*
98+
* @param string $label The name of the cache to be displayed.
99+
* @param string $envVar The corresponding environment variable.
100+
*/
101+
private function displayCacheStatus(string $label, bool|string|int $value): void
102+
{
103+
$isEnabled = $this->infoProvider->isCacheEnabled($value);
104+
$status = $isEnabled ? "<style='bold fg-green'>ENABLED</style>" : "<style='bold fg-red'>DISABLED</style>";
105+
$this->console->keyValue($label, $status);
106+
}
107+
108+
/**
109+
* Transform a string in snake case format
110+
*
111+
* @param string $string
112+
* @return string
113+
*/
114+
private function toSnakeCase(string $string): string
115+
{
116+
// Replace spaces by underscores
117+
$string = str_replace(' ', '_', $string);
118+
// Convert all characters in lowercase
119+
$string = strtolower($string);
120+
return $string;
121+
}
122+
123+
/**
124+
* Convert keys of an array in snake case
125+
*
126+
* @param array $array
127+
* @return array
128+
*/
129+
private function arrayKeysToSnakeCase(array $array): array
130+
{
131+
$result = [];
132+
foreach ($array as $key => $value) {
133+
// Convert key in snake_case
134+
$newKey = $this->toSnakeCase($key);
135+
if (is_array($value)) {
136+
// If value is an array, apply recusivity
137+
$result[$newKey] = $this->arrayKeysToSnakeCase($value);
138+
} else {
139+
// Else keep the value as is
140+
$result[$newKey] = $value;
141+
}
142+
}
143+
return $result;
144+
}
145+
}

0 commit comments

Comments
 (0)