[ Index ]

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


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