Skip to content

Commit

Permalink
Merge pull request #72 from Hi-Folks/feature/70-better-output
Browse files Browse the repository at this point in the history
Feature/70 better output
  • Loading branch information
roberto-butti authored Jul 17, 2022
2 parents 9da64aa + 476f025 commit 297110f
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-php81.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: [ '8.0','7.4','8.1' ]
php-versions: [ '8.0','8.1' ]
dependency-stability: [ 'prefer-none' ]

name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ jobs:
run: composer run-script test

- name: PHP Syntax Checker (Lint)
uses: StephaneBour/actions-php-lint@7.2
uses: StephaneBour/actions-php-lint@8.0
with:
dir: './src'
2 changes: 1 addition & 1 deletion .github/workflows/test-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: [ '8.1', '8.0','7.4' ]
php-versions: [ '8.1', '8.0' ]
dependency-stability: [ prefer-stable ]

name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: [ '8.1', '8.0','7.4','7.3' ]
php-versions: [ '8.1', '8.0' ]
dependency-stability: [ prefer-stable ]

name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}}
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.3.0 - 2022-07-17
- Add Termwind package
- Review console output, thanks also to @exodusanto
- Support for PHP 8.0 and greater

## 0.2.7 - 2022-01-23
- Add Support for Laravel 9
- Add tests for PHP8.1
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
}
],
"require": {
"php": "^7.2|^8.0",
"php": "^7.4|^8.0|^8.1",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.0|^7.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0",
"ext-json": "*"
"nunomaduro/termwind": "^1.13"
},
"require-dev": {
"nunomaduro/larastan": "^0.7.2",
"nunomaduro/larastan": "^0.7.2|^1.0.0",
"orchestra/testbench": "4.*|5.*|6.*|7.*",
"phpunit/phpunit": "^8.0|^9.3",
"psalm/plugin-laravel": "^1.5",
Expand Down
50 changes: 50 additions & 0 deletions resources/views/laralens/term/checks.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<div class="mt-2 mx-1">
@if (sizeof($rows) > 0)
<div class="flex space-x-1">
<span class="text-red">CHECK: issues found</span>
<span class="flex-1 content-repeat-[─] text-red"></span>
</div>
@else
<div class="flex space-x-1">
<span class="text-green">CHECK: everything looks good</span>
<span class="flex-1 content-repeat-[─] text-green"></span>
</div>
@endif
<div>
@foreach ($rows as $row)
@php
$lineType = Arr::get($row, "lineType", HiFolks\LaraLens\ResultLens::LINE_TYPE_DEFAULT);
$label = Arr::get($row, "label", "");
@endphp
@if ($label === "*** HINT")
<div class="flex space-x-1 mt-1">
<span class="px-1 text-gray-100">💡 Hint:</span>
<span>
<span class="px-0 text-gray-300">
{{ Arr::get($row, "value", "") }}
</span>
</span>
</div>
@else
<div @class([
'w-full mx-1 py-1 mt-1 text-center font-bold',
'bg-red text-white' => $lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_ERROR,
'bg-yellow text-black' => $lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_WARNING,
'bg-green text-white' => $lineType !== HiFolks\LaraLens\ResultLens::LINE_TYPE_ERROR
&& $lineType !== HiFolks\LaraLens\ResultLens::LINE_TYPE_WARNING,
])>
{{ Arr::get($row, "label", "")}}
</div>
<div class="mt-1 mx-1 text-gray-300">
@if ($lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_ERROR)
{{ Arr::get($row, "value", "")}}
@elseif ($lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_WARNING)
{{ Arr::get($row, "value", "")}}
@else
{{ Str::replace("\\", "/", Arr::get($row, "value", "")) }}
@endif
</div>
@endif
@endforeach
</div>
</div>
52 changes: 52 additions & 0 deletions resources/views/laralens/term/table.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<div class="mt-1.5 mx-1">
<div class="flex space-x-1">
<span class="text-green">{{ $title }}</span>
<span class="flex-1 content-repeat-[─] text-gray"></span>
</div>
<div class="mt-1">
<span>
@foreach ($rows as $row)
@php
$lineType = Arr::get($row, "lineType", HiFolks\LaraLens\ResultLens::LINE_TYPE_DEFAULT);
$label = Arr::get($row, "label", "");
@endphp
@if ($label === "*** HINT")
<div class="flex space-x-1">
<span class=" text-white font-bold">Hint:</span>
<span class="flex-1 content-repeat-[.] text-gray"></span>
<div>
<span class="px-2 text-gray font-bold">
{{ Arr::get($row, "value", "") }}
</span>
</div>
</div>
@else
<div class="flex space-x-1">
<span class=" text-white font-bold">
{{ Arr::get($row, "label", "")}}
</span>

<span class="flex-1 content-repeat-[.] text-gray"></span>

<span>
@if ($lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_ERROR)
<span class="text-gray-100 bg-red font-bold">
{{ Arr::get($row, "value", "")}}
</span>
@elseif ($lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_WARNING)
<span class="text-gray-100 bg-yellow font-bold">
{{ Arr::get($row, "value", "")}}
</span>
@else
<span class="text-gray-100">
{{ Str::replace("\\", "/", Arr::get($row, "value", "")) }}
</span>
@endif
</span>
</div>
@endif
@endforeach
</span>
</div>

</div>
133 changes: 14 additions & 119 deletions src/Console/LaraLensCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace HiFolks\LaraLens\Console;

use HiFolks\LaraLens\Lens\LaraLens;
use HiFolks\LaraLens\Lens\Traits\TermOutput;
use HiFolks\LaraLens\ResultLens;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class LaraLensCommand extends Command
{
use TermOutput;

private const TABLE_STYLES = 'default|borderless|compact|symfony-style-guide|box|box-double';
private const DEFAULT_STYLE = 'box-double';
private const DEFAULT_PATH = '';
Expand Down Expand Up @@ -54,138 +57,30 @@ private function allConfigs(): void
$this->info(json_encode(config()->all(), JSON_PRETTY_PRINT));
}

private function formatCell($string, $width)
{
$retVal = "";
if (strlen($string) > $width) {
$retVal = Str::limit($string, $width, '');
} elseif (strlen($string) < $width) {
$retVal = str_pad($string, $width);
} else {
$retVal = $string;
}
return $retVal;
}

private function printOutput(array $headers, array $rows): void
{
$rowsTable = [];
$rowsLine = [];
foreach ($rows as $key => $row) {
$label = Arr::get($row, "label", "");
$value = Arr::get($row, "value", "");
if (is_array($value)) {
$value = implode(",", $value);
}
$isLine = Arr::get($row, "isLine", false);
$lineType = Arr::get($row, "lineType", ResultLens::LINE_TYPE_DEFAULT);
if (
strlen($value) > $this->widthValue ||
$isLine ||
$lineType === ResultLens::LINE_TYPE_ERROR ||
$lineType === ResultLens::LINE_TYPE_WARNING
) {
$rowsLine[] = $row;
} else {
$row["label"] = $this->formatCell($label, $this->widthLabel);
$row["value"] = $this->formatCell($value, $this->widthValue);
$rowsTable[] = [ $row["label"], $row["value"] ];
}
}
/*
* table style:
* 'default'
* 'borderless'
* 'compact'
* 'symfony-style-guide'
* 'box'
* 'box-double'
*/
$this->table($headers, $rowsTable, $this->styleTable);
foreach ($rowsLine as $key => $line) {
$label = Arr::get($line, "label", "");
$value = Arr::get($line, "value", "");
$lineType = Arr::get($line, "lineType", ResultLens::LINE_TYPE_DEFAULT);
if ($label != "") {
$this->info($label . ":");
}
if ($lineType === ResultLens::LINE_TYPE_ERROR) {
$this->error($value);
} elseif ($lineType === ResultLens::LINE_TYPE_WARNING) {
$this->warn($value);
} else {
$this->line($value);
}
}
}

private function alertGreen($string): void
{
$length = Str::length(strip_tags($string)) + 12;
$this->info(str_repeat('*', $length));
$this->info('* ' . $string . ' *');
$this->info(str_repeat('*', $length));
$this->output->newLine();
}

private function printChecks(array $rows): void
{
if (sizeof($rows) == 0) {
$this->alertGreen("CHECK: everything looks good");
} else {
$this->alert("CHECK: issues found");
}
$idx = 0;
foreach ($rows as $key => $row) {
$label = Arr::get($row, "label", "");
$value = Arr::get($row, "value", "");
$isLine = Arr::get($row, "isLine", false);
$lineType = Arr::get($row, "lineType", ResultLens::LINE_TYPE_DEFAULT);
if (
($label !== "") &&
(($lineType === ResultLens::LINE_TYPE_ERROR) ||
ResultLens::isMessageLine($lineType))
) {
$idx++;
$this->warn("--- " . $idx . " ------------------");
$this->warn("*** " . $label);
}
if ($lineType === ResultLens::LINE_TYPE_ERROR) {
$this->error($value);
} elseif ($lineType === ResultLens::LINE_TYPE_WARNING) {
$this->warn($value);
} elseif ($lineType === ResultLens::LINE_TYPE_INFO) {
$this->info($value);
} else {
$this->comment($value);
}
}
}

private function overview($checkTable = "users", $columnSorting = "created_at", $show = self::OPTION_SHOW_ALL): void
{
$ll = new LaraLens();
if ($show & self::OPTION_SHOW_CONFIGS) {
$output = $ll->getConfigs();
$this->printOutput(["Config keys via config()", "Values"], $output->toArray());
$this->printOutputTerm("Config keys via config()", $output->toArray());
}
if ($show & self::OPTION_SHOW_RUNTIMECONFIGS) {
$output = $ll->getRuntimeConfigs();
$this->printOutput(["Runtime Configs", "Values"], $output->toArray());
$this->printOutputTerm("Runtime Configs", $output->toArray());
$output = $ll->checkServerRequirements();
$this->printOutput(["Laravel Requirements", "Values"], $output->toArray());
$this->printOutputTerm("Laravel Requirements", $output->toArray());
}
if ($show & self::OPTION_SHOW_RUNTIMECONFIGS) {
$output = $ll->checkFiles();
$this->printOutput(["Check files", "Values"], $output->toArray());
$this->printOutputTerm("Check files", $output->toArray());
}
if ($show & self::OPTION_SHOW_CONNECTIONS) {
$output = $ll->getConnections($this->urlPath);
$this->printOutput(["Connections", "Values"], $output->toArray());
$this->printOutputTerm("Connections", $output->toArray());
}
if ($show & self::OPTION_SHOW_DATABASE) {
$output = $ll->getDatabase($checkTable, $columnSorting);
$this->printOutput(["Database", "Values"], $output->toArray());
$this->printOutputTerm("Database", $output->toArray());
}
if ($show & self::OPTION_SHOW_MIGRATION) {
try {
Expand All @@ -201,22 +96,22 @@ private function overview($checkTable = "users", $columnSorting = "created_at",
$e->getMessage(),
"Check the Database configuration"
);
$this->printOutput(["Migration" , "result"], $r->toArray());
$this->printOutputTerm("Migration", $r->toArray());
}
}
if ($show & self::OPTION_SHOW_PHPEXTENSIONS) {
$output = $ll->getPhpExtensions();
$this->printOutput(["PHP Extensions"], $output->toArray());
$this->printOutputTerm("PHP Extensions", $output->toArray());
}
if ($show & self::OPTION_SHOW_PHPINIVALUES) {
$output = $ll->getPhpIniValues();
$this->printOutput(["PHP ini config", "Values"], $output->toArray());
$this->printOutputTerm("PHP ini config", $output->toArray());
}
if ($show & self::OPTION_SHOW_OS) {
$output = $ll->getOsConfigs();
$this->printOutput(["Operating System", "Values"], $output->toArray());
$this->printOutputTerm("Operating System", $output->toArray());
}
$this->printChecks($ll->checksBag->toArray());
$this->printChecksTerm($ll->checksBag->toArray());
}


Expand Down
29 changes: 29 additions & 0 deletions src/Lens/Traits/TermOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace HiFolks\LaraLens\Lens\Traits;

use function Termwind\{render};

trait TermOutput
{
public function printOutputTerm(
string $title,
array $rows
) {
render(
view('lara-lens::laralens.term.table', [
'title' => $title,
'rows' => $rows
])
);
}

public function printChecksTerm(array $rows): void
{
render(
view('lara-lens::laralens.term.checks', [
'rows' => $rows
])
);
}
}

0 comments on commit 297110f

Please sign in to comment.