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