[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Command/ -> DebugCommand.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\Command;
  13  
  14  use Symfony\Component\Console\Command\Command;
  15  use Symfony\Component\Console\Input\InputArgument;
  16  use Symfony\Component\Console\Input\InputInterface;
  17  use Symfony\Component\Console\Input\InputOption;
  18  use Symfony\Component\Console\Output\OutputInterface;
  19  use Symfony\Component\Console\Style\SymfonyStyle;
  20  use Twig\Environment;
  21  
  22  /**
  23   * Lists twig functions, filters, globals and tests present in the current project.
  24   *
  25   * @author Jordi Boggiano <j.boggiano@seld.be>
  26   */
  27  class DebugCommand extends Command
  28  {
  29      private $twig;
  30  
  31      /**
  32       * {@inheritdoc}
  33       */
  34      public function __construct($name = 'debug:twig')
  35      {
  36          parent::__construct($name);
  37      }
  38  
  39      public function setTwigEnvironment(Environment $twig)
  40      {
  41          $this->twig = $twig;
  42      }
  43  
  44      /**
  45       * @return Environment $twig
  46       */
  47      protected function getTwigEnvironment()
  48      {
  49          return $this->twig;
  50      }
  51  
  52      protected function configure()
  53      {
  54          $this
  55              ->setDefinition(array(
  56                  new InputArgument('filter', InputArgument::OPTIONAL, 'Show details for all entries matching this filter'),
  57                  new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (text or json)', 'text'),
  58              ))
  59              ->setDescription('Shows a list of twig functions, filters, globals and tests')
  60              ->setHelp(<<<'EOF'
  61  The <info>%command.name%</info> command outputs a list of twig functions,
  62  filters, globals and tests. Output can be filtered with an optional argument.
  63  
  64    <info>php %command.full_name%</info>
  65  
  66  The command lists all functions, filters, etc.
  67  
  68    <info>php %command.full_name% date</info>
  69  
  70  The command lists everything that contains the word date.
  71  
  72    <info>php %command.full_name% --format=json</info>
  73  
  74  The command lists everything in a machine readable json format.
  75  EOF
  76              )
  77          ;
  78      }
  79  
  80      protected function execute(InputInterface $input, OutputInterface $output)
  81      {
  82          $io = new SymfonyStyle($input, $output);
  83          $twig = $this->getTwigEnvironment();
  84  
  85          if (null === $twig) {
  86              $io->error('The Twig environment needs to be set.');
  87  
  88              return 1;
  89          }
  90  
  91          $types = array('functions', 'filters', 'tests', 'globals');
  92  
  93          if ('json' === $input->getOption('format')) {
  94              $data = array();
  95              foreach ($types as $type) {
  96                  foreach ($twig->{'get'.ucfirst($type)}() as $name => $entity) {
  97                      $data[$type][$name] = $this->getMetadata($type, $entity);
  98                  }
  99              }
 100              $data['tests'] = array_keys($data['tests']);
 101              $io->writeln(json_encode($data));
 102  
 103              return 0;
 104          }
 105  
 106          $filter = $input->getArgument('filter');
 107  
 108          foreach ($types as $index => $type) {
 109              $items = array();
 110              foreach ($twig->{'get'.ucfirst($type)}() as $name => $entity) {
 111                  if (!$filter || false !== strpos($name, $filter)) {
 112                      $items[$name] = $name.$this->getPrettyMetadata($type, $entity);
 113                  }
 114              }
 115  
 116              if (!$items) {
 117                  continue;
 118              }
 119  
 120              $io->section(ucfirst($type));
 121  
 122              ksort($items);
 123              $io->listing($items);
 124          }
 125  
 126          return 0;
 127      }
 128  
 129      private function getMetadata($type, $entity)
 130      {
 131          if ('globals' === $type) {
 132              return $entity;
 133          }
 134          if ('tests' === $type) {
 135              return;
 136          }
 137          if ('functions' === $type || 'filters' === $type) {
 138              $cb = $entity->getCallable();
 139              if (null === $cb) {
 140                  return;
 141              }
 142              if (\is_array($cb)) {
 143                  if (!method_exists($cb[0], $cb[1])) {
 144                      return;
 145                  }
 146                  $refl = new \ReflectionMethod($cb[0], $cb[1]);
 147              } elseif (\is_object($cb) && method_exists($cb, '__invoke')) {
 148                  $refl = new \ReflectionMethod($cb, '__invoke');
 149              } elseif (\function_exists($cb)) {
 150                  $refl = new \ReflectionFunction($cb);
 151              } elseif (\is_string($cb) && preg_match('{^(.+)::(.+)$}', $cb, $m) && method_exists($m[1], $m[2])) {
 152                  $refl = new \ReflectionMethod($m[1], $m[2]);
 153              } else {
 154                  throw new \UnexpectedValueException('Unsupported callback type');
 155              }
 156  
 157              $args = $refl->getParameters();
 158  
 159              // filter out context/environment args
 160              if ($entity->needsEnvironment()) {
 161                  array_shift($args);
 162              }
 163              if ($entity->needsContext()) {
 164                  array_shift($args);
 165              }
 166  
 167              if ('filters' === $type) {
 168                  // remove the value the filter is applied on
 169                  array_shift($args);
 170              }
 171  
 172              // format args
 173              $args = array_map(function ($param) {
 174                  if ($param->isDefaultValueAvailable()) {
 175                      return $param->getName().' = '.json_encode($param->getDefaultValue());
 176                  }
 177  
 178                  return $param->getName();
 179              }, $args);
 180  
 181              return $args;
 182          }
 183      }
 184  
 185      private function getPrettyMetadata($type, $entity)
 186      {
 187          if ('tests' === $type) {
 188              return '';
 189          }
 190  
 191          try {
 192              $meta = $this->getMetadata($type, $entity);
 193              if (null === $meta) {
 194                  return '(unknown?)';
 195              }
 196          } catch (\UnexpectedValueException $e) {
 197              return ' <error>'.$e->getMessage().'</error>';
 198          }
 199  
 200          if ('globals' === $type) {
 201              if (\is_object($meta)) {
 202                  return ' = object('.\get_class($meta).')';
 203              }
 204  
 205              return ' = '.substr(@json_encode($meta), 0, 50);
 206          }
 207  
 208          if ('functions' === $type) {
 209              return '('.implode(', ', $meta).')';
 210          }
 211  
 212          if ('filters' === $type) {
 213              return $meta ? '('.implode(', ', $meta).')' : '';
 214          }
 215      }
 216  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1