Completed
Pull Request — master (#70)
by Martin
02:41
created

TwitalLoaderTrait::doConstruct()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 7.6666
cc 10
nc 8
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Goetas\Twital;
4
5
use Goetas\Twital\SourceAdapter\HTML5Adapter;
6
use Goetas\Twital\SourceAdapter\XHTMLAdapter;
7
use Goetas\Twital\SourceAdapter\XMLAdapter;
8
use Twig\Environment;
9
use Twig\Error\LoaderError;
10
use Twig\Loader\ExistsLoaderInterface;
11
use Twig\Loader\LoaderInterface;
12
use Twig\Loader\SourceContextLoaderInterface;
13
use Twig\Source;
14
15
/**
16
 * @author Martin Hasoň <[email protected]>
17
 */
18
trait TwitalLoaderTrait
19
{
20
    /**
21
     * Array of patterns used to decide if a template is twital-compilable or not.
22
     * Items are strings or callbacks
23
     *
24
     * @var array
25
     */
26
    protected $sourceAdapters = array();
27
28
    /**
29
     * The internal Twital compiler
30
     *
31
     * @var Twital
32
     */
33
    protected $twital;
34
35
    /**
36
     * The wrapped Twig loader
37
     *
38
     * @var LoaderInterface|\Twig_LoaderInterface
39
     */
40
    protected $loader;
41
42
    private $twigMajorVersion;
43
44
    /**
45
     * Add a new pattern that can decide if a template is twital-compilable or not.
46
     * If $pattern is a string, then must be a valid regex that matches the template filename.
47
     * If $pattern is a callback, then must return true if the template is compilable, false otherwise.
48
     *
49
     * @param string|callback $pattern
50
     * @param SourceAdapter $adapter
51
     * @return TwitalLoader
52
     */
53
    public function addSourceAdapter($pattern, SourceAdapter $adapter)
54
    {
55
        $this->sourceAdapters[$pattern] = $adapter;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Get all patterns used to choose if a template is twital-compilable or not
62
     *
63
     * @return array:
64
     */
65
    public function getSourceAdapters()
66
    {
67
        return $this->sourceAdapters;
68
    }
69
70
    /**
71
     * Decide if a template is twital-compilable or not.
72
     *
73
     * @param string $name
74
     * @return SourceAdapter
75
     */
76
    public function getSourceAdapter($name)
77
    {
78
        foreach (array_reverse($this->sourceAdapters) as $pattern => $adapter) {
79
            if (preg_match($pattern, $name)) {
80
                return $adapter;
81
            }
82
        }
83
84
        return null;
85
    }
86
87
    /**
88
     * Get the wrapped Twig loader
89
     *
90
     * @return LoaderInterface|\Twig_LoaderInterface
91
     */
92
    public function getLoader()
93
    {
94
        return $this->loader;
95
    }
96
97
    /**
98
     * Set the wrapped Twig loader
99
     *
100
     * @param LoaderInterface|\Twig_LoaderInterface $loader
101
     * @return TwitalLoader
102
     */
103
    public function setLoader($loader)
104
    {
105
        $this->loader = $loader;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return Twital
112
     */
113
    public function getTwital()
114
    {
115
        if ($this->twital === null) {
116
            $this->twital = new Twital();
117
        }
118
119
        return $this->twital;
120
    }
121
122
    /**
123
     * Creates a new Twital loader.
124
     *
125
     * @param LoaderInterface|\Twig_LoaderInterface $loader
126
     * @param Twital $twital
127
     * @param bool $addDefaults If NULL, some standard rules will be used (`*.twital.*` and `*.twital`).
128
     */
129
    private function doConstruct($loader = null, Twital $twital = null, $addDefaults = true)
130
    {
131
        $this->loader = $loader;
132
        $this->twital = $twital;
133
134
        if ($addDefaults === true || (is_array($addDefaults) && in_array('html', $addDefaults))) {
135
            $this->addSourceAdapter('/\.twital\.html$/i', new HTML5Adapter());
136
        }
137
        if ($addDefaults === true || (is_array($addDefaults) && in_array('xml', $addDefaults))) {
138
            $this->addSourceAdapter('/\.twital\.xml$/i', new XMLAdapter());
139
        }
140
        if ($addDefaults === true || (is_array($addDefaults) && in_array('xhtml', $addDefaults))) {
141
            $this->addSourceAdapter('/\.twital\.xhtml$/i', new XHTMLAdapter());
142
        }
143
    }
144
145
    private function doGetSourceContext($name)
146
    {
147
        if ($this->getTwigMajorVersion() >= 2 || $this->loader instanceof SourceContextLoaderInterface) {
148
            $originalContext = $this->loader->getSourceContext($name);
149
            $code = $originalContext->getCode();
150
            $path = $originalContext->getPath();
151
        } else {
152
            $code = $this->loader->getSource($name);
153
            $path = null;
154
        }
155
156
        if ($adapter = $this->getSourceAdapter($name)) {
157
            $code = $this->getTwital()->compile($adapter, $code);
158
        }
159
160
        return [$code, $name, $path];
161
    }
162
163
    private function doExists($name)
164
    {
165
        if ($this->getTwigMajorVersion() >= 2 || $this->loader instanceof ExistsLoaderInterface) {
166
            return $this->loader->exists($name);
167
        } else {
168
            try {
169
                $this->getSourceContext($name);
0 ignored issues
show
Bug introduced by
The method getSourceContext() does not exist on Goetas\Twital\TwitalLoaderTrait. Did you maybe mean doGetSourceContext()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
170
171
                return true;
172
            } catch (LoaderError $e) {
173
                return false;
174
            } catch (\Twig_Error_Loader $e) {
175
                return false;
176
            }
177
        }
178
    }
179
180
    private function getTwigMajorVersion()
181
    {
182
        if (null === $this->twigMajorVersion) {
183
            $this->twigMajorVersion = class_exists(Environment::class) ? Environment::MAJOR_VERSION : \Twig_Environment::MAJOR_VERSION;
184
        }
185
186
        return $this->twigMajorVersion;
187
    }
188
}
189