Skip to content

Commit

Permalink
Upstream twig extensions (#1599)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Apr 1, 2024
1 parent def2fbe commit c3f7489
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 342 deletions.
88 changes: 8 additions & 80 deletions src/Twig/Extension/Debug.php
Expand Up @@ -2,98 +2,26 @@

namespace Barryvdh\Debugbar\Twig\Extension;

use DebugBar\Bridge\Twig\DebugTwigExtension;
use Illuminate\Foundation\Application;
use Twig_Environment;
use Twig_Extension;
use Twig_SimpleFunction;

/**
* Access Laravels auth class in your Twig templates.
* Access debugbar debug in your Twig templates.
*/
class Debug extends Extension
class Debug extends DebugTwigExtension
{
/**
* @var \Barryvdh\Debugbar\LaravelDebugbar
*/
protected $debugbar;

/**
* Create a new auth extension.
* Create a new debug extension.
*
* @param \Illuminate\Foundation\Application $app
*/
public function __construct(Application $app)
{
if ($app->bound('debugbar')) {
$this->debugbar = $app['debugbar'];
} else {
$this->debugbar = null;
}
}

/**
* {@inheritDoc}
*/
public function getName()
{
return 'Laravel_Debugbar_Debug';
}

/**
* {@inheritDoc}
*/
public function getFunctions()
{
// Maintain compatibility with Twig 2 and 3.
$simpleFunction = 'Twig_SimpleFunction';

if (!class_exists($simpleFunction)) {
$simpleFunction = '\Twig\TwigFunction';
}

return [
new $simpleFunction(
'debug',
[$this, 'debug'],
['needs_context' => true, 'needs_environment' => true]
),
];
}

/**
* Based on Twig_Extension_Debug / twig_var_dump
* (c) 2011 Fabien Potencier
*
* @param \Twig_Environment|\Twig\Environment $env
* @param $context
*/
public function debug($env, $context)
{
if (!$env->isDebug() || !$this->debugbar) {
return;
}

$count = func_num_args();
if (2 === $count) {
$data = [];
foreach ($context as $key => $value) {
if (is_object($value)) {
if (method_exists($value, 'toArray')) {
$data[$key] = $value->toArray();
} else {
$data[$key] = "Object (" . get_class($value) . ")";
}
} else {
$data[$key] = $value;
}
}
$this->debugbar->addMessage($data);
} else {
for ($i = 2; $i < $count; $i++) {
$this->debugbar->addMessage(func_get_arg($i));
}
$messagesCollector = null;
if ($app->bound('debugbar') && $app['debugbar']->hasCollector('messages')) {
$messagesCollector = $app['debugbar']['messages'];
}

return;
parent::__construct($messagesCollector);
}
}
84 changes: 2 additions & 82 deletions src/Twig/Extension/Dump.php
Expand Up @@ -2,91 +2,11 @@

namespace Barryvdh\Debugbar\Twig\Extension;

use DebugBar\DataFormatter\DataFormatterInterface;
use DebugBar\Bridge\Twig\DumpTwigExtension;

/**
* Dump variables using the DataFormatter
*/
class Dump extends Extension
class Dump extends DumpTwigExtension
{
/**
* @var \DebugBar\DataFormatter\DataFormatter
*/
protected $formatter;

/**
* Create a new auth extension.
*
* @param \DebugBar\DataFormatter\DataFormatterInterface $formatter
*/
public function __construct(DataFormatterInterface $formatter)
{
$this->formatter = $formatter;
}

/**
* {@inheritDoc}
*/
public function getName()
{
return 'Laravel_Debugbar_Dump';
}

/**
* {@inheritDoc}
*/
public function getFunctions()
{
// Maintain compatibility with Twig 2 and 3.
$simpleFunction = '\Twig_SimpleFunction';

if (!class_exists($simpleFunction)) {
$simpleFunction = '\Twig\TwigFunction';
}

return [
new $simpleFunction(
'dump',
[$this, 'dump'],
['is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true]
),
];
}

/**
* Based on Twig_Extension_Debug / twig_var_dump
* (c) 2011 Fabien Potencier
*
* @param \Twig_Environment|\Twig\Environment $env
* @param $context
*
* @return string
*/
public function dump($env, $context)
{
$output = '';

$count = func_num_args();
if (2 === $count) {
$data = [];
foreach ($context as $key => $value) {
if (is_object($value)) {
if (method_exists($value, 'toArray')) {
$data[$key] = $value->toArray();
} else {
$data[$key] = "Object (" . get_class($value) . ")";
}
} else {
$data[$key] = $value;
}
}
$output .= $this->formatter->formatVar($data);
} else {
for ($i = 2; $i < $count; $i++) {
$output .= $this->formatter->formatVar(func_get_arg($i));
}
}

return '<pre>' . $output . '</pre>';
}
}
31 changes: 12 additions & 19 deletions src/Twig/Extension/Stopwatch.php
Expand Up @@ -2,52 +2,45 @@

namespace Barryvdh\Debugbar\Twig\Extension;

use Barryvdh\Debugbar\Twig\TokenParser\StopwatchTokenParser;
use DebugBar\Bridge\Twig\MeasureTwigExtension;
use Illuminate\Foundation\Application;

/**
* Access Laravels auth class in your Twig templates.
* Access debugbar time measures in your Twig templates.
* Based on Symfony\Bridge\Twig\Extension\StopwatchExtension
*/
class Stopwatch extends Extension
class Stopwatch extends MeasureTwigExtension
{
/**
* @var \Barryvdh\Debugbar\LaravelDebugbar
*/
protected $debugbar;

/**
* Create a new auth extension.
* Create a new time measure extension.
*
* @param \Illuminate\Foundation\Application $app
*/
public function __construct(Application $app)
{
$timeCollector = null;
if ($app->bound('debugbar')) {
$this->debugbar = $app['debugbar'];
} else {
$this->debugbar = null;

if ($app['debugbar']->hasCollector('time')) {
$timeCollector = $app['debugbar']['time'];
}
}

parent::__construct($timeCollector, 'stopwatch');
}

/**
* {@inheritDoc}
*/
public function getName()
{
return 'stopwatch';
}

public function getTokenParsers()
{
return [
/*
* {% stopwatch foo %}
* Some stuff which will be recorded on the timeline
* {% endstopwatch %}
*/
new StopwatchTokenParser($this->debugbar !== null),
];
return static::class;
}

public function getDebugbar()
Expand Down
14 changes: 0 additions & 14 deletions src/Twig/Node/Node.php

This file was deleted.

56 changes: 0 additions & 56 deletions src/Twig/Node/StopwatchNode.php

This file was deleted.

0 comments on commit c3f7489

Please sign in to comment.