1: <?php
2: namespace Yep\TracyTwigExtensions;
3:
4: use Tracy\Dumper;
5:
6: class DumpExtension extends \Twig_Extension {
7: protected $name = 'dump';
8: protected $options;
9:
10: public function __construct(array $options = []) {
11: $this->options = $options;
12: }
13:
14: public function getFunctions() {
15: return [
16: new \Twig_SimpleFunction($this->name, [$this, 'dump'], ['is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true]),
17: ];
18: }
19:
20: public function getName() {
21: return $this->name;
22: }
23:
24: public function dump(\Twig_Environment $environment, $context) {
25: if (!$environment->isDebug()) {
26: return '';
27: }
28:
29: $arguments = func_get_args();
30: array_shift($arguments);
31: array_shift($arguments);
32: $count = count($arguments);
33:
34: if ($count === 0) {
35: $arguments = $context;
36: }
37:
38: if ($count === 1) {
39: $arguments = array_shift($arguments);
40: }
41:
42: return $this->doDump($arguments);
43: }
44:
45: protected function doDump($data) {
46: if (!class_exists('\Tracy\Dumper')) {
47: return '';
48: }
49:
50: ob_start();
51: Dumper::dump($data, $this->options);
52:
53: return ob_get_clean();
54: }
55: }
56: