Skip to content

Commit

Permalink
[9.x] Allow section payload to be lazy in the "about" command (#43329)
Browse files Browse the repository at this point in the history
* allow invokables

* fix type

* improve type check

* retain collection

* demonstrate lazy evaluation with built in data registration
  • Loading branch information
timacdonald authored Jul 21, 2022
1 parent b141ba9 commit d507623
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions src/Illuminate/Foundation/Console/AboutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,32 @@ public function handle()
{
$this->gatherApplicationInformation();

collect(static::$data)->sortBy(function ($data, $key) {
$index = array_search($key, ['Environment', 'Cache', 'Drivers']);

if ($index === false) {
return 99;
}

return $index;
})
collect(static::$data)
->map(fn ($items) => collect($items)
->map(function ($value) {
if (is_array($value)) {
return [$value];
}

if (is_string($value)) {
$value = $this->laravel->make($value);
}

return collect($this->laravel->call($value))
->map(fn ($value, $key) => [$key, $value])
->values()
->all();
})->flatten(1)
)
->sortBy(function ($data, $key) {
$index = array_search($key, ['Environment', 'Cache', 'Drivers']);

if ($index === false) {
return 99;
}

return $index;
})
->filter(function ($data, $key) {
return $this->option('only') ? in_array(Str::of($key)->lower()->snake(), $this->sections()) : true;
})
Expand Down Expand Up @@ -115,13 +132,11 @@ protected function displayDetail($data)

$this->components->twoColumnDetail(' <fg=green;options=bold>'.$section.'</>');

sort($data);

foreach ($data as $detail) {
$data->sort()->each(function ($detail) {
[$label, $value] = $detail;

$this->components->twoColumnDetail($label, value($value));
}
});
});
}

Expand All @@ -134,7 +149,7 @@ protected function displayDetail($data)
protected function displayJson($data)
{
$output = $data->flatMap(function ($data, $section) {
return [(string) Str::of($section)->snake() => collect($data)->mapWithKeys(fn ($item, $key) => [(string) Str::of($item[0])->lower()->snake() => value($item[1])])];
return [(string) Str::of($section)->snake() => $data->mapWithKeys(fn ($item, $key) => [(string) Str::of($item[0])->lower()->snake() => value($item[1])])];
});

$this->output->writeln(strip_tags(json_encode($output)));
Expand All @@ -143,11 +158,11 @@ protected function displayJson($data)
/**
* Gather information about the application.
*
* @return array
* @return void
*/
protected function gatherApplicationInformation()
{
static::add('Environment', [
static::add('Environment', fn () => [
'Laravel Version' => $this->laravel->version(),
'PHP Version' => phpversion(),
'Composer Version' => $this->composer->getVersion() ?? '<fg=yellow;options=bold>-</>',
Expand All @@ -158,7 +173,7 @@ protected function gatherApplicationInformation()
'Maintenance Mode' => $this->laravel->isDownForMaintenance() ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF',
]);

static::add('Cache', [
static::add('Cache', fn () => [
'Config' => file_exists($this->laravel->bootstrapPath('cache/config.php')) ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>',
'Routes' => file_exists($this->laravel->bootstrapPath('cache/routes-v7.php')) ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>',
'Events' => file_exists($this->laravel->bootstrapPath('cache/events.php')) ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>',
Expand All @@ -177,7 +192,7 @@ protected function gatherApplicationInformation()
$logs = $logChannel;
}

static::add('Drivers', array_filter([
static::add('Drivers', fn () => array_filter([
'Broadcasting' => config('broadcasting.default'),
'Cache' => config('cache.default'),
'Database' => config('database.default'),
Expand Down Expand Up @@ -215,7 +230,7 @@ protected function sections()
* Add additional data to the output of the "about" command.
*
* @param string $section
* @param string|array $data
* @param callable|string|array $data
* @param string|null $value
* @return void
*/
Expand All @@ -225,6 +240,8 @@ public static function add(string $section, $data, string $value = null)
foreach ($data as $key => $value) {
self::$data[$section][] = [$key, $value];
}
} elseif (is_callable($data) || ($value === null && class_exists($data))) {
self::$data[$section][] = $data;
} else {
self::$data[$section][] = [$data, $value];
}
Expand Down

0 comments on commit d507623

Please sign in to comment.