Completed
Pull Request — master (#70)
by Martin
01:40
created

TwitalLoaderTrait::addSourceAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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