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 3 : $this->tmpDir = P::mcd($tmpDir);
45 2 : }
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 4 : $inputFile = P::mcf($inputFile);
60 3 : $name = pathinfo($inputFile, PATHINFO_FILENAME) . '.html';
61 3 : $outputFile = P::m(P::mcd($outputDir), $name);
62 :
63 2 : $report = $this->getReport($inputFile);
64 :
65 : // list of processors
66 1 : $processorsList = array();
67 1 : $processorsFullNames = array();
68 1 : foreach ($report->xpath('//global/processors/processor') as $processor)
69 : {
70 1 : $name = (string)$processor['name'];
71 1 : unset($processor['name']);
72 :
73 1 : $processorsFullNames[$name] = (string)$processor['fullName'];
74 1 : unset($processor['fullName']);
75 :
76 1 : $processorsList[$name] = $this->getAttributes($processor);
77 1 : }
78 :
79 : // list of tests
80 1 : $tests = array();
81 1 : foreach ($report->xpath('//tests/test') as $test)
82 : {
83 1 : $processors = array();
84 1 : foreach ($test->xpath('processor') as $processor)
85 : {
86 1 : $inputs = array();
87 1 : foreach ($processor->xpath('input') as $input)
88 : {
89 1 : $inputs[] = $this->getAttributes($input);
90 1 : }
91 :
92 1 : $processorName = (string)$processor['name'];
93 1 : $processors[$processorName] = $inputs;
94 1 : }
95 :
96 1 : $testName = (string)$test['name'];
97 1 : $tests[$testName]['template'] = (string)$test['template'];
98 1 : $tests[$testName]['processors'] = $processors;
99 1 : }
100 :
101 : // settings
102 1 : $settings = array();
103 1 : $settings['processors'] = $processorsList;
104 1 : $settings['processorsFullNames'] = $processorsFullNames;
105 1 : $settings['tests'] = $tests;
106 1 : $settings['outputFiler'] = 'nothing';
107 :
108 1 : $templating = new SmartyTemplatingDriver($this->tmpDir);
109 1 : $templating->generate(P::m(__DIR__, 'report.tpl.html'), $outputFile, $settings);
110 :
111 1 : 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 2 : $dom = new \DOMDocument();
126 2 : $dom->load($path);
127 : try {
128 2 : $dom->schemaValidate(P::m(__DIR__, '/../Report.xsd'));
129 2 : } catch (\Exception $e) {
130 1 : $error = libxml_get_last_error();
131 1 : throw new \XSLTBenchmarking\InvalidArgumentException(
132 1 : 'File "' . $path . '" has wrong format: ' . $error->message
133 1 : );
134 : }
135 :
136 : // make SimpleXML
137 1 : $report = new \SimpleXMLElement($path, 0, TRUE);
138 :
139 1 : 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 1 : $attributes = array();
153 1 : foreach ($element->attributes() as $attribute => $value)
154 : {
155 1 : $attributes[(string)$attribute] = (string)$value;
156 1 : }
157 1 : return $attributes;
158 : }
159 :
160 :
161 : }
|