Skip to content

Commit a3b1f74

Browse files
committed
Split Debug/Dump extensions
So other dump functions could be used, and a difference between dumping and passing to the debugbar can be made.
1 parent a8607cf commit a3b1f74

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,14 @@ Laravel Debugbar comes with two Twig Extensions. These are tested with [rcrowe/T
128128
Add the following extensions to your TwigBridge config/extensions.php (or register the extensions manually)
129129

130130
'Barryvdh\Debugbar\Twig\Extension\Debug',
131+
'Barryvdh\Debugbar\Twig\Extension\Dump',
131132
'Barryvdh\Debugbar\Twig\Extension\Stopwatch',
132133

133-
The Debug extension will replace the [dump function](http://twig.sensiolabs.org/doc/functions/dump.html) to output variables to the Messages,
134+
The Dump extension will replace the [dump function](http://twig.sensiolabs.org/doc/functions/dump.html) to output variables using the DataFormatter. The Debug extension adds a `debug()` function which passes variables to the Message Collector,
134135
instead of showing it directly in the template. It dumps the arguments, or when empty; all context variables.
135136

136-
{{ dump() }}
137-
{{ dump(user, categories) }}
137+
{{ debug() }}
138+
{{ debug(user, categories) }}
138139

139140
The Stopwatch extension adds a [stopwatch tag](http://symfony.com/blog/new-in-symfony-2-4-a-stopwatch-tag-for-twig) similar to the one in Symfony/Silex Twigbridge.
140141

src/Twig/Extension/Debug.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ public function getFunctions()
4444
{
4545
return array(
4646
new Twig_SimpleFunction(
47-
'dump', [$this, 'dump'], array('needs_context' => true, 'needs_environment' => true)
48-
),
49-
new Twig_SimpleFunction(
50-
'debug', [$this, 'dump'], array('needs_context' => true, 'needs_environment' => true)
47+
'debug', [$this, 'debug'], array('needs_context' => true, 'needs_environment' => true)
5148
),
5249
);
5350
}
@@ -59,7 +56,7 @@ public function getFunctions()
5956
* @param Twig_Environment $env
6057
* @param $context
6158
*/
62-
public function dump(Twig_Environment $env, $context)
59+
public function debug(Twig_Environment $env, $context)
6360
{
6461
if (!$env->isDebug() || !$this->debugbar) {
6562
return;

src/Twig/Extension/Dump.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)