Esempio n. 1
0
 protected function renderTableElement(Element $tableEl, $config)
 {
     $rows = [];
     $colNames = [];
     foreach ($tableEl->query('.//col') as $colEl) {
         $colNames[] = $colEl->getAttribute('label');
     }
     foreach ($tableEl->query('.//row') as $rowEl) {
         $row = [];
         $formatterParams = [];
         foreach ($rowEl->query('./formatter-param') as $paramEl) {
             $formatterParams[$paramEl->getAttribute('name')] = $paramEl->nodeValue;
         }
         foreach ($rowEl->query('.//cell') as $cellEl) {
             $colName = $cellEl->getAttribute('name');
             $value = $cellEl->nodeValue;
             if ('' !== $value && $cellEl->hasAttribute('class')) {
                 $classes = explode(' ', $cellEl->getAttribute('class'));
                 $value = $this->formatter->applyClasses($classes, $value, $formatterParams);
             }
             $row[$colName] = $value;
         }
         $rows[] = $row;
     }
     $table = $this->createTable();
     // style only supported in Symfony > 2.4
     if (method_exists($table, 'setStyle')) {
         $table->setStyle($config['table_style']);
     }
     $table->setHeaders($colNames);
     $table->setRows($rows);
     $this->renderTable($table);
     $this->output->writeln('');
 }
Esempio n. 2
0
 /**
  * Render the table.
  *
  * @param mixed $tableDom
  * @param mixed $config
  */
 public function render(Document $reportDom, Config $config)
 {
     $template = $config['template'];
     $out = strtr($config['file'], ['%report_name%' => $reportDom->firstChild->getAttribute('name')]);
     if (!file_exists($template)) {
         throw new \RuntimeException(sprintf('XSLT template file "%s" does not exist', $template));
     }
     foreach ($reportDom->query('.//row') as $rowEl) {
         $formatterParams = [];
         foreach ($rowEl->query('./formatter-param') as $paramEl) {
             $formatterParams[$paramEl->getAttribute('name')] = $paramEl->nodeValue;
         }
         foreach ($rowEl->query('./cell') as $cellEl) {
             $value = $cellEl->nodeValue;
             if ('' !== $value && $cellEl->hasAttribute('class')) {
                 $classes = explode(' ', $cellEl->getAttribute('class'));
                 $value = $this->formatter->applyClasses($classes, $value, $formatterParams);
                 $cellEl->nodeValue = $value;
             }
         }
     }
     $stylesheetDom = new \DOMDocument('1.0');
     $stylesheetDom->load($template);
     $xsltProcessor = new \XsltProcessor();
     $xsltProcessor->importStylesheet($stylesheetDom);
     $xsltProcessor->setParameter(null, 'title', $config['title']);
     $xsltProcessor->setParameter(null, 'phpbench-version', PhpBench::VERSION);
     $xsltProcessor->setParameter(null, 'date', date('Y-m-d H:i:s'));
     $output = $xsltProcessor->transformToXml($reportDom);
     if (!$output) {
         throw new \InvalidArgumentException(sprintf('Could not render report with XSL file "%s"', $template));
     }
     if ($out) {
         file_put_contents($out, $output);
         $this->output->writeln('Dumped XSLT report:');
         $this->output->writeln($out);
     } else {
         $this->output->write($output);
     }
 }
Esempio n. 3
0
 private function registerFormatter(Container $container)
 {
     $container->register('phpbench.formatter.registry', function (Container $container) {
         $registry = new FormatRegistry();
         $registry->register('printf', new PrintfFormat());
         $registry->register('balance', new BalanceFormat());
         $registry->register('number', new NumberFormat());
         $registry->register('truncate', new TruncateFormat());
         $registry->register('time', new TimeUnitFormat($container->get('benchmark.time_unit')));
         return $registry;
     });
     $container->register('phpbench.formatter', function (Container $container) {
         $formatter = new Formatter($container->get('phpbench.formatter.registry'));
         $formatter->classesFromFile(__DIR__ . '/config/class/main.json');
         return $formatter;
     });
 }