-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from dkreuer/feature/different-output-formats
Add `--output=json` and `--output=text` option to CLI, to allow for better automation/reporting based on `STDOUT`
- Loading branch information
Showing
7 changed files
with
398 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ComposerRequireChecker\Cli\ResultsWriter; | ||
|
||
use DateTimeImmutable; | ||
|
||
use function json_encode; | ||
|
||
use const JSON_THROW_ON_ERROR; | ||
|
||
final class CliJson implements ResultsWriter | ||
{ | ||
/** @var callable(string): void */ | ||
private $writeCallable; | ||
private string $applicationVersion; | ||
/** @var callable(): DateTimeImmutable */ | ||
private $nowCallable; | ||
|
||
/** | ||
* @param callable(string): void $write | ||
* @param callable(): DateTimeImmutable $now | ||
*/ | ||
public function __construct(callable $write, string $applicationVersion, callable $now) | ||
{ | ||
$this->writeCallable = $write; | ||
$this->applicationVersion = $applicationVersion; | ||
$this->nowCallable = $now; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write(array $unknownSymbols): void | ||
{ | ||
$write = $this->writeCallable; | ||
$now = $this->nowCallable; | ||
|
||
$write( | ||
json_encode( | ||
[ | ||
'_meta' => [ | ||
'composer-require-checker' => [ | ||
'version' => $this->applicationVersion, | ||
], | ||
'date' => $now()->format(DateTimeImmutable::ATOM), | ||
], | ||
'unknown-symbols' => $unknownSymbols, | ||
], | ||
JSON_THROW_ON_ERROR | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ComposerRequireChecker\Cli\ResultsWriter; | ||
|
||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
use function count; | ||
use function implode; | ||
|
||
final class CliText implements ResultsWriter | ||
{ | ||
private OutputInterface $output; | ||
/** @var callable */ | ||
private $writeCallable; | ||
|
||
public function __construct(OutputInterface $output, ?callable $write = null) | ||
{ | ||
$this->output = $output; | ||
if ($write === null) { | ||
$write = static function (string $string) use ($output): void { | ||
$output->write($string); | ||
}; | ||
} | ||
|
||
$this->writeCallable = $write; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write(array $unknownSymbols): void | ||
{ | ||
if (! $unknownSymbols) { | ||
$this->output->writeln('There were no unknown symbols found.'); | ||
|
||
return; | ||
} | ||
|
||
$this->output->writeln('The following ' . count($unknownSymbols) . ' unknown symbols were found:'); | ||
|
||
$tableOutput = new BufferedOutput(); | ||
$table = new Table($tableOutput); | ||
$table->setHeaders(['Unknown Symbol', 'Guessed Dependency']); | ||
foreach ($unknownSymbols as $unknownSymbol => $guessedDependencies) { | ||
$table->addRow([$unknownSymbol, implode("\n", $guessedDependencies)]); | ||
} | ||
|
||
$table->render(); | ||
|
||
$write = $this->writeCallable; | ||
$write($tableOutput->fetch()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/ComposerRequireChecker/Cli/ResultsWriter/ResultsWriter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ComposerRequireChecker\Cli\ResultsWriter; | ||
|
||
interface ResultsWriter | ||
{ | ||
/** | ||
* @param array<array-key, list<string>> $unknownSymbols the unknown symbols found | ||
*/ | ||
public function write(array $unknownSymbols): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.