ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Twig.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Gettext\Extractors;
4 
8 
12 class Twig extends Extractor implements ExtractorInterface
13 {
19  protected static $twig;
20 
24  public static function fromString($string, Translations $translations = null, $file = '')
25  {
26  self::addExtension('Twig_Extensions_Extension_I18n');
27 
28  $string = self::$twig->compileSource($string);
29 
30  // add default global php gettext functions
31  PhpCode::$functions['gettext'] = '__';
32  PhpCode::$functions['ngettext'] = '__';
33  PhpCode::$functions['_'] = '__';
34 
35  return PhpCode::fromString($string, $translations, $file);
36  }
37 
44  public static function addExtension($extension)
45  {
46  // initialise twig
47  if (!isset(self::$twig)) {
48  $twigCompiler = new Twig_Loader_String();
49 
50  self::$twig = new Twig_Environment($twigCompiler);
51  }
52 
53  if (!self::checkHasExtensionByClassName($extension)) {
54  self::$twig->addExtension(new $extension());
55  }
56  }
57 
65  protected static function checkHasExtensionByClassName($className)
66  {
67  foreach (self::$twig->getExtensions() as $extension) {
68  if ($className == get_class($extension)) {
69  return true;
70  }
71  }
72 
73  return false;
74  }
75 }
static fromString($string, Translations $translations=null, $file='')
{Parses a string and append the translations found in the Translations instance.The file path to inse...
Definition: PhpCode.php:50
static checkHasExtensionByClassName($className)
Checks if a given Twig extension is already registered or not.
Definition: Twig.php:65
Class to manage a collection of translations.
static addExtension($extension)
Initialise Twig if it isn&#39;t already, and add a given Twig extension.
Definition: Twig.php:44
static fromString($string, Translations $translations=null, $file='')
{Parses a string and append the translations found in the Translations instance.The file path to inse...
Definition: Twig.php:24
Stores the Twig configuration.
Definition: Environment.php:17