Completed
Pull Request — master (#69)
by Vladimir
05:36
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
18
class TwigExtension extends \Twig_Extension
19
{
20
    private $filters = [];
21
    private $functions = [];
22
    private $markupManager;
23
24 54
    public function __construct(MarkupEngineManager $manager)
25
    {
26 54
        $this->markupManager = $manager;
27 54
    }
28
29
    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...
30
    {
31
        $tag = lcfirst(str_replace('parseMarkup', '', $name));
32
33
        if (!$tag)
34
        {
35
            throw new \BadMethodCallException("No method $name found in this class.");
36
        }
37
38
        return $this->parseMarkup($arguments[0], $tag);
39
    }
40
41
    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...
42
    {
43
        return $this->markupManager->getEngineByTag($tag)->parse($content);
44
    }
45
46 18 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...
47
    {
48
        /** @var AbstractTwigExtension|TwigFilterInterface $filter */
49 18
        foreach ($filters as $filter)
50
        {
51 18
            if (Service::hasRunTimeFlag(RuntimeStatus::IN_SAFE_MODE) && $filter::disableInSafeMode())
52
            {
53
                continue;
54
            }
55
56 18
            $this->filters[] = $filter::get();
57
        }
58 18
    }
59
60 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...
61
    {
62
        /** @var AbstractTwigExtension|TwigFunctionInterface $fxn */
63
        foreach ($functions as $fxn)
64
        {
65
            if (Service::hasRunTimeFlag(RuntimeStatus::IN_SAFE_MODE) && $fxn::disableInSafeMode())
66
            {
67
                continue;
68
            }
69
70
            $this->functions[] = $fxn::get();
71
        }
72
    }
73
74
    public function getFilters()
75
    {
76
        $filters = $this->filters;
77
78
        foreach ($this->markupManager->getTemplateTags() as $tag)
79
        {
80
            // Since we can't pass what tag/markup language we're using to the callable, let's make the callable to a
81
            // non-existent method that will be handled by __call()
82
            $filters[] = new \Twig_SimpleFilter(
83
                $tag,
84
                [$this, 'parseMarkup' . ucfirst($tag)],
85
                ['is_safe' => ['html']]
86
            );
87
        }
88
89
        return $filters;
90
    }
91
92
    public function getFunctions()
93
    {
94
        return $this->functions;
95
    }
96
97
    public function getTokenParsers()
98
    {
99
        $tokenParsers = [];
100
101
        foreach ($this->markupManager->getTemplateTags() as $tag)
102
        {
103
            $tokenParsers[] = new TokenParser($tag);
104
        }
105
106
        return $tokenParsers;
107
    }
108
109 54
    public function getName()
110
    {
111 54
        return 'stakx_twig_base_extension';
112
    }
113
}
114