Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

TwigStakxBridge::getProfilerOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace allejo\stakx\Templating;
4
5
use allejo\stakx\Compiler;
6
use allejo\stakx\Twig\StakxTwigTextProfiler;
7
use Psr\Log\LoggerInterface;
8
9
class TwigStakxBridge implements TemplateBridgeInterface
10
{
11
    private $twig;
12
    private $logger;
13
14
    /** @var \Twig_Profiler_Profile */
15
    private $profiler;
16
17 42
    public function __construct(\Twig_Environment $twig)
18
    {
19 42
        $this->twig = $twig;
20 42
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function setLogger(LoggerInterface $logger)
26
    {
27
        $this->logger = $logger;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 14
    public function setGlobalVariable($key, $value)
34
    {
35 14
        $this->twig->addGlobal($key, $value);
36 14
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 14
    public function createTemplate($templateContent)
42
    {
43
        try
44
        {
45 14
            $template = $this->twig->createTemplate($templateContent);
46
        }
47 14
        catch (\Twig_Error $e)
48
        {
49
            throw new TwigError($e);
50
        }
51
52 14
        return (new TwigTemplate($template));
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 21 View Code Duplication
    public function getAssortmentDependencies($namespace, $bodyContent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        // To see what this regex should match and what shouldn't be see:
61
        //     tests/allejo/stakx/Test/FrontMatter/FrontMatterDocumentTest.php
62
63 21
        $regex = "/{[{%].*?(?:$namespace)(?:\.|\[['\"])?([^_][^\W]+)?(?:\.|['\"]\])?[^_=]*?[%}]}/";
64 21
        $results = array();
65
66 21
        preg_match_all($regex, $bodyContent, $results);
67
68 21
        return array_unique($results[1]);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 7 View Code Duplication
    public function getTemplateImportDependencies($bodyContent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76 7
        $regex = "/{%\s?(?:import|from|include)\s?['\"](.+)['\"].+/";
77 7
        $results = array();
78
79 7
        preg_match_all($regex, $bodyContent, $results);
80
81 7
        if (empty($results[1]))
82 7
        {
83
            return [];
84
        }
85
86 7
        return array_unique($results[1]);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function hasProfiler()
93
    {
94
        return ($this->profiler !== null);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 14
    public function setProfiler($profiler)
101
    {
102 14
        $this->profiler = $profiler;
103 14
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getProfilerOutput(Compiler $compiler)
109
    {
110
        $dumper = new StakxTwigTextProfiler();
111
        $dumper->setTemplateMappings($compiler->getTemplateMappings());
112
113
        return $dumper->dump($this->profiler);
114
    }
115
}
116