1
|
|
|
<?php |
2
|
|
|
namespace phphound\output; |
3
|
|
|
|
4
|
|
|
use League\Plates\Engine; |
5
|
|
|
use phphound\Analyser; |
6
|
|
|
use phphound\AnalysisResult; |
7
|
|
|
use phphound\output\html\FileHighlighter; |
8
|
|
|
use phphound\output\html\History; |
9
|
|
|
use DateTime; |
10
|
|
|
use SplFileObject; |
11
|
|
|
|
12
|
|
|
class HtmlOutput extends AbstractOutput implements TriggerableInterface |
13
|
|
|
{ |
14
|
|
|
use TextTriggerTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Plates engine used to output HTML. |
18
|
|
|
* @var Engine Plates engine instance. |
19
|
|
|
*/ |
20
|
|
|
protected $platesEngine; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
|
|
public function result(AnalysisResult $result) |
26
|
|
|
{ |
27
|
|
|
$this->cli->br(); |
28
|
|
|
$this->cli->inline('Writing HTML report in "./phphound/"... '); |
29
|
|
|
$history = new History($this->getOutputDirectory()); |
30
|
|
|
$history->append($result); |
31
|
|
|
foreach ($result->toArray() as $filePath => $lines) { |
32
|
|
|
$this->writeFileHtml($filePath, $lines); |
33
|
|
|
} |
34
|
|
|
$this->writeIndexHtml($result, $history); |
35
|
|
|
$history->save(); |
36
|
|
|
$this->cli->out('Done!'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create HTML report index page. |
41
|
|
|
* @param AnalysisResult $result result data object. |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
protected function writeIndexHtml(AnalysisResult $result, History $history) |
45
|
|
|
{ |
46
|
|
|
$files = []; |
47
|
|
|
|
48
|
|
|
foreach ($result->toArray() as $fileName => $lines) { |
49
|
|
|
if (!isset($files[$fileName])) { |
50
|
|
|
$files[$fileName] = 0; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($lines as $issues) { |
54
|
|
|
$files[$fileName] += count($issues); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$chartData = $history->getData(); |
59
|
|
|
$indexHtml = $this->renderView( |
60
|
|
|
'index', |
61
|
|
|
[ |
62
|
|
|
'files' => $files, |
63
|
|
|
'executions' => $chartData['executions'], |
64
|
|
|
'historyData' => array_values($chartData['historyData']), |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
$fileName = $this->getOutputDirectory() . '/index.html'; |
68
|
|
|
$file = new SplFileObject($fileName, 'w'); |
69
|
|
|
$file->fwrite($indexHtml); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create HTML report for one file. |
74
|
|
|
* @param string $phpFilePath analyzed PHP file path. |
75
|
|
|
* @param array $lines lines with their issues. |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
protected function writeFileHtml($phpFilePath, $lines) |
79
|
|
|
{ |
80
|
|
|
$highlighter = new FileHighlighter($phpFilePath, $lines); |
81
|
|
|
$fileHtml = $this->renderView( |
82
|
|
|
'file', |
83
|
|
|
[ |
84
|
|
|
'fileName' => $phpFilePath, |
85
|
|
|
'lines' => $lines, |
86
|
|
|
'fileContent' => $highlighter->getHtml(), |
87
|
|
|
'backButton' => true, |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
$htmlFileName = $this->getOutputDirectory() . '/' |
91
|
|
|
. str_replace(DIRECTORY_SEPARATOR, '_', $phpFilePath) . '.html'; |
92
|
|
|
|
93
|
|
|
$file = new SplFileObject($htmlFileName, 'w'); |
94
|
|
|
$file->fwrite($fileHtml); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Render a HTML view within the layout. |
99
|
|
|
* @param string $view view file name. |
100
|
|
|
* @param array $data variables required by view file. |
101
|
|
|
* @return string output HTML. |
102
|
|
|
*/ |
103
|
|
|
protected function renderView($view, $data) |
104
|
|
|
{ |
105
|
|
|
$date = new DateTime; |
106
|
|
|
$content = $this->getPlatesEngine()->render($view, $data); |
107
|
|
|
|
108
|
|
|
return $this->getPlatesEngine()->render( |
109
|
|
|
'layout', |
110
|
|
|
[ |
111
|
|
|
'content' => $content, |
112
|
|
|
'phpHoundVersion' => 'PHP Hound ' . Analyser::VERSION, |
113
|
|
|
'phpVersion' => 'PHP ' . phpversion(), |
114
|
|
|
'generationTime' => $date->format('r'), |
115
|
|
|
'backButton' => !empty($data['backButton']), |
116
|
|
|
] |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Configure and return Plates engine. |
122
|
|
|
* @return Engine Plates engine instance. |
123
|
|
|
*/ |
124
|
|
|
protected function getPlatesEngine() |
125
|
|
|
{ |
126
|
|
|
if (null === $this->platesEngine) { |
127
|
|
|
$this->platesEngine = new Engine(__DIR__ . '/../templates'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->platesEngine; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get output directory. |
135
|
|
|
* @return string output directory path. |
136
|
|
|
*/ |
137
|
|
|
protected function getOutputDirectory() |
138
|
|
|
{ |
139
|
|
|
$directory = $this->outputDirectory . '/phphound'; |
140
|
|
|
|
141
|
|
|
if (!file_exists($directory)) { |
142
|
|
|
mkdir($directory); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $directory; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|