|
| 1 | +<?php namespace Barryvdh\Debugbar\Twig\Extension; |
| 2 | + |
| 3 | +use DebugBar\DataFormatter\DataFormatterInterface; |
| 4 | +use Twig_Environment; |
| 5 | +use Twig_Extension; |
| 6 | +use Twig_SimpleFunction; |
| 7 | + |
| 8 | +/** |
| 9 | + * Dump variables using the DataFormatter |
| 10 | + */ |
| 11 | +class Dump extends Twig_Extension |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \DebugBar\DataFormatter\DataFormatter |
| 15 | + */ |
| 16 | + protected $formatter; |
| 17 | + |
| 18 | + /** |
| 19 | + * Create a new auth extension. |
| 20 | + * |
| 21 | + * @param \DebugBar\DataFormatter\DataFormatterInterface $formatter |
| 22 | + */ |
| 23 | + public function __construct(DataFormatterInterface $formatter) |
| 24 | + { |
| 25 | + $this->formatter = $formatter; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * {@inheritDoc} |
| 30 | + */ |
| 31 | + public function getName() |
| 32 | + { |
| 33 | + return 'Laravel_Debugbar_Dump'; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritDoc} |
| 38 | + */ |
| 39 | + public function getFunctions() |
| 40 | + { |
| 41 | + return array( |
| 42 | + new Twig_SimpleFunction( |
| 43 | + 'dump', [$this, 'dump'], array('is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true) |
| 44 | + ), |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Based on Twig_Extension_Debug / twig_var_dump |
| 50 | + * (c) 2011 Fabien Potencier |
| 51 | + * |
| 52 | + * @param Twig_Environment $env |
| 53 | + * @param $context |
| 54 | + * |
| 55 | + * @return string |
| 56 | + */ |
| 57 | + public function dump(Twig_Environment $env, $context) |
| 58 | + { |
| 59 | + $output = ''; |
| 60 | + |
| 61 | + $count = func_num_args(); |
| 62 | + if (2 === $count) { |
| 63 | + $data = array(); |
| 64 | + foreach ($context as $key => $value) { |
| 65 | + if (is_object($value)) { |
| 66 | + if (method_exists($value, 'toArray')) { |
| 67 | + $data[$key] = $value->toArray(); |
| 68 | + } else { |
| 69 | + $data[$key] = "Object (" . get_class($value) . ")"; |
| 70 | + } |
| 71 | + } else { |
| 72 | + $data[$key] = $value; |
| 73 | + } |
| 74 | + } |
| 75 | + $output .= $this->formatter->formatVar($data); |
| 76 | + } else { |
| 77 | + for ($i = 2; $i < $count; $i++) { |
| 78 | + $output .= $this->formatter->formatVar(func_get_arg($i)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return '<pre>'.$output.'</pre>'; |
| 83 | + } |
| 84 | +} |
0 commit comments