ReportsΒΆ

Report GeneratorΒΆ

The report generators are responsible for generating an object structure which can be rendered to a result.

Reports consist of tables, which consist of rows, which in turn consist of cells - which are infact Expression Language Nodes.

Example GeneratorΒΆ

namespace PhpBench\Examples\Extension\Report;

use PhpBench\Dom\Document;
use PhpBench\Expression\Ast\StringNode;
use PhpBench\Model\SuiteCollection;
use PhpBench\Registry\Config;
use PhpBench\Report\GeneratorInterface;
use PhpBench\Report\Model\Report;
use PhpBench\Report\Model\Reports;
use PhpBench\Report\Model\Table;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AcmeGenerator implements GeneratorInterface
{
    public function configure(OptionsResolver $options): void
    {
        $options->setDefaults([
            'title' => 'Cats report',
            'description' => 'Are cats really cats or are they dogs?',
        ]);
    }

    public function generate(SuiteCollection $suiteCollection, Config $config): Reports
    {
        $rows = [];

        foreach ([
            [ '🐈', 'Yes' ],
            [ 'πŸ•', 'No' ],
        ] as [$symbol, $isCat]) {
            $rows[] = [
                'symbol' => new StringNode($symbol),
                'is_cat' => new StringNode($isCat),
            ];
        }

        return Reports::fromReport(
            Report::fromTables(
                [
                    Table::fromRowArray($rows, 'Cat or dog?'),
                ],
                $config['title'],
                $config['description']
            )
        );
    }
}

And register with your DI container:

class AcmeExtension implements ExtensionInterface
{
    public function configure(OptionsResolver $resolver): void
    {
    }

    public function load(Container $container): void
    {
        $container->register(AcmeGenerator::class, function (Container $container) {
            return new AcmeGenerator();
        }, [
            ReportExtension::TAG_REPORT_GENERATOR => [
                'name' => 'catordog',
            ]
        ]);
    }
}

Use your new generator:

$ phpbench run --report=catordog

Cats report
===========

Are cats really cats or are they dogs?

This table will explain
+---------------+---------+
| Candidate Cat | Is Cat? |
+---------------+---------+
| 🐈             | Yes     |
| πŸ•             | No      |
+---------------+---------+