Completed
Pull Request — develop (#122)
by Vladimir
01:44
created

TwigExtension::getTokenParsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.9
ccs 0
cts 5
cp 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Templating\Twig;
9
10
use allejo\stakx\MarkupEngine\MarkupEngineManager;
11
use allejo\stakx\RuntimeStatus;
12
use allejo\stakx\Service;
13
use allejo\stakx\Templating\Twig\Extension\AbstractTwigExtension;
14
use allejo\stakx\Templating\Twig\Extension\TwigFilterInterface;
15
use allejo\stakx\Templating\Twig\Extension\TwigFunctionInterface;
16
use allejo\stakx\Templating\Twig\MarkupBlock\TokenParser;
17
use Twig\Extension\AbstractExtension;
18
use Twig\TwigFilter;
19
20
class TwigExtension extends AbstractExtension
21
{
22
    private $filters = [];
23
    private $functions = [];
24
    private $markupManager;
25
26 70
    public function __construct(MarkupEngineManager $manager)
27
    {
28 70
        $this->markupManager = $manager;
29 70
    }
30
31
    public function __call($name, $arguments)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
32
    {
33
        $tag = lcfirst(str_replace('parseMarkup', '', $name));
34
35
        if (!$tag)
36
        {
37
            throw new \BadMethodCallException("No method $name found in this class.");
38
        }
39
40
        return $this->parseMarkup($arguments[0], $tag);
41
    }
42
43
    public function parseMarkup($content, $tag)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
44
    {
45
        return $this->markupManager->getEngineByTag($tag)->parse($content);
46
    }
47
48 22 View Code Duplication
    public function addFilters(/*iterable*/ $filters)
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...
49
    {
50
        /** @var AbstractTwigExtension|TwigFilterInterface $filter */
51 22
        foreach ($filters as $filter)
52
        {
53 22
            if (Service::hasRunTimeFlag(RuntimeStatus::IN_SAFE_MODE) && $filter::disableInSafeMode())
54
            {
55
                continue;
56
            }
57
58 22
            $this->filters[] = $filter::get();
59
        }
60 22
    }
61
62 View Code Duplication
    public function addFunctions(/*iterable*/ $functions)
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...
63
    {
64
        /** @var AbstractTwigExtension|TwigFunctionInterface $fxn */
65
        foreach ($functions as $fxn)
66
        {
67
            if (Service::hasRunTimeFlag(RuntimeStatus::IN_SAFE_MODE) && $fxn::disableInSafeMode())
68
            {
69
                continue;
70
            }
71
72
            $this->functions[] = $fxn::get();
73
        }
74
    }
75
76
    public function getFilters()
77
    {
78
        $filters = $this->filters;
79
80
        foreach ($this->markupManager->getTemplateTags() as $tag)
81
        {
82
            // Since we can't pass what tag/markup language we're using to the callable, let's make the callable to a
83
            // non-existent method that will be handled by __call()
84
            $filters[] = new TwigFilter(
85
                $tag,
86
                [$this, 'parseMarkup' . ucfirst($tag)],
87
                ['is_safe' => ['html']]
88
            );
89
        }
90
91
        return $filters;
92
    }
93
94
    public function getFunctions()
95
    {
96
        return $this->functions;
97
    }
98
99
    public function getTokenParsers()
100
    {
101
        $tokenParsers = [];
102
103
        foreach ($this->markupManager->getTemplateTags() as $tag)
104
        {
105
            $tokenParsers[] = new TokenParser($tag);
106
        }
107
108
        return $tokenParsers;
109
    }
110
111 70
    public function getName()
112
    {
113 70
        return 'stakx_twig_base_extension';
114
    }
115
}
116