Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Develop | |
100.00% |
16 / 16 |
|
100.00% |
5 / 5 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFunctions | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
dump | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
dd | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
setDumper | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
exit | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
flushBuffers | n/a |
0 / 0 |
n/a |
0 / 0 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Renderer\Twig\Extensions; |
6 | |
7 | use Engelsystem\Config\Config; |
8 | use Symfony\Component\VarDumper\VarDumper; |
9 | use Twig\Extension\AbstractExtension as TwigExtension; |
10 | use Twig\TwigFunction; |
11 | |
12 | class Develop extends TwigExtension |
13 | { |
14 | protected ?VarDumper $dumper = null; |
15 | |
16 | public function __construct(protected Config $config) |
17 | { |
18 | } |
19 | |
20 | /** |
21 | * @return TwigFunction[] |
22 | */ |
23 | public function getFunctions(): array |
24 | { |
25 | if ($this->config->get('environment') != 'development') { |
26 | return []; |
27 | } |
28 | |
29 | return [ |
30 | new TwigFunction('dump', [$this, 'dump'], ['is_safe' => ['html']]), |
31 | new TwigFunction('dd', [$this, 'dd']), |
32 | ]; |
33 | } |
34 | |
35 | public function dump(mixed ...$vars): string |
36 | { |
37 | ob_start(); |
38 | |
39 | foreach ($vars as $v) { |
40 | $this->dumper ? $this->dumper->dump($v) : var_dump($v); |
41 | } |
42 | |
43 | return ob_get_clean(); |
44 | } |
45 | |
46 | public function dd(mixed ...$vars): string |
47 | { |
48 | $this->flushBuffers(); |
49 | |
50 | echo call_user_func_array([$this, 'dump'], $vars); |
51 | |
52 | $this->exit(); |
53 | |
54 | return ''; |
55 | } |
56 | |
57 | public function setDumper(VarDumper $dumper): void |
58 | { |
59 | $this->dumper = $dumper; |
60 | } |
61 | |
62 | /** |
63 | * @codeCoverageIgnore |
64 | */ |
65 | protected function exit(): void |
66 | { |
67 | exit(1); |
68 | } |
69 | |
70 | /** |
71 | * @codeCoverageIgnore |
72 | */ |
73 | protected function flushBuffers(): void |
74 | { |
75 | ob_end_flush(); |
76 | } |
77 | } |