Skip to content

Commit

Permalink
improve output option by introducing Reporter extension
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Dec 16, 2021
1 parent e464f74 commit 7c0334c
Show file tree
Hide file tree
Showing 11 changed files with 506 additions and 282 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-6.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ using the [Keep a CHANGELOG](http://keepachangelog.com) principles.

## [Unreleased]

### Added

- improves `output` option by introducing Reporter extension (see [documentation](docs/01_Components/04_Extensions/Reporter.md))

## [6.0.1] - 2021-12-13

### Fixed
Expand Down
43 changes: 43 additions & 0 deletions docs/01_Components/04_Extensions/Reporter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!-- markdownlint-disable MD013 -->
# Output format

**Since version 6.1.0**, PHP CompatInfo supports different output formats through various formatters.

You can pass the following keywords to the `--output` CLI option of the `analyser:run` command
in order to affect the output:

- `console`: default table format for human reading.
- `dump`: raw format (`var_dump`) for debugging purpose only.
- `json`: creates minified json file format output without whitespaces.

You can also implement your own custom formatter by implementing
the `Bartlett\CompatInfo\Application\Extension\Reporter\FormatterInterface` interface in a new class.

This is how the `FormatterInterface` interface looks like:

```php
<?php declare(strict_types=1);

namespace Bartlett\CompatInfo\Application\Extension\Reporter;

interface FormatterInterface
{
/**
* @param object $object
* @param string[] $formats
* @return bool
*/
public function supportsFormatting(object $object, array $formats): bool;

/**
* @param mixed $data Data to format
* @return void
*/
public function format($data): void;
}
```

Before you can start using your custom output formatter, you have to include it in a new class that implement
the `Bartlett\CompatInfo\Application\Extension\ExtensionInterface` interface (see [Registering Extensions](Hooks.md) chapter for details).

`ConsoleReporter` is a good first example to follow.
59 changes: 59 additions & 0 deletions src/Application/Extension/Reporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types=1);

namespace Bartlett\CompatInfo\Application\Extension;

use Bartlett\CompatInfo\Application\Event\AfterAnalysisEvent;
use Bartlett\CompatInfo\Application\Event\AfterAnalysisInterface;
use Bartlett\CompatInfo\Application\Profiler\Profile;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function in_array;

/**
* @author Laurent Laville
* @since Release 6.1.0
*/
abstract class Reporter implements ExtensionInterface, AfterAnalysisInterface
{
protected const NAME = 'custom';

/** @var InputInterface */
protected $input;

/** @var OutputInterface */
protected $output;

public function __construct(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
}

/**
* {@inheritDoc}
*/
public function getName(): string
{
return static::NAME . '_reporter';
}

/**
* @param string[] $formats
*/
public function supportsFormatting(object $object, array $formats): bool
{
return ($object instanceof Profile && in_array(static::NAME, $formats));
}

/**
* {@inheritDoc}
*/
public function afterAnalysis(AfterAnalysisEvent $event): void
{
if ($event->hasArgument('profile')) {
$this->format($event->getArgument('profile')); // @phpstan-ignore-line
}
}
}
Loading

0 comments on commit 7c0334c

Please sign in to comment.