1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace PHPJasper; |
||
6 | |||
7 | class PHPJasper |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $command; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $executable; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $pathExecutable; |
||
24 | |||
25 | /** |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $windows; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $formats = ['pdf', 'rtf', 'xls', 'xlsx', 'docx', 'odt', 'ods', 'pptx', 'csv', 'html', 'xhtml', 'xml', 'jrprint']; |
||
34 | |||
35 | protected $lang; |
||
36 | |||
37 | /** |
||
38 | * PHPJasper constructor |
||
39 | */ |
||
40 | public function __construct($lang = "pt_BR.UTF-8") |
||
41 | { |
||
42 | $this->lang = $lang; |
||
43 | $this->executable = 'jasperstarter'; |
||
44 | $this->pathExecutable = __DIR__ . '/../bin/jasperstarter/bin'; |
||
45 | $this->windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? true : false; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | private function checkServer() |
||
52 | { |
||
53 | return $this->command = ($this->windows) ? $this->executable : 'LANG=' . $this->lang . ' ./' . $this->executable; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param string $input |
||
58 | * @param string $output optional |
||
59 | * @return $this |
||
60 | * @throws Exception\InvalidInputFile |
||
61 | */ |
||
62 | public function compile(string $input, string $output = '') |
||
63 | { |
||
64 | if (!is_file($input)) { |
||
65 | throw new \PHPJasper\Exception\InvalidInputFile(); |
||
66 | } |
||
67 | |||
68 | $this->command = $this->checkServer(); |
||
69 | $this->command .= ' compile '; |
||
70 | $this->command .= '"' . realpath($input) . '"'; |
||
71 | |||
72 | if (!empty($output)) { |
||
73 | $this->command .= ' -o ' . "\"$output\""; |
||
74 | } |
||
75 | |||
76 | return $this; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param string $input |
||
81 | * @param string $output |
||
82 | * @param array $options |
||
83 | * @return $this |
||
84 | * @throws Exception\InvalidInputFile |
||
85 | * @throws Exception\InvalidFormat |
||
86 | */ |
||
87 | public function process(string $input, string $output, array $options = []) |
||
88 | { |
||
89 | $options = $this->parseProcessOptions($options); |
||
90 | |||
91 | if (!$input) { |
||
92 | throw new \PHPJasper\Exception\InvalidInputFile(); |
||
93 | } |
||
94 | |||
95 | $this->validateFormat($options['format']); |
||
96 | |||
97 | $this->command = $this->checkServer(); |
||
98 | |||
99 | if ($options['locale']) { |
||
100 | $this->command .= " --locale {$options['locale']}"; |
||
101 | } |
||
102 | |||
103 | $this->command .= ' process '; |
||
104 | $this->command .= "\"$input\""; |
||
105 | $this->command .= ' -o ' . "\"$output\""; |
||
106 | |||
107 | $this->command .= ' -f ' . join(' ', $options['format']); |
||
108 | if ($options['params']) { |
||
109 | $this->command .= ' -P '; |
||
110 | foreach ($options['params'] as $key => $value) { |
||
111 | $this->command .= " " . $key . '="' . $value . '" ' . " "; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | if ($options['db_connection']) { |
||
116 | $mapDbParams = [ |
||
117 | 'driver' => '-t', |
||
118 | 'username' => '-u', |
||
119 | 'password' => '-p', |
||
120 | 'host' => '-H', |
||
121 | 'database' => '-n', |
||
122 | 'port' => '--db-port', |
||
123 | 'jdbc_driver' => '--db-driver', |
||
124 | 'jdbc_url' => '--db-url', |
||
125 | 'jdbc_dir' => '--jdbc-dir', |
||
126 | 'db_sid' => '-db-sid', |
||
127 | 'xml_xpath' => '--xml-xpath', |
||
128 | 'data_file' => '--data-file', |
||
129 | 'json_query' => '--json-query' |
||
130 | ]; |
||
131 | |||
132 | foreach ($options['db_connection'] as $key => $value) { |
||
133 | $this->command .= " {$mapDbParams[$key]} {$value}"; |
||
134 | } |
||
135 | |||
136 | if ($options['resources']) { |
||
137 | $this->command .= " -r {$options['resources']}"; |
||
138 | } |
||
139 | |||
140 | $this->command = $this->command . ' 2>&1'; |
||
141 | } |
||
142 | |||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param array $options |
||
148 | * @return array |
||
149 | */ |
||
150 | protected function parseProcessOptions(array $options) |
||
151 | { |
||
152 | $defaultOptions = [ |
||
153 | 'format' => ['pdf'], |
||
154 | 'params' => [], |
||
155 | 'resources' => false, |
||
156 | 'locale' => false, |
||
157 | 'db_connection' => [] |
||
158 | ]; |
||
159 | |||
160 | return array_merge($defaultOptions, $options); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param $format |
||
165 | * @throws Exception\InvalidFormat |
||
166 | */ |
||
167 | protected function validateFormat($format) |
||
168 | { |
||
169 | if (!is_array($format)) { |
||
170 | $format = [$format]; |
||
171 | } |
||
172 | foreach ($format as $value) { |
||
173 | if (!in_array($value, $this->formats)) { |
||
174 | throw new \PHPJasper\Exception\InvalidFormat(); |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param string $input |
||
181 | * @return $this |
||
182 | * @throws \Exception |
||
183 | */ |
||
184 | public function listParameters(string $input) |
||
185 | { |
||
186 | if (!is_file($input)) { |
||
187 | throw new \PHPJasper\Exception\InvalidInputFile(); |
||
188 | } |
||
189 | |||
190 | $this->command = $this->checkServer(); |
||
191 | $this->command .= ' list_parameters '; |
||
192 | $this->command .= '"'.realpath($input).'"'; |
||
193 | |||
194 | return $this; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param bool $user |
||
199 | * @return mixed |
||
200 | * @throws Exception\InvalidCommandExecutable |
||
201 | * @throws Exception\InvalidResourceDirectory |
||
202 | * @throws Exception\ErrorCommandExecutable |
||
203 | */ |
||
204 | public function execute($user = false) |
||
205 | { |
||
206 | $this->validateExecute(); |
||
207 | $this->addUserToCommand($user); |
||
208 | |||
209 | $output = []; |
||
210 | $returnVar = 0; |
||
211 | |||
212 | chdir($this->pathExecutable); |
||
213 | exec($this->command, $output, $returnVar); |
||
214 | if ($returnVar !== 0) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
215 | //throw new \PHPJasper\Exception\ErrorCommandExecutable(); |
||
216 | throw new \Exception("{$output[0]}", 1); |
||
217 | } |
||
218 | |||
219 | return $output; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function output() |
||
226 | { |
||
227 | return $this->command; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param $user |
||
232 | */ |
||
233 | protected function addUserToCommand($user) |
||
234 | { |
||
235 | if ($user && !$this->windows) { |
||
236 | $this->command = 'su -u ' . $user . " -c \"" . $this->command . "\""; |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @throws Exception\InvalidCommandExecutable |
||
242 | * @throws Exception\InvalidResourceDirectory |
||
243 | */ |
||
244 | protected function validateExecute() |
||
245 | { |
||
246 | if (!$this->command) { |
||
247 | throw new \PHPJasper\Exception\InvalidCommandExecutable(); |
||
248 | } |
||
249 | if (!is_dir($this->pathExecutable)) { |
||
250 | throw new \PHPJasper\Exception\InvalidResourceDirectory(); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 |