Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Improves output when using php artisan about --json #49154

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 50 additions & 20 deletions src/Illuminate/Foundation/Console/AboutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Foundation\Console;

use Closure;
use Illuminate\Console\Command;
use Illuminate\Support\Composer;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -127,7 +128,7 @@ protected function displayDetail($data)
$data->pipe(fn ($data) => $section !== 'Environment' ? $data->sort() : $data)->each(function ($detail) {
[$label, $value] = $detail;

$this->components->twoColumnDetail($label, value($value));
$this->components->twoColumnDetail($label, value($value, false));
});
});
}
Expand All @@ -143,7 +144,7 @@ protected function displayJson($data)
$output = $data->flatMap(function ($data, $section) {
return [
(string) Str::of($section)->snake() => $data->mapWithKeys(fn ($item, $key) => [
$this->toSearchKeyword($item[0]) => value($item[1]),
$this->toSearchKeyword($item[0]) => value($item[1], true),
]),
];
});
Expand All @@ -158,40 +159,48 @@ protected function displayJson($data)
*/
protected function gatherApplicationInformation()
{
$formatEnabledStatus = fn ($value) => $value ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF';
$formatCachedStatus = fn ($value) => $value ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>';

static::addToSection('Environment', fn () => [
'Application Name' => config('app.name'),
'Laravel Version' => $this->laravel->version(),
'PHP Version' => phpversion(),
'Composer Version' => $this->composer->getVersion() ?? '<fg=yellow;options=bold>-</>',
'Environment' => $this->laravel->environment(),
'Debug Mode' => config('app.debug') ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF',
'Debug Mode' => static::format(config('app.debug'), console: $formatEnabledStatus),
'URL' => Str::of(config('app.url'))->replace(['http://', 'https://'], ''),
'Maintenance Mode' => $this->laravel->isDownForMaintenance() ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF',
'Maintenance Mode' => static::format($this->laravel->isDownForMaintenance(), console: $formatEnabledStatus),
]);

static::addToSection('Cache', fn () => [
'Config' => $this->laravel->configurationIsCached() ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>',
'Events' => $this->laravel->eventsAreCached() ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>',
'Routes' => $this->laravel->routesAreCached() ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>',
'Views' => $this->hasPhpFiles($this->laravel->storagePath('framework/views')) ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>',
'Config' => static::format($this->laravel->configurationIsCached(), console: $formatCachedStatus),
'Events' => static::format($this->laravel->eventsAreCached(), console: $formatCachedStatus),
'Routes' => static::format($this->laravel->routesAreCached(), console: $formatCachedStatus),
'Views' => static::format($this->hasPhpFiles($this->laravel->storagePath('framework/views')), console: $formatCachedStatus),
]);

$logChannel = config('logging.default');

if (config('logging.channels.'.$logChannel.'.driver') === 'stack') {
$secondary = collect(config('logging.channels.'.$logChannel.'.channels'))
->implode(', ');

$logs = '<fg=yellow;options=bold>'.$logChannel.'</> <fg=gray;options=bold>/</> '.$secondary;
} else {
$logs = $logChannel;
}

static::addToSection('Drivers', fn () => array_filter([
'Broadcasting' => config('broadcasting.default'),
'Cache' => config('cache.default'),
'Database' => config('database.default'),
'Logs' => $logs,
'Logs' => function ($json) {
$logChannel = config('logging.default');

if (config('logging.channels.'.$logChannel.'.driver') === 'stack') {
$secondary = collect(config('logging.channels.'.$logChannel.'.channels'));

return value(static::format(
value: $logChannel,
console: fn ($value) => '<fg=yellow;options=bold>'.$value.'</> <fg=gray;options=bold>/</> '.$secondary->implode(', '),
json: fn () => $secondary->all(),
), $json);
} else {
$logs = $logChannel;
}

return $logs;
},
'Mail' => config('mail.default'),
'Octane' => config('octane.server'),
'Queue' => config('queue.default'),
Expand Down Expand Up @@ -260,6 +269,27 @@ protected function sections()
->all();
}

/**
* Materialize a function that formats a given value for CLI or JSON output.
*
* @param mixed $value
* @param (\Closure():(mixed))|null $console
* @param (\Closure():(mixed))|null $json
* @return \Closure(bool):mixed
*/
public static function format($value, Closure $console = null, Closure $json = null)
{
return function ($isJson) use ($value, $console, $json) {
if ($isJson === true && $json instanceof Closure) {
return value($json, $value);
} elseif ($isJson === false && $console instanceof Closure) {
return value($console, $value);
}

return value($value);
};
}

/**
* Format the given string for searching.
*
Expand Down
42 changes: 42 additions & 0 deletions tests/Foundation/Console/AboutCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Illuminate\Tests\Foundation\Console;

use Illuminate\Foundation\Console\AboutCommand;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class AboutCommandTest extends TestCase
{
/**
* @param \Closure(bool):mixed $format
* @param mixed $expected
*/
#[DataProvider('cliDataProvider')]
public function testItCanFormatForCliInterface($format, $expected)
{
$this->assertSame($expected, value($format, false));
}

public static function cliDataProvider()
{
yield [AboutCommand::format(true, console: fn ($value) => $value === true ? 'YES' : 'NO'), 'YES'];
yield [AboutCommand::format(false, console: fn ($value) => $value === true ? 'YES' : 'NO'), 'NO'];
}

/**
* @param \Closure(bool):mixed $format
* @param mixed $expected
*/
#[DataProvider('jsonDataProvider')]
public function testItCanFormatForJsonInterface($format, $expected)
{
$this->assertSame($expected, value($format, true));
}

public static function jsonDataProvider()
{
yield [AboutCommand::format(true, json: fn ($value) => $value === true ? 'YES' : 'NO'), 'YES'];
yield [AboutCommand::format(false, json: fn ($value) => $value === true ? 'YES' : 'NO'), 'NO'];
}
}
43 changes: 43 additions & 0 deletions tests/Integration/Foundation/Console/AboutCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Illuminate\Tests\Integration\Foundation\Console;

use Illuminate\Testing\Assert;
use Orchestra\Testbench\TestCase;

use function Orchestra\Testbench\remote;

class AboutCommandTest extends TestCase
{
public function testItCanDisplayAboutCommandAsJson()
{
$process = remote('about --json')->mustRun();

tap(json_decode($process->getOutput(), true), function ($output) {
Assert::assertArraySubset([
'application_name' => 'Laravel',
'php_version' => PHP_VERSION,
'environment' => 'testing',
'debug_mode' => true,
'url' => 'localhost',
'maintenance_mode' => false,
], $output['environment']);

Assert::assertArraySubset([
'config' => false,
'events' => false,
'routes' => false,
], $output['cache']);

Assert::assertArraySubset([
'broadcasting' => 'log',
'cache' => 'file',
'database' => 'testing',
'logs' => ['single'],
'mail' => 'smtp',
'queue' => 'sync',
'session' => 'file',
], $output['drivers']);
});
}
}