Completed
Push — twig-2 ( a5bda0 )
by Asmir
03:26
created

TwitalLoader   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 6

Test Coverage

Coverage 87.27%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
dl 0
loc 182
c 4
b 0
f 1
wmc 28
cbo 6
ccs 48
cts 55
cp 0.8727
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 15 10
A getSourceContext() 0 8 1
A addSourceAdapter() 0 6 1
A getSourceAdapters() 0 4 1
A getSourceAdapter() 0 10 3
A getSource() 0 10 2
A getCacheKey() 0 4 1
A isFresh() 0 4 1
A exists() 0 14 4
A getLoader() 0 4 1
A setLoader() 0 6 1
A getTwital() 0 8 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
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 32
    public function __construct(\Twig_LoaderInterface $loader = null, Twital $twital = null, $addDefaults = true)
45
    {
46 32
        $this->loader = $loader;
47 32
        $this->twital = $twital;
48
49 32
        if ($addDefaults === true || (is_array($addDefaults) && in_array('html', $addDefaults))) {
50 6
            $this->addSourceAdapter('/\.twital\.html$/i', new HTML5Adapter());
51 6
        }
52 32
        if ($addDefaults === true || (is_array($addDefaults) && in_array('xml', $addDefaults))) {
53 6
            $this->addSourceAdapter('/\.twital\.xml$/i', new XMLAdapter());
54 6
        }
55 32
        if ($addDefaults === true || (is_array($addDefaults) && in_array('xhtml', $addDefaults))) {
56 6
            $this->addSourceAdapter('/\.twital\.xhtml$/i', new XHTMLAdapter());
57 6
        }
58 32
    }
59
60
    public function getSourceContext($name)
61
    {
62
        $originalContext = $this->loader->getSourceContext($name);
63
64
        $source = $this->getSource($name);
65
66
        return new \Twig_Source($source, $name, $originalContext->getPath());
67
    }
68
69
    /**
70
     * Add a new pattern that can decide if a template is twital-compilable or not.
71
     * If $pattern is a string, then must be a valid regex that matches the template filename.
72
     * If $pattern is a callback, then must return true if the template is compilable, false otherwise.
73
     *
74
     * @param string|callback $pattern
75
     * @param SourceAdapter $adapter
76
     * @return TwitalLoader
77
     */
78 30
    public function addSourceAdapter($pattern, SourceAdapter $adapter)
79
    {
80 30
        $this->sourceAdapters[$pattern] = $adapter;
81
82 30
        return $this;
83
    }
84
85
    /**
86
     * Get all patterns used to choose if a template is twital-compilable or not
87
     *
88
     * @return array:
89
     */
90 1
    public function getSourceAdapters()
91
    {
92 1
        return $this->sourceAdapters;
93
    }
94
95
    /**
96
     * Decide if a template is twital-compilable or not.
97
     *
98
     * @param string $name
99
     * @return SourceAdapter
100
     */
101 25
    public function getSourceAdapter($name)
102
    {
103 25
        foreach (array_reverse($this->sourceAdapters) as $pattern => $adapter) {
104 25
            if (preg_match($pattern, $name)) {
105 23
                return $adapter;
106
            }
107 4
        }
108
109 2
        return null;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 21
    public function getSource($name)
116
    {
117 21
        $source = $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...
118
119 21
        if ($adapter = $this->getSourceAdapter($name)) {
120 20
            $source = $this->getTwital()->compile($adapter, $source);
121 18
        }
122
123 19
        return $source;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 22
    public function getCacheKey($name)
130
    {
131 22
        return $this->loader->getCacheKey($name);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function isFresh($name, $time)
138
    {
139
        return $this->loader->isFresh($name, $time);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 2
    public function exists($name)
146
    {
147 2
        if (\Twig_Environment::MAJOR_VERSION == 2 || ($this->loader instanceof \Twig_ExistsLoaderInterface)) {
148
            return $this->loader->exists($name);
149
        } else {
150
            try {
151 2
                $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...
152
153 1
                return true;
154 1
            } catch (\Twig_Error_Loader $e) {
155 1
                return false;
156
            }
157
        }
158
    }
159
160
    /**
161
     * Get the wrapped Twig loader
162
     *
163
     * @return \Twig_LoaderInterface
164
     */
165 1
    public function getLoader()
166
    {
167 1
        return $this->loader;
168
    }
169
170
    /**
171
     * Set the wrapped Twig loader
172
     *
173
     * @param \Twig_LoaderInterface $loader
174
     * @return TwitalLoader
175
     */
176 1
    public function setLoader(\Twig_LoaderInterface $loader)
177
    {
178 1
        $this->loader = $loader;
179
180 1
        return $this;
181
    }
182
183
    /**
184
     * @return Twital
185
     */
186 20
    public function getTwital()
187
    {
188 20
        if ($this->twital===null) {
189 19
            $this->twital = new Twital();
190 19
        }
191
192 20
        return $this->twital;
193
    }
194
195
}
196