1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Giansalex |
5
|
|
|
* Date: 17/09/2017 |
6
|
|
|
* Time: 21:55 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Greenter\Report; |
10
|
|
|
|
11
|
|
|
use Greenter\Model\DocumentInterface; |
12
|
|
|
use Greenter\Report\Extension\ReportTwigExtension; |
13
|
|
|
use Greenter\Report\Extension\RuntimeLoader; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class HtmlReport |
17
|
|
|
* @package Greenter\Report |
18
|
|
|
*/ |
19
|
|
|
class HtmlReport implements ReportInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \Twig_Environment |
23
|
|
|
*/ |
24
|
|
|
private $twig; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $template; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* HtmlReport constructor. |
33
|
|
|
* @param string $templatesDir |
34
|
|
|
* @param array $optionTwig |
35
|
|
|
*/ |
36
|
4 |
|
public function __construct($templatesDir = '', $optionTwig = []) |
37
|
|
|
{ |
38
|
4 |
|
if (empty($templatesDir)) { |
39
|
4 |
|
$templatesDir = __DIR__ . '/Templates'; |
40
|
4 |
|
} |
41
|
|
|
|
42
|
4 |
|
if (!isset($optionTwig['autoescape'])) { |
43
|
4 |
|
$optionTwig['autoescape'] = false; |
44
|
4 |
|
} |
45
|
|
|
|
46
|
4 |
|
$this->twig = $this->buildTwig($templatesDir, $optionTwig); |
47
|
4 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Build html report. |
51
|
|
|
* |
52
|
|
|
* @param DocumentInterface $document |
53
|
|
|
* @param array $parameters |
54
|
|
|
* @return mixed |
55
|
|
|
* @throws \Twig_Error_Loader |
56
|
|
|
* @throws \Twig_Error_Runtime |
57
|
|
|
* @throws \Twig_Error_Syntax |
58
|
|
|
*/ |
59
|
4 |
|
public function render(DocumentInterface $document, $parameters = []) |
60
|
|
|
{ |
61
|
4 |
|
$html = $this->twig->render($this->template, [ |
62
|
4 |
|
'doc' => $document, |
63
|
|
|
'params' => $parameters |
64
|
4 |
|
]); |
65
|
|
|
|
66
|
4 |
|
return $html; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set filename templte. |
71
|
|
|
* |
72
|
|
|
* @param string $template |
73
|
|
|
*/ |
74
|
4 |
|
public function setTemplate($template) |
75
|
|
|
{ |
76
|
4 |
|
$this->template = $template; |
77
|
4 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \Twig_Environment |
81
|
|
|
*/ |
82
|
4 |
|
public function getTwig() |
83
|
|
|
{ |
84
|
4 |
|
return $this->twig; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $directory |
89
|
|
|
* @param $options |
90
|
|
|
* @return \Twig_Environment |
91
|
|
|
*/ |
92
|
4 |
|
private function buildTwig($directory, $options) |
93
|
|
|
{ |
94
|
4 |
|
$loader = new \Twig_Loader_Filesystem($directory); |
95
|
4 |
|
$twig = new \Twig_Environment($loader, $options); |
96
|
4 |
|
$twig->addRuntimeLoader(new RuntimeLoader()); |
97
|
4 |
|
$twig->addExtension(new ReportTwigExtension()); |
98
|
|
|
|
99
|
4 |
|
return $twig; |
100
|
|
|
} |
101
|
|
|
} |