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
|
|
|
*/ |
18
|
|
|
class HtmlReport implements ReportInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \Twig_Environment |
22
|
|
|
*/ |
23
|
|
|
private $twig; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $template = 'invoice2.html.twig'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* HtmlReport constructor. |
32
|
|
|
* |
33
|
|
|
* @param string $templatesDir |
34
|
|
|
* @param array $optionTwig |
35
|
|
|
*/ |
36
|
10 |
|
public function __construct($templatesDir = '', $optionTwig = []) |
37
|
|
|
{ |
38
|
10 |
|
if (!isset($optionTwig['autoescape'])) { |
39
|
10 |
|
$optionTwig['autoescape'] = false; |
40
|
10 |
|
} |
41
|
|
|
|
42
|
10 |
|
$this->twig = $this->buildTwig($templatesDir, $optionTwig); |
43
|
10 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Build html report. |
47
|
|
|
* |
48
|
|
|
* @param DocumentInterface $document |
49
|
|
|
* @param array $parameters |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
* |
53
|
|
|
* @throws \Twig_Error_Loader |
54
|
|
|
* @throws \Twig_Error_Runtime |
55
|
|
|
* @throws \Twig_Error_Syntax |
56
|
|
|
*/ |
57
|
10 |
|
public function render(DocumentInterface $document, $parameters = []) |
58
|
|
|
{ |
59
|
10 |
|
$html = $this->twig->render($this->template, [ |
60
|
10 |
|
'doc' => $document, |
61
|
10 |
|
'params' => $parameters, |
62
|
10 |
|
]); |
63
|
|
|
|
64
|
10 |
|
return $html; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set filename templte. |
69
|
|
|
* |
70
|
|
|
* @param string $template |
71
|
|
|
*/ |
72
|
10 |
|
public function setTemplate($template) |
73
|
|
|
{ |
74
|
10 |
|
$this->template = $template; |
75
|
10 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return \Twig_Environment |
79
|
|
|
*/ |
80
|
10 |
|
public function getTwig() |
81
|
|
|
{ |
82
|
10 |
|
return $this->twig; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $directory |
87
|
|
|
* @param $options |
88
|
|
|
* |
89
|
|
|
* @return \Twig_Environment |
90
|
|
|
*/ |
91
|
10 |
|
private function buildTwig($directory, $options) |
92
|
|
|
{ |
93
|
10 |
|
$dirs = $this->getDirectories($directory); |
94
|
|
|
|
95
|
10 |
|
$loader = new \Twig_Loader_Filesystem($dirs); |
96
|
10 |
|
$twig = new \Twig_Environment($loader, $options); |
97
|
|
|
|
98
|
10 |
|
$twig->addRuntimeLoader(new RuntimeLoader()); |
99
|
10 |
|
$twig->addExtension(new ReportTwigExtension()); |
100
|
|
|
|
101
|
10 |
|
return $twig; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $directory |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
10 |
|
private function getDirectories($directory) |
110
|
|
|
{ |
111
|
10 |
|
$dirs = []; |
112
|
10 |
|
if ($directory) { |
113
|
|
|
$dirs[] = $directory; |
114
|
|
|
} |
115
|
10 |
|
$dirs[] = __DIR__.'/Templates'; |
116
|
|
|
|
117
|
10 |
|
return $dirs; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|