[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Translation/ -> TwigExtractor.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\Translation;
  13  
  14  use Symfony\Component\Finder\Finder;
  15  use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
  16  use Symfony\Component\Translation\Extractor\ExtractorInterface;
  17  use Symfony\Component\Translation\MessageCatalogue;
  18  use Twig\Environment;
  19  use Twig\Error\Error;
  20  use Twig\Source;
  21  
  22  /**
  23   * TwigExtractor extracts translation messages from a twig template.
  24   *
  25   * @author Michel Salib <michelsalib@hotmail.com>
  26   * @author Fabien Potencier <fabien@symfony.com>
  27   */
  28  class TwigExtractor extends AbstractFileExtractor implements ExtractorInterface
  29  {
  30      /**
  31       * Default domain for found messages.
  32       *
  33       * @var string
  34       */
  35      private $defaultDomain = 'messages';
  36  
  37      /**
  38       * Prefix for found message.
  39       *
  40       * @var string
  41       */
  42      private $prefix = '';
  43  
  44      private $twig;
  45  
  46      public function __construct(Environment $twig)
  47      {
  48          $this->twig = $twig;
  49      }
  50  
  51      /**
  52       * {@inheritdoc}
  53       */
  54      public function extract($resource, MessageCatalogue $catalogue)
  55      {
  56          foreach ($this->extractFiles($resource) as $file) {
  57              try {
  58                  $this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
  59              } catch (Error $e) {
  60                  // ignore errors, these should be fixed by using the linter
  61              }
  62          }
  63      }
  64  
  65      /**
  66       * {@inheritdoc}
  67       */
  68      public function setPrefix($prefix)
  69      {
  70          $this->prefix = $prefix;
  71      }
  72  
  73      protected function extractTemplate($template, MessageCatalogue $catalogue)
  74      {
  75          $visitor = $this->twig->getExtension('Symfony\Bridge\Twig\Extension\TranslationExtension')->getTranslationNodeVisitor();
  76          $visitor->enable();
  77  
  78          $this->twig->parse($this->twig->tokenize(new Source($template, '')));
  79  
  80          foreach ($visitor->getMessages() as $message) {
  81              $catalogue->set(trim($message[0]), $this->prefix.trim($message[0]), $message[1] ?: $this->defaultDomain);
  82          }
  83  
  84          $visitor->disable();
  85      }
  86  
  87      /**
  88       * @param string $file
  89       *
  90       * @return bool
  91       */
  92      protected function canBeExtracted($file)
  93      {
  94          return $this->isFile($file) && 'twig' === pathinfo($file, \PATHINFO_EXTENSION);
  95      }
  96  
  97      /**
  98       * {@inheritdoc}
  99       */
 100      protected function extractFromDirectory($directory)
 101      {
 102          $finder = new Finder();
 103  
 104          return $finder->files()->name('*.twig')->in($directory);
 105      }
 106  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1