vendor and env first commit

This commit is contained in:
2025-03-28 08:52:46 +01:00
parent f8388bc81b
commit 8f26283832
10976 changed files with 1349952 additions and 2 deletions
@@ -0,0 +1,94 @@
<?php
namespace DebugBar\Bridge\Twig;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Debug messages to debugbar in your Twig templates.
*
* @package DebugBar\Bridge\Twig
*/
class DebugTwigExtension extends AbstractExtension
{
/**
* @var \DebugBar\DataCollector\MessagesCollector|null
*/
protected $messagesCollector;
/**
* @var string
*/
protected $functionName;
/**
*
* @param \DebugBar\DataCollector\MessagesCollector|null $app
* @param string $functionName
*/
public function __construct($messagesCollector, $functionName = 'debug')
{
$this->messagesCollector = $messagesCollector;
$this->functionName = $functionName;
}
/**
* {@inheritDoc}
*/
public function getName()
{
return static::class;
}
/**
* {@inheritDoc}
*/
public function getFunctions()
{
return [
new TwigFunction(
$this->functionName,
[$this, 'debug'],
['needs_context' => true, 'needs_environment' => true]
),
];
}
/**
* Based on Twig_Extension_Debug / twig_var_dump
*
* @param Environment $env
* @param $context
*/
public function debug(Environment $env, $context)
{
if (!$env->isDebug() || !$this->messagesCollector) {
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->messagesCollector->addMessage($data, 'debug');
} else {
for ($i = 2; $i < $count; $i++) {
$this->messagesCollector->addMessage(func_get_arg($i), 'debug');
}
}
return;
}
}
@@ -0,0 +1,99 @@
<?php
namespace DebugBar\Bridge\Twig;
use DebugBar\DataFormatter\HasDataFormatter;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Dump variables using debugbar DataFormatter
*
* @package DebugBar\Bridge\Twig
*/
class DumpTwigExtension extends AbstractExtension
{
use HasDataFormatter;
/**
* @var string
*/
protected $functionName;
/**
* Create a new auth extension.
*
* @param string $functionName
*/
public function __construct($functionName = 'dump')
{
$this->functionName = $functionName;
}
/**
* {@inheritDoc}
*/
public function getName()
{
return static::class;
}
/**
* {@inheritDoc}
*/
public function getFunctions()
{
return [
new TwigFunction(
$this->functionName,
[$this, 'dump'],
['is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true]
),
];
}
/**
* Based on Twig_Extension_Debug / twig_var_dump
*
* @param Environment $env
* @param $context
*
* @return string
*/
public function dump(Environment $env, $context)
{
if (!$env->isDebug()) {
return;
}
$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->formatVar($data);
} else {
for ($i = 2; $i < $count; $i++) {
$output .= $this->formatVar(func_get_arg($i));
}
}
if ($this->isHtmlVarDumperUsed()) {
return $output;
}
return '<pre>' . $output . '</pre>';
}
}
@@ -0,0 +1,77 @@
<?php
namespace DebugBar\Bridge\Twig;
use Twig\Extension\AbstractExtension;
/**
* Access debugbar timeline measure in your Twig templates.
* Based on Symfony\Bridge\Twig\Extension\StopwatchExtension
*
* @package DebugBar\Bridge\Twig
*/
class MeasureTwigExtension extends AbstractExtension
{
/**
* @var \DebugBar\DataCollector\TimeDataCollector|null
*/
protected $timeCollector;
/**
* @var string
*/
protected $tagName;
/**
* Create a new auth extension.
*
* @param \DebugBar\DataCollector\TimeDataCollector|null $debugbar
* @param string $tagName
*/
public function __construct($timeCollector, $tagName = 'measure')
{
$this->timeCollector = $timeCollector;
$this->tagName = $tagName;
}
/**
* {@inheritDoc}
*/
public function getName()
{
return static::class;
}
/**
* @return \Twig\TokenParser\TokenParserInterface[]
*/
public function getTokenParsers()
{
return [
/*
* {% measure foo %}
* Some stuff which will be recorded on the timeline
* {% endmeasure %}
*/
new MeasureTwigTokenParser(!is_null($this->timeCollector), $this->tagName, $this->getName()),
];
}
public function startMeasure(...$arg)
{
if (!$this->timeCollector) {
return;
}
$this->timeCollector->startMeasure(...$arg);
}
public function stopMeasure(...$arg)
{
if (!$this->timeCollector) {
return;
}
$this->timeCollector->stopMeasure(...$arg);
}
}
@@ -0,0 +1,50 @@
<?php
namespace DebugBar\Bridge\Twig;
use DebugBar\Bridge\Twig\MeasureTwigExtension;
use Twig\Compiler;
use Twig\Node\Expression\AssignNameExpression;
use Twig\Node\Node;
/**
* Represents a measure node.
* Based on Symfony\Bridge\Twig\Node\StopwatchNode
*/
class MeasureTwigNode extends Node
{
/**
* @var string
*/
protected $extName;
public function __construct(
Node $name,
$body,
AssignNameExpression $var,
$lineno = 0,
$tag = null,
$extName = null
) {
parent::__construct(['body' => $body, 'name' => $name, 'var' => $var], [], $lineno, $tag);
$this->extName = $extName ?: MeasureTwigExtension::class;
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write('')
->subcompile($this->getNode('var'))
->raw(' = ')
->subcompile($this->getNode('name'))
->write(";\n")
->write("\$this->env->getExtension('".$this->extName."')->startMeasure(")
->subcompile($this->getNode('var'))
->raw(");\n")
->subcompile($this->getNode('body'))
->write("\$this->env->getExtension('".$this->extName."')->stopMeasure(")
->subcompile($this->getNode('var'))
->raw(");\n");
}
}
@@ -0,0 +1,78 @@
<?php
namespace DebugBar\Bridge\Twig;
use Twig\Node\Expression\AssignNameExpression;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;
/**
* Token Parser for the measure tag.
* Based on Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser;
*/
class MeasureTwigTokenParser extends AbstractTokenParser
{
/**
* @var string
*/
private $extName;
/**
* @var string
*/
private $tagName;
/**
* @var bool
*/
private $enabled;
/**
*
* @param string $tagName
*/
public function __construct($enabled, $tagName, $extName = null)
{
$this->enabled = $enabled;
$this->tagName = $tagName;
$this->extName = $extName;
}
public function parse(Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
// {% measure 'bar' %}
$name = $this->parser->getExpressionParser()->parseExpression();
$stream->expect(Token::BLOCK_END_TYPE);
// {% endmeasure %}
$body = $this->parser->subparse([$this, 'decideMeasureEnd'], true);
$stream->expect(Token::BLOCK_END_TYPE);
if ($this->enabled) {
return new MeasureTwigNode(
$name,
$body,
new AssignNameExpression($this->parser->getVarName(), $token->getLine()),
$lineno,
$this->getTag(),
$this->extName
);
}
return $body;
}
public function getTag()
{
return $this->tagName;
}
public function decideMeasureEnd(Token $token)
{
return $token->test('end'.$this->getTag());
}
}
@@ -0,0 +1,61 @@
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2017 Tim Riemenschneider
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar\Bridge\Twig;
use DebugBar\DataCollector\TimeDataCollector;
use Twig\Extension\ProfilerExtension;
use Twig\Profiler\Profile;
/**
* Class TimeableTwigExtensionProfiler
*
* Extends ProfilerExtension to add rendering times to the TimeDataCollector
*
* @package DebugBar\Bridge\Twig
*/
class TimeableTwigExtensionProfiler extends ProfilerExtension
{
/**
* @var \DebugBar\DataCollector\TimeDataCollector
*/
private $timeDataCollector;
/**
* @param \DebugBar\DataCollector\TimeDataCollector $timeDataCollector
*/
public function setTimeDataCollector(TimeDataCollector $timeDataCollector)
{
$this->timeDataCollector = $timeDataCollector;
}
public function __construct(Profile $profile, TimeDataCollector $timeDataCollector = null)
{
parent::__construct($profile);
$this->timeDataCollector = $timeDataCollector;
}
public function enter(Profile $profile)
{
if ($this->timeDataCollector && $profile->isTemplate()) {
$this->timeDataCollector->startMeasure($profile->getName(), 'template ' . $profile->getName());
}
parent::enter($profile);
}
public function leave(Profile $profile)
{
parent::leave($profile);
if ($this->timeDataCollector && $profile->isTemplate()) {
$this->timeDataCollector->stopMeasure($profile->getName());
}
}
}
@@ -0,0 +1,419 @@
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar\Bridge\Twig;
use DebugBar\DataCollector\TimeDataCollector;
use Twig_CompilerInterface;
use Twig_Environment;
use Twig_ExtensionInterface;
use Twig_LexerInterface;
use Twig_LoaderInterface;
use Twig_NodeInterface;
use Twig_NodeVisitorInterface;
use Twig_ParserInterface;
use Twig_TokenParserInterface;
use Twig_TokenStream;
/**
* Wrapped a Twig Environment to provide profiling features
*
* @deprecated
*/
class TraceableTwigEnvironment extends Twig_Environment
{
protected $twig;
protected $renderedTemplates = array();
protected $timeDataCollector;
/**
* @param Twig_Environment $twig
* @param TimeDataCollector $timeDataCollector
*/
public function __construct(Twig_Environment $twig, TimeDataCollector $timeDataCollector = null)
{
$this->twig = $twig;
$this->timeDataCollector = $timeDataCollector;
}
public function __call($name, $arguments)
{
return call_user_func_array(array($this->twig, $name), $arguments);
}
public function getRenderedTemplates()
{
return $this->renderedTemplates;
}
public function addRenderedTemplate(array $info)
{
$this->renderedTemplates[] = $info;
}
public function getTimeDataCollector()
{
return $this->timeDataCollector;
}
public function getBaseTemplateClass()
{
return $this->twig->getBaseTemplateClass();
}
public function setBaseTemplateClass($class)
{
$this->twig->setBaseTemplateClass($class);
}
public function enableDebug()
{
$this->twig->enableDebug();
}
public function disableDebug()
{
$this->twig->disableDebug();
}
public function isDebug()
{
return $this->twig->isDebug();
}
public function enableAutoReload()
{
$this->twig->enableAutoReload();
}
public function disableAutoReload()
{
$this->twig->disableAutoReload();
}
public function isAutoReload()
{
return $this->twig->isAutoReload();
}
public function enableStrictVariables()
{
$this->twig->enableStrictVariables();
}
public function disableStrictVariables()
{
$this->twig->disableStrictVariables();
}
public function isStrictVariables()
{
return $this->twig->isStrictVariables();
}
public function getCache($original = true)
{
return $this->twig->getCache($original);
}
public function setCache($cache)
{
$this->twig->setCache($cache);
}
public function getCacheFilename($name)
{
return $this->twig->getCacheFilename($name);
}
public function getTemplateClass($name, $index = null)
{
return $this->twig->getTemplateClass($name, $index);
}
public function getTemplateClassPrefix()
{
return $this->twig->getTemplateClassPrefix();
}
public function render($name, array $context = array())
{
return $this->loadTemplate($name)->render($context);
}
public function display($name, array $context = array())
{
$this->loadTemplate($name)->display($context);
}
public function loadTemplate($name, $index = null)
{
$cls = $this->twig->getTemplateClass($name, $index);
if (isset($this->twig->loadedTemplates[$cls])) {
return $this->twig->loadedTemplates[$cls];
}
if (!class_exists($cls, false)) {
if (false === $cache = $this->getCacheFilename($name)) {
eval('?>'.$this->compileSource($this->getLoader()->getSource($name), $name));
} else {
if (!is_file($cache) || ($this->isAutoReload() && !$this->isTemplateFresh($name, filemtime($cache)))) {
$this->writeCacheFile($cache, $this->compileSource($this->getLoader()->getSource($name), $name));
}
require_once $cache;
}
}
if (!$this->twig->runtimeInitialized) {
$this->initRuntime();
}
return $this->twig->loadedTemplates[$cls] = new TraceableTwigTemplate($this, new $cls($this));
}
public function isTemplateFresh($name, $time)
{
return $this->twig->isTemplateFresh($name, $time);
}
public function resolveTemplate($names)
{
return $this->twig->resolveTemplate($names);
}
public function clearTemplateCache()
{
$this->twig->clearTemplateCache();
}
public function clearCacheFiles()
{
$this->twig->clearCacheFiles();
}
public function getLexer()
{
return $this->twig->getLexer();
}
public function setLexer(Twig_LexerInterface $lexer)
{
$this->twig->setLexer($lexer);
}
public function tokenize($source, $name = null)
{
return $this->twig->tokenize($source, $name);
}
public function getParser()
{
return $this->twig->getParser();
}
public function setParser(Twig_ParserInterface $parser)
{
$this->twig->setParser($parser);
}
public function parse(Twig_TokenStream $tokens)
{
return $this->twig->parse($tokens);
}
public function getCompiler()
{
return $this->twig->getCompiler();
}
public function setCompiler(Twig_CompilerInterface $compiler)
{
$this->twig->setCompiler($compiler);
}
public function compile(Twig_NodeInterface $node)
{
return $this->twig->compile($node);
}
public function compileSource($source, $name = null)
{
return $this->twig->compileSource($source, $name);
}
public function setLoader(Twig_LoaderInterface $loader)
{
$this->twig->setLoader($loader);
}
public function getLoader()
{
return $this->twig->getLoader();
}
public function setCharset($charset)
{
$this->twig->setCharset($charset);
}
public function getCharset()
{
return $this->twig->getCharset();
}
public function initRuntime()
{
$this->twig->initRuntime();
}
public function hasExtension($name)
{
return $this->twig->hasExtension($name);
}
public function getExtension($name)
{
return $this->twig->getExtension($name);
}
public function addExtension(Twig_ExtensionInterface $extension)
{
$this->twig->addExtension($extension);
}
public function removeExtension($name)
{
$this->twig->removeExtension($name);
}
public function setExtensions(array $extensions)
{
$this->twig->setExtensions($extensions);
}
public function getExtensions()
{
return $this->twig->getExtensions();
}
public function addTokenParser(Twig_TokenParserInterface $parser)
{
$this->twig->addTokenParser($parser);
}
public function getTokenParsers()
{
return $this->twig->getTokenParsers();
}
public function getTags()
{
return $this->twig->getTags();
}
public function addNodeVisitor(Twig_NodeVisitorInterface $visitor)
{
$this->twig->addNodeVisitor($visitor);
}
public function getNodeVisitors()
{
return $this->twig->getNodeVisitors();
}
public function addFilter($name, $filter = null)
{
$this->twig->addFilter($name, $filter);
}
public function getFilter($name)
{
return $this->twig->getFilter($name);
}
public function registerUndefinedFilterCallback($callable)
{
$this->twig->registerUndefinedFilterCallback($callable);
}
public function getFilters()
{
return $this->twig->getFilters();
}
public function addTest($name, $test = null)
{
$this->twig->addTest($name, $test);
}
public function getTests()
{
return $this->twig->getTests();
}
public function getTest($name)
{
return $this->twig->getTest($name);
}
public function addFunction($name, $function = null)
{
$this->twig->addFunction($name, $function);
}
public function getFunction($name)
{
return $this->twig->getFunction($name);
}
public function registerUndefinedFunctionCallback($callable)
{
$this->twig->registerUndefinedFunctionCallback($callable);
}
public function getFunctions()
{
return $this->twig->getFunctions();
}
public function addGlobal($name, $value)
{
$this->twig->addGlobal($name, $value);
}
public function getGlobals()
{
return $this->twig->getGlobals();
}
public function mergeGlobals(array $context)
{
return $this->twig->mergeGlobals($context);
}
public function getUnaryOperators()
{
return $this->twig->getUnaryOperators();
}
public function getBinaryOperators()
{
return $this->twig->getBinaryOperators();
}
public function computeAlternatives($name, $items)
{
return $this->twig->computeAlternatives($name, $items);
}
}
@@ -0,0 +1,138 @@
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar\Bridge\Twig;
use Twig_Template;
use Twig_TemplateInterface;
/**
* Wraps a Twig_Template to add profiling features
*
* @deprecated
*/
class TraceableTwigTemplate extends Twig_Template implements Twig_TemplateInterface
{
protected $template;
/**
* @param TraceableTwigEnvironment $env
* @param Twig_Template $template
*/
public function __construct(TraceableTwigEnvironment $env, Twig_Template $template)
{
$this->env = $env;
$this->template = $template;
}
public function __call($name, $arguments)
{
return call_user_func_array(array($this->template, $name), $arguments);
}
public function doDisplay(array $context, array $blocks = array())
{
return $this->template->doDisplay($context, $blocks);
}
public function getTemplateName()
{
return $this->template->getTemplateName();
}
public function getEnvironment()
{
return $this->template->getEnvironment();
}
public function getParent(array $context)
{
return $this->template->getParent($context);
}
public function isTraitable()
{
return $this->template->isTraitable();
}
public function displayParentBlock($name, array $context, array $blocks = array())
{
$this->template->displayParentBlock($name, $context, $blocks);
}
public function displayBlock($name, array $context, array $blocks = array(), $useBlocks = true)
{
$this->template->displayBlock($name, $context, $blocks, $useBlocks);
}
public function renderParentBlock($name, array $context, array $blocks = array())
{
return $this->template->renderParentBlock($name, $context, $blocks);
}
public function renderBlock($name, array $context, array $blocks = array(), $useBlocks = true)
{
return $this->template->renderBlock($name, $context, $blocks, $useBlocks);
}
public function hasBlock($name)
{
return $this->template->hasBlock($name);
}
public function getBlockNames()
{
return $this->template->getBlockNames();
}
public function getBlocks()
{
return $this->template->getBlocks();
}
public function display(array $context, array $blocks = array())
{
$start = microtime(true);
$this->template->display($context, $blocks);
$end = microtime(true);
if ($timeDataCollector = $this->env->getTimeDataCollector()) {
$name = sprintf("twig.render(%s)", $this->template->getTemplateName());
$timeDataCollector->addMeasure($name, $start, $end);
}
$this->env->addRenderedTemplate(array(
'name' => $this->template->getTemplateName(),
'render_time' => $end - $start
));
}
public function render(array $context)
{
$level = ob_get_level();
ob_start();
try {
$this->display($context);
} catch (Exception $e) {
while (ob_get_level() > $level) {
ob_end_clean();
}
throw $e;
}
return ob_get_clean();
}
public static function clearCache()
{
Twig_Template::clearCache();
}
}
@@ -0,0 +1,89 @@
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar\Bridge\Twig;
use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
/**
* Collects data about rendered templates
*
* http://twig.sensiolabs.org/
*
* Your Twig_Environment object needs to be wrapped in a
* TraceableTwigEnvironment object
*
* <code>
* $env = new TraceableTwigEnvironment(new Twig_Environment($loader));
* $debugbar->addCollector(new TwigCollector($env));
* </code>
*
* @deprecated use DebugBar\Bridge\TwigProfileCollector instead
*/
class TwigCollector extends DataCollector implements Renderable, AssetProvider
{
public function __construct(TraceableTwigEnvironment $twig)
{
$this->twig = $twig;
}
public function collect()
{
$templates = array();
$accuRenderTime = 0;
foreach ($this->twig->getRenderedTemplates() as $tpl) {
$accuRenderTime += $tpl['render_time'];
$templates[] = array(
'name' => $tpl['name'],
'render_time' => $tpl['render_time'],
'render_time_str' => $this->formatDuration($tpl['render_time'])
);
}
return array(
'nb_templates' => count($templates),
'templates' => $templates,
'accumulated_render_time' => $accuRenderTime,
'accumulated_render_time_str' => $this->formatDuration($accuRenderTime)
);
}
public function getName()
{
return 'twig';
}
public function getWidgets()
{
return array(
'twig' => array(
'icon' => 'leaf',
'widget' => 'PhpDebugBar.Widgets.TemplatesWidget',
'map' => 'twig',
'default' => json_encode(array('templates' => array())),
),
'twig:badge' => array(
'map' => 'twig.nb_templates',
'default' => 0
)
);
}
public function getAssets()
{
return array(
'css' => 'widgets/templates/widget.css',
'js' => 'widgets/templates/widget.js'
);
}
}