Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * This file is part of the PHP_CodeCoverage package.
|
Chris@0
|
4 *
|
Chris@0
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@0
|
6 *
|
Chris@0
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
8 * file that was distributed with this source code.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Generates an HTML report from an PHP_CodeCoverage object.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @since Class available since Release 1.0.0
|
Chris@0
|
15 */
|
Chris@0
|
16 class PHP_CodeCoverage_Report_HTML
|
Chris@0
|
17 {
|
Chris@0
|
18 /**
|
Chris@0
|
19 * @var string
|
Chris@0
|
20 */
|
Chris@0
|
21 private $templatePath;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * @var string
|
Chris@0
|
25 */
|
Chris@0
|
26 private $generator;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * @var int
|
Chris@0
|
30 */
|
Chris@0
|
31 private $lowUpperBound;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * @var int
|
Chris@0
|
35 */
|
Chris@0
|
36 private $highLowerBound;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Constructor.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param int $lowUpperBound
|
Chris@0
|
42 * @param int $highLowerBound
|
Chris@0
|
43 * @param string $generator
|
Chris@0
|
44 */
|
Chris@0
|
45 public function __construct($lowUpperBound = 50, $highLowerBound = 90, $generator = '')
|
Chris@0
|
46 {
|
Chris@0
|
47 $this->generator = $generator;
|
Chris@0
|
48 $this->highLowerBound = $highLowerBound;
|
Chris@0
|
49 $this->lowUpperBound = $lowUpperBound;
|
Chris@0
|
50
|
Chris@0
|
51 $this->templatePath = sprintf(
|
Chris@0
|
52 '%s%sHTML%sRenderer%sTemplate%s',
|
Chris@0
|
53 dirname(__FILE__),
|
Chris@0
|
54 DIRECTORY_SEPARATOR,
|
Chris@0
|
55 DIRECTORY_SEPARATOR,
|
Chris@0
|
56 DIRECTORY_SEPARATOR,
|
Chris@0
|
57 DIRECTORY_SEPARATOR
|
Chris@0
|
58 );
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * @param PHP_CodeCoverage $coverage
|
Chris@0
|
63 * @param string $target
|
Chris@0
|
64 */
|
Chris@0
|
65 public function process(PHP_CodeCoverage $coverage, $target)
|
Chris@0
|
66 {
|
Chris@0
|
67 $target = $this->getDirectory($target);
|
Chris@0
|
68 $report = $coverage->getReport();
|
Chris@0
|
69 unset($coverage);
|
Chris@0
|
70
|
Chris@0
|
71 if (!isset($_SERVER['REQUEST_TIME'])) {
|
Chris@0
|
72 $_SERVER['REQUEST_TIME'] = time();
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 $date = date('D M j G:i:s T Y', $_SERVER['REQUEST_TIME']);
|
Chris@0
|
76
|
Chris@0
|
77 $dashboard = new PHP_CodeCoverage_Report_HTML_Renderer_Dashboard(
|
Chris@0
|
78 $this->templatePath,
|
Chris@0
|
79 $this->generator,
|
Chris@0
|
80 $date,
|
Chris@0
|
81 $this->lowUpperBound,
|
Chris@0
|
82 $this->highLowerBound
|
Chris@0
|
83 );
|
Chris@0
|
84
|
Chris@0
|
85 $directory = new PHP_CodeCoverage_Report_HTML_Renderer_Directory(
|
Chris@0
|
86 $this->templatePath,
|
Chris@0
|
87 $this->generator,
|
Chris@0
|
88 $date,
|
Chris@0
|
89 $this->lowUpperBound,
|
Chris@0
|
90 $this->highLowerBound
|
Chris@0
|
91 );
|
Chris@0
|
92
|
Chris@0
|
93 $file = new PHP_CodeCoverage_Report_HTML_Renderer_File(
|
Chris@0
|
94 $this->templatePath,
|
Chris@0
|
95 $this->generator,
|
Chris@0
|
96 $date,
|
Chris@0
|
97 $this->lowUpperBound,
|
Chris@0
|
98 $this->highLowerBound
|
Chris@0
|
99 );
|
Chris@0
|
100
|
Chris@0
|
101 $directory->render($report, $target . 'index.html');
|
Chris@0
|
102 $dashboard->render($report, $target . 'dashboard.html');
|
Chris@0
|
103
|
Chris@0
|
104 foreach ($report as $node) {
|
Chris@0
|
105 $id = $node->getId();
|
Chris@0
|
106
|
Chris@0
|
107 if ($node instanceof PHP_CodeCoverage_Report_Node_Directory) {
|
Chris@0
|
108 if (!file_exists($target . $id)) {
|
Chris@0
|
109 mkdir($target . $id, 0777, true);
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 $directory->render($node, $target . $id . '/index.html');
|
Chris@0
|
113 $dashboard->render($node, $target . $id . '/dashboard.html');
|
Chris@0
|
114 } else {
|
Chris@0
|
115 $dir = dirname($target . $id);
|
Chris@0
|
116
|
Chris@0
|
117 if (!file_exists($dir)) {
|
Chris@0
|
118 mkdir($dir, 0777, true);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 $file->render($node, $target . $id . '.html');
|
Chris@0
|
122 }
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 $this->copyFiles($target);
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * @param string $target
|
Chris@0
|
130 */
|
Chris@0
|
131 private function copyFiles($target)
|
Chris@0
|
132 {
|
Chris@0
|
133 $dir = $this->getDirectory($target . 'css');
|
Chris@0
|
134 copy($this->templatePath . 'css/bootstrap.min.css', $dir . 'bootstrap.min.css');
|
Chris@0
|
135 copy($this->templatePath . 'css/nv.d3.min.css', $dir . 'nv.d3.min.css');
|
Chris@0
|
136 copy($this->templatePath . 'css/style.css', $dir . 'style.css');
|
Chris@0
|
137
|
Chris@0
|
138 $dir = $this->getDirectory($target . 'fonts');
|
Chris@0
|
139 copy($this->templatePath . 'fonts/glyphicons-halflings-regular.eot', $dir . 'glyphicons-halflings-regular.eot');
|
Chris@0
|
140 copy($this->templatePath . 'fonts/glyphicons-halflings-regular.svg', $dir . 'glyphicons-halflings-regular.svg');
|
Chris@0
|
141 copy($this->templatePath . 'fonts/glyphicons-halflings-regular.ttf', $dir . 'glyphicons-halflings-regular.ttf');
|
Chris@0
|
142 copy($this->templatePath . 'fonts/glyphicons-halflings-regular.woff', $dir . 'glyphicons-halflings-regular.woff');
|
Chris@0
|
143 copy($this->templatePath . 'fonts/glyphicons-halflings-regular.woff2', $dir . 'glyphicons-halflings-regular.woff2');
|
Chris@0
|
144
|
Chris@0
|
145 $dir = $this->getDirectory($target . 'js');
|
Chris@0
|
146 copy($this->templatePath . 'js/bootstrap.min.js', $dir . 'bootstrap.min.js');
|
Chris@0
|
147 copy($this->templatePath . 'js/d3.min.js', $dir . 'd3.min.js');
|
Chris@0
|
148 copy($this->templatePath . 'js/holder.min.js', $dir . 'holder.min.js');
|
Chris@0
|
149 copy($this->templatePath . 'js/html5shiv.min.js', $dir . 'html5shiv.min.js');
|
Chris@0
|
150 copy($this->templatePath . 'js/jquery.min.js', $dir . 'jquery.min.js');
|
Chris@0
|
151 copy($this->templatePath . 'js/nv.d3.min.js', $dir . 'nv.d3.min.js');
|
Chris@0
|
152 copy($this->templatePath . 'js/respond.min.js', $dir . 'respond.min.js');
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * @param string $directory
|
Chris@0
|
157 * @return string
|
Chris@0
|
158 * @throws PHP_CodeCoverage_Exception
|
Chris@0
|
159 * @since Method available since Release 1.2.0
|
Chris@0
|
160 */
|
Chris@0
|
161 private function getDirectory($directory)
|
Chris@0
|
162 {
|
Chris@0
|
163 if (substr($directory, -1, 1) != DIRECTORY_SEPARATOR) {
|
Chris@0
|
164 $directory .= DIRECTORY_SEPARATOR;
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 if (is_dir($directory)) {
|
Chris@0
|
168 return $directory;
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 if (@mkdir($directory, 0777, true)) {
|
Chris@0
|
172 return $directory;
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 throw new PHP_CodeCoverage_Exception(
|
Chris@0
|
176 sprintf(
|
Chris@0
|
177 'Directory "%s" does not exist.',
|
Chris@0
|
178 $directory
|
Chris@0
|
179 )
|
Chris@0
|
180 );
|
Chris@0
|
181 }
|
Chris@0
|
182 }
|