Completed
Push — master ( d7c080...7c46c5 )
by Asmir
10s
created

TwitalLoader::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Goetas\Twital;
3
4
use Goetas\Twital\SourceAdapter\HTML5Adapter;
5
use Goetas\Twital\SourceAdapter\XMLAdapter;
6
use Goetas\Twital\SourceAdapter\XHTMLAdapter;
7
8
/**
9
 * This is a Twital Loader.
10
 * Compiles a Twital template into a Twig template.
11
 *
12
 * @author Asmir Mustafic <[email protected]>
13
 */
14
class TwitalLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface, \Twig_SourceContextLoaderInterface
15
{
16
    /**
17
     * Array of patterns used to decide if a template is twital-compilable or not.
18
     * Items are strings or callbacks
19
     *
20
     * @var array
21
     */
22
    protected $sourceAdapters = array();
23
24
    /**
25
     * The wrapped Twig loader
26
     *
27
     * @var \Twig_LoaderInterface
28
     */
29
    protected $loader;
30
31
    /**
32
     * The internal Twital compiler
33
     *
34
     * @var Twital
35
     */
36
    protected $twital;
37
38
    /**
39
     * Creates a new Twital loader.
40
     * @param \Twig_LoaderInterface $loader
41
     * @param Twital $twital
42
     * @param bool $addDefaults If NULL, some standard rules will be used (`*.twital.*` and `*.twital`).
43
     */
44 45
    public function __construct(\Twig_LoaderInterface $loader = null, Twital $twital = null, $addDefaults = true)
45
    {
46 45
        $this->loader = $loader;
47 45
        $this->twital = $twital;
48
49 45
        if ($addDefaults === true || (is_array($addDefaults) && in_array('html', $addDefaults))) {
50 6
            $this->addSourceAdapter('/\.twital\.html$/i', new HTML5Adapter());
51 6
        }
52 45
        if ($addDefaults === true || (is_array($addDefaults) && in_array('xml', $addDefaults))) {
53 6
            $this->addSourceAdapter('/\.twital\.xml$/i', new XMLAdapter());
54 6
        }
55 45
        if ($addDefaults === true || (is_array($addDefaults) && in_array('xhtml', $addDefaults))) {
56 6
            $this->addSourceAdapter('/\.twital\.xhtml$/i', new XHTMLAdapter());
57 6
        }
58 45
    }
59
60 28
    public function getSourceContext($name)
61
    {
62 28
        if (\Twig_Environment::MAJOR_VERSION === 2 || $this->loader instanceof \Twig_SourceContextLoaderInterface) {
63 26
            $originalContext = $this->loader->getSourceContext($name);
64 26
            $code = $originalContext->getCode();
65 26
            $path = $originalContext->getPath();
66 26
        } else {
67 2
            $code = $this->loader->getSource($name);
0 ignored issues
show
Bug introduced by
The method getSource() does not exist on Twig_LoaderInterface. Did you maybe mean getSourceContext()?

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...
68 1
            $path = null;
69
        }
70
71 27
        if ($adapter = $this->getSourceAdapter($name)) {
72 25
            $code = $this->getTwital()->compile($adapter, $code);
73 23
        }
74
75 25
        return new \Twig_Source($code, $name, $path);
76
    }
77
78
    /**
79
     * Add a new pattern that can decide if a template is twital-compilable or not.
80
     * If $pattern is a string, then must be a valid regex that matches the template filename.
81
     * If $pattern is a callback, then must return true if the template is compilable, false otherwise.
82
     *
83
     * @param string|callback $pattern
84
     * @param SourceAdapter $adapter
85
     * @return TwitalLoader
86
     */
87 43
    public function addSourceAdapter($pattern, SourceAdapter $adapter)
88
    {
89 43
        $this->sourceAdapters[$pattern] = $adapter;
90
91 43
        return $this;
92
    }
93
94
    /**
95
     * Get all patterns used to choose if a template is twital-compilable or not
96
     *
97
     * @return array:
98
     */
99 1
    public function getSourceAdapters()
100
    {
101 1
        return $this->sourceAdapters;
102
    }
103
104
    /**
105
     * Decide if a template is twital-compilable or not.
106
     *
107
     * @param string $name
108
     * @return SourceAdapter
109
     */
110 31
    public function getSourceAdapter($name)
111
    {
112 31
        foreach (array_reverse($this->sourceAdapters) as $pattern => $adapter) {
113 30
            if (preg_match($pattern, $name)) {
114 28
                return $adapter;
115
            }
116 5
        }
117
118 3
        return null;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function getSource($name)
125
    {
126
        return $this->getSourceContext($name)->getCode();
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 35
    public function getCacheKey($name)
133
    {
134 35
        return $this->loader->getCacheKey($name);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function isFresh($name, $time)
141
    {
142
        return $this->loader->isFresh($name, $time);
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 2
    public function exists($name)
149
    {
150 2
        if (\Twig_Environment::MAJOR_VERSION === 2 || $this->loader instanceof \Twig_ExistsLoaderInterface) {
151
            return $this->loader->exists($name);
152
        } else {
153
            try {
154 2
                $this->getSourceContext($name);
155
156 1
                return true;
157 1
            } catch (\Twig_Error_Loader $e) {
158 1
                return false;
159
            }
160
        }
161
    }
162
163
    /**
164
     * Get the wrapped Twig loader
165
     *
166
     * @return \Twig_LoaderInterface
167
     */
168 1
    public function getLoader()
169
    {
170 1
        return $this->loader;
171
    }
172
173
    /**
174
     * Set the wrapped Twig loader
175
     *
176
     * @param \Twig_LoaderInterface $loader
177
     * @return TwitalLoader
178
     */
179 1
    public function setLoader(\Twig_LoaderInterface $loader)
180
    {
181 1
        $this->loader = $loader;
182
183 1
        return $this;
184
    }
185
186
    /**
187
     * @return Twital
188
     */
189 25
    public function getTwital()
190
    {
191 25
        if ($this->twital === null) {
192 24
            $this->twital = new Twital();
193 24
        }
194
195 25
        return $this->twital;
196
    }
197
198
}
199