Overview

Namespaces

  • PHP
  • XSLTBenchmarking
    • Reports
    • RunnerConsole
    • TestsGenerator
    • TestsRunner

Classes

  • Convertor
  • HtmlConvertorDriver
  • Merger
  • Printer
  • Report

Interfaces

  • IConvertorDriver
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * XSLT Benchmarking
  5:  * @link https://github.com/masicek/XSLT-Benchmarking
  6:  * @author Viktor Mašíček <viktor@masicek.net>
  7:  * @license "New" BSD License
  8:  */
  9: 
 10: namespace XSLTBenchmarking\Reports;
 11: 
 12: require_once __DIR__ . '/IConvertorDriver.php';
 13: require_once ROOT . '/Exceptions.php';
 14: require_once ROOT . '/TestsGenerator/Templating/SmartyTemplatingDriver.php';
 15: require_once LIBS . '/PhpPath/PhpPath.min.php';
 16: 
 17: use PhpPath\P;
 18: use XSLTBenchmarking\TestsGenerator\SmartyTemplatingDriver;
 19: 
 20: /**
 21:  * Converting XML reports into HTML format
 22:  *
 23:  * @author Viktor Mašíček <viktor@masicek.net>
 24:  */
 25: class HtmlConvertorDriver implements IConvertorDriver
 26: {
 27: 
 28: 
 29:     /**
 30:      * Path of temporary directory
 31:      *
 32:      * @var string
 33:      */
 34:     private $tmpDir;
 35: 
 36: 
 37:     /**
 38:      * Configure object
 39:      *
 40:      * @param string $tmpDir Path of temporary directory
 41:      */
 42:     public function __construct($tmpDir)
 43:     {
 44:         $this->tmpDir = P::mcd($tmpDir);
 45:     }
 46: 
 47: 
 48:     /**
 49:      * Convert reports into set format and save it into set directory.
 50:      *
 51:      * @param string $inputFile Report file for converting
 52:      * @param string $outputDir Directory to save generated file
 53:      *
 54:      * @return string
 55:      */
 56:     public function convert($inputFile, $outputDir)
 57:     {
 58:         // output file name
 59:         $inputFile = P::mcf($inputFile);
 60:         $name = pathinfo($inputFile, PATHINFO_FILENAME) . '.html';
 61:         $outputFile = P::m(P::mcd($outputDir), $name);
 62: 
 63:         $report = $this->getReport($inputFile);
 64: 
 65:         // list of processors
 66:         $processorsList = array();
 67:         $processorsFullNames = array();
 68:         foreach ($report->xpath('//global/processors/processor') as $processor)
 69:         {
 70:             $name = (string)$processor['name'];
 71:             unset($processor['name']);
 72: 
 73:             $processorsFullNames[$name] = (string)$processor['fullName'];
 74:             unset($processor['fullName']);
 75: 
 76:             $processorsList[$name] = $this->getAttributes($processor);
 77:         }
 78: 
 79:         // list of tests
 80:         $tests = array();
 81:         foreach ($report->xpath('//tests/test') as $test)
 82:         {
 83:             $processors = array();
 84:             foreach ($test->xpath('processor') as $processor)
 85:             {
 86:                 $inputs = array();
 87:                 foreach ($processor->xpath('input') as $input)
 88:                 {
 89:                     $inputs[] = $this->getAttributes($input);
 90:                 }
 91: 
 92:                 $processorName = (string)$processor['name'];
 93:                 $processors[$processorName] = $inputs;
 94:             }
 95: 
 96:             $testName = (string)$test['name'];
 97:             $tests[$testName]['template'] = (string)$test['template'];
 98:             $tests[$testName]['processors'] = $processors;
 99:         }
100: 
101:         // settings
102:         $settings = array();
103:         $settings['processors'] = $processorsList;
104:         $settings['processorsFullNames'] = $processorsFullNames;
105:         $settings['tests'] = $tests;
106:         $settings['outputFiler'] = 'nothing';
107: 
108:         $templating = new SmartyTemplatingDriver($this->tmpDir);
109:         $templating->generate(P::m(__DIR__, 'report.tpl.html'), $outputFile, $settings);
110: 
111:         return $outputFile;
112:     }
113: 
114: 
115:     /**
116:      * Load report file into \SimpleXML object.
117:      *
118:      * @param string $path Path of report file for reading
119:      *
120:      * @return \SimpleXMLElement
121:      */
122:     private function getReport($path)
123:     {
124:         // validate
125:         $dom = new \DOMDocument();
126:         $dom->load($path);
127:         try {
128:             $dom->schemaValidate(P::m(__DIR__, '/../Report.xsd'));
129:         } catch (\Exception $e) {
130:             $error = libxml_get_last_error();
131:             throw new \XSLTBenchmarking\InvalidArgumentException(
132:                 'File "' . $path . '" has wrong format: ' . $error->message
133:             );
134:         }
135: 
136:         // make SimpleXML
137:         $report = new \SimpleXMLElement($path, 0, TRUE);
138: 
139:         return $report;
140:     }
141: 
142: 
143:     /**
144:      * Return attributes of element as array
145:      *
146:      * @param \SimpleXMLElement $element
147:      *
148:      * @return array array([ATTRIBUTE] => [VALUE], ...)
149:      */
150:     private function getAttributes(\SimpleXMLElement $element)
151:     {
152:         $attributes = array();
153:         foreach ($element->attributes() as $attribute => $value)
154:         {
155:             $attributes[(string)$attribute] = (string)$value;
156:         }
157:         return $attributes;
158:     }
159: 
160: 
161: }
162: 
XSTL Benchmarking API documentation generated by ApiGen.
Generated using the TokenReflection library.