Skip to content

Commit c3f7489

Browse files
authored
Upstream twig extensions (#1599)
1 parent def2fbe commit c3f7489

File tree

7 files changed

+22
-342
lines changed

7 files changed

+22
-342
lines changed

src/Twig/Extension/Debug.php

Lines changed: 8 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,26 @@
22

33
namespace Barryvdh\Debugbar\Twig\Extension;
44

5+
use DebugBar\Bridge\Twig\DebugTwigExtension;
56
use Illuminate\Foundation\Application;
6-
use Twig_Environment;
7-
use Twig_Extension;
8-
use Twig_SimpleFunction;
97

108
/**
11-
* Access Laravels auth class in your Twig templates.
9+
* Access debugbar debug in your Twig templates.
1210
*/
13-
class Debug extends Extension
11+
class Debug extends DebugTwigExtension
1412
{
1513
/**
16-
* @var \Barryvdh\Debugbar\LaravelDebugbar
17-
*/
18-
protected $debugbar;
19-
20-
/**
21-
* Create a new auth extension.
14+
* Create a new debug extension.
2215
*
2316
* @param \Illuminate\Foundation\Application $app
2417
*/
2518
public function __construct(Application $app)
2619
{
27-
if ($app->bound('debugbar')) {
28-
$this->debugbar = $app['debugbar'];
29-
} else {
30-
$this->debugbar = null;
31-
}
32-
}
33-
34-
/**
35-
* {@inheritDoc}
36-
*/
37-
public function getName()
38-
{
39-
return 'Laravel_Debugbar_Debug';
40-
}
41-
42-
/**
43-
* {@inheritDoc}
44-
*/
45-
public function getFunctions()
46-
{
47-
// Maintain compatibility with Twig 2 and 3.
48-
$simpleFunction = 'Twig_SimpleFunction';
49-
50-
if (!class_exists($simpleFunction)) {
51-
$simpleFunction = '\Twig\TwigFunction';
52-
}
53-
54-
return [
55-
new $simpleFunction(
56-
'debug',
57-
[$this, 'debug'],
58-
['needs_context' => true, 'needs_environment' => true]
59-
),
60-
];
61-
}
62-
63-
/**
64-
* Based on Twig_Extension_Debug / twig_var_dump
65-
* (c) 2011 Fabien Potencier
66-
*
67-
* @param \Twig_Environment|\Twig\Environment $env
68-
* @param $context
69-
*/
70-
public function debug($env, $context)
71-
{
72-
if (!$env->isDebug() || !$this->debugbar) {
73-
return;
74-
}
75-
76-
$count = func_num_args();
77-
if (2 === $count) {
78-
$data = [];
79-
foreach ($context as $key => $value) {
80-
if (is_object($value)) {
81-
if (method_exists($value, 'toArray')) {
82-
$data[$key] = $value->toArray();
83-
} else {
84-
$data[$key] = "Object (" . get_class($value) . ")";
85-
}
86-
} else {
87-
$data[$key] = $value;
88-
}
89-
}
90-
$this->debugbar->addMessage($data);
91-
} else {
92-
for ($i = 2; $i < $count; $i++) {
93-
$this->debugbar->addMessage(func_get_arg($i));
94-
}
20+
$messagesCollector = null;
21+
if ($app->bound('debugbar') && $app['debugbar']->hasCollector('messages')) {
22+
$messagesCollector = $app['debugbar']['messages'];
9523
}
9624

97-
return;
25+
parent::__construct($messagesCollector);
9826
}
9927
}

src/Twig/Extension/Dump.php

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,11 @@
22

33
namespace Barryvdh\Debugbar\Twig\Extension;
44

5-
use DebugBar\DataFormatter\DataFormatterInterface;
5+
use DebugBar\Bridge\Twig\DumpTwigExtension;
66

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

src/Twig/Extension/Stopwatch.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,45 @@
22

33
namespace Barryvdh\Debugbar\Twig\Extension;
44

5-
use Barryvdh\Debugbar\Twig\TokenParser\StopwatchTokenParser;
5+
use DebugBar\Bridge\Twig\MeasureTwigExtension;
66
use Illuminate\Foundation\Application;
77

88
/**
9-
* Access Laravels auth class in your Twig templates.
9+
* Access debugbar time measures in your Twig templates.
1010
* Based on Symfony\Bridge\Twig\Extension\StopwatchExtension
1111
*/
12-
class Stopwatch extends Extension
12+
class Stopwatch extends MeasureTwigExtension
1313
{
1414
/**
1515
* @var \Barryvdh\Debugbar\LaravelDebugbar
1616
*/
1717
protected $debugbar;
1818

1919
/**
20-
* Create a new auth extension.
20+
* Create a new time measure extension.
2121
*
2222
* @param \Illuminate\Foundation\Application $app
2323
*/
2424
public function __construct(Application $app)
2525
{
26+
$timeCollector = null;
2627
if ($app->bound('debugbar')) {
2728
$this->debugbar = $app['debugbar'];
28-
} else {
29-
$this->debugbar = null;
29+
30+
if ($app['debugbar']->hasCollector('time')) {
31+
$timeCollector = $app['debugbar']['time'];
32+
}
3033
}
34+
35+
parent::__construct($timeCollector, 'stopwatch');
3136
}
3237

3338
/**
3439
* {@inheritDoc}
3540
*/
3641
public function getName()
3742
{
38-
return 'stopwatch';
39-
}
40-
41-
public function getTokenParsers()
42-
{
43-
return [
44-
/*
45-
* {% stopwatch foo %}
46-
* Some stuff which will be recorded on the timeline
47-
* {% endstopwatch %}
48-
*/
49-
new StopwatchTokenParser($this->debugbar !== null),
50-
];
43+
return static::class;
5144
}
5245

5346
public function getDebugbar()

src/Twig/Node/Node.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Twig/Node/StopwatchNode.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)