Twig::fromString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
use Twig_Loader_Array;
7
use Twig_Environment;
8
use Twig_Source;
9
use Twig_Extensions_Extension_I18n;
10
11
/**
12
 * Class to get gettext strings from twig files returning arrays.
13
 */
14
class Twig extends Extractor implements ExtractorInterface
15
{
16
    public static $options = [
17
        'twig' => null,
18
    ];
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public static function fromString($string, Translations $translations, array $options = [])
24
    {
25
        $options += static::$options;
26
27
        $twig = $options['twig'] ?: self::createTwig();
28
29
        PhpCode::fromString($twig->compileSource(new Twig_Source($string, '')), $translations, $options);
30
    }
31
32
    /**
33
     * Returns a Twig instance.
34
     *
35
     * @return Twig_Environment
36
     */
37
    private static function createTwig()
38
    {
39
        $twig = new Twig_Environment(new Twig_Loader_Array(['' => '']));
40
        $twig->addExtension(new Twig_Extensions_Extension_I18n());
41
42
        return static::$options['twig'] = $twig;
43
    }
44
}
45