Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

TwigExtractor.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 2.91 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Bridge\Twig\Translation;
013   
014  use Symfony\Component\Finder\Finder;
015  use Symfony\Component\Finder\SplFileInfo;
016  use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
017  use Symfony\Component\Translation\Extractor\ExtractorInterface;
018  use Symfony\Component\Translation\MessageCatalogue;
019   
020  /**
021   * TwigExtractor extracts translation messages from a twig template.
022   *
023   * @author Michel Salib <michelsalib@hotmail.com>
024   * @author Fabien Potencier <fabien@symfony.com>
025   */
026  class TwigExtractor extends AbstractFileExtractor implements ExtractorInterface
027  {
028      /**
029       * Default domain for found messages.
030       *
031       * @var string
032       */
033      private $defaultDomain = 'messages';
034   
035      /**
036       * Prefix for found message.
037       *
038       * @var string
039       */
040      private $prefix = '';
041   
042      /**
043       * The twig environment.
044       *
045       * @var \Twig_Environment
046       */
047      private $twig;
048   
049      public function __construct(\Twig_Environment $twig)
050      {
051          $this->twig = $twig;
052      }
053   
054      /**
055       * {@inheritdoc}
056       */
057      public function extract($resource, MessageCatalogue $catalogue)
058      {
059          $files = $this->extractFiles($resource);
060          foreach ($files as $file) {
061              try {
062                  $this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
063              } catch (\Twig_Error $e) {
064                  if ($file instanceof SplFileInfo) {
065                      $e->setTemplateFile($file->getRelativePathname());
066                  } elseif ($file instanceof \SplFileInfo) {
067                      $e->setTemplateFile($file->getRealPath());
068                  }
069   
070                  throw $e;
071              }
072          }
073      }
074   
075      /**
076       * {@inheritdoc}
077       */
078      public function setPrefix($prefix)
079      {
080          $this->prefix = $prefix;
081      }
082   
083      protected function extractTemplate($template, MessageCatalogue $catalogue)
084      {
085          $visitor = $this->twig->getExtension('translator')->getTranslationNodeVisitor();
086          $visitor->enable();
087   
088          $this->twig->parse($this->twig->tokenize($template));
089   
090          foreach ($visitor->getMessages() as $message) {
091              $catalogue->set(trim($message[0]), $this->prefix.trim($message[0]), $message[1] ?: $this->defaultDomain);
092          }
093   
094          $visitor->disable();
095      }
096   
097      /**
098       * @param string $file
099       *
100       * @return bool
101       */
102      protected function canBeExtracted($file)
103      {
104          return $this->isFile($file) && 'twig' === pathinfo($file, PATHINFO_EXTENSION);
105      }
106   
107      /**
108       * @param string|array $directory
109       *
110       * @return array
111       */
112      protected function extractFromDirectory($directory)
113      {
114          $finder = new Finder();
115   
116          return $finder->files()->name('*.twig')->in($directory);
117      }
118  }
119