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 |
|
$this->twig = $this->getTwig($templatesDir, $optionTwig); |
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Build html report. |
47
|
|
|
* |
48
|
|
|
* @param DocumentInterface $document |
49
|
|
|
* @param array $parameters |
50
|
|
|
* @return mixed |
51
|
|
|
* @throws \Twig_Error_Loader |
52
|
|
|
* @throws \Twig_Error_Runtime |
53
|
|
|
* @throws \Twig_Error_Syntax |
54
|
|
|
*/ |
55
|
4 |
|
public function render(DocumentInterface $document, $parameters = []) |
56
|
|
|
{ |
57
|
4 |
|
$html = $this->twig->render($this->template, [ |
58
|
4 |
|
'doc' => $document, |
59
|
|
|
'params' => $parameters |
60
|
4 |
|
]); |
61
|
|
|
|
62
|
4 |
|
return $html; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set filename templte. |
67
|
|
|
* |
68
|
|
|
* @param string $template |
69
|
|
|
*/ |
70
|
4 |
|
public function setTemplate($template) |
71
|
|
|
{ |
72
|
4 |
|
$this->template = $template; |
73
|
4 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $directory |
77
|
|
|
* @param $options |
78
|
|
|
* @return \Twig_Environment |
79
|
|
|
*/ |
80
|
4 |
|
private function getTwig($directory, $options) |
81
|
|
|
{ |
82
|
4 |
|
$loader = new \Twig_Loader_Filesystem($directory); |
83
|
4 |
|
$twig = new \Twig_Environment($loader, $options); |
84
|
4 |
|
$twig->addRuntimeLoader(new RuntimeLoader()); |
85
|
4 |
|
$twig->addExtension(new ReportTwigExtension()); |
86
|
|
|
|
87
|
4 |
|
return $twig; |
88
|
|
|
} |
89
|
|
|
} |