[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Extension/ -> DumpExtension.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Bridge\Twig\Extension;
  13  
  14  use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
  15  use Symfony\Component\VarDumper\Cloner\ClonerInterface;
  16  use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  17  use Twig\Environment;
  18  use Twig\Extension\AbstractExtension;
  19  use Twig\Template;
  20  use Twig\TwigFunction;
  21  
  22  /**
  23   * Provides integration of the dump() function with Twig.
  24   *
  25   * @author Nicolas Grekas <p@tchwork.com>
  26   */
  27  class DumpExtension extends AbstractExtension
  28  {
  29      private $cloner;
  30      private $dumper;
  31  
  32      public function __construct(ClonerInterface $cloner, HtmlDumper $dumper = null)
  33      {
  34          $this->cloner = $cloner;
  35          $this->dumper = $dumper;
  36      }
  37  
  38      public function getFunctions()
  39      {
  40          return [
  41              new TwigFunction('dump', [$this, 'dump'], ['is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true]),
  42          ];
  43      }
  44  
  45      public function getTokenParsers()
  46      {
  47          return [new DumpTokenParser()];
  48      }
  49  
  50      public function getName()
  51      {
  52          return 'dump';
  53      }
  54  
  55      public function dump(Environment $env, $context)
  56      {
  57          if (!$env->isDebug()) {
  58              return null;
  59          }
  60  
  61          if (2 === \func_num_args()) {
  62              $vars = [];
  63              foreach ($context as $key => $value) {
  64                  if (!$value instanceof Template) {
  65                      $vars[$key] = $value;
  66                  }
  67              }
  68  
  69              $vars = [$vars];
  70          } else {
  71              $vars = \func_get_args();
  72              unset($vars[0], $vars[1]);
  73          }
  74  
  75          $dump = fopen('php://memory', 'r+b');
  76          $this->dumper = $this->dumper ?: new HtmlDumper();
  77          $this->dumper->setCharset($env->getCharset());
  78  
  79          foreach ($vars as $value) {
  80              $this->dumper->dump($this->cloner->cloneVar($value), $dump);
  81          }
  82  
  83          return stream_get_contents($dump, -1, 0);
  84      }
  85  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1