Passed
Pull Request — master (#2)
by Chauncey
02:01
created

TwigLoader::isTemplateString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Charcoal\View\Twig;
4
5
// From Twig
6
use Twig_LoaderInterface;
7
use Twig_Source;
8
9
// From 'charcoal-view'
10
use Charcoal\View\AbstractLoader;
11
use Charcoal\View\LoaderInterface;
12
13
/**
14
 * The Charcoal View Twig Loader implements both Twig_LoaderInterface and its own LoaderInterface.
15
 */
16
class TwigLoader extends AbstractLoader implements
17
    LoaderInterface,
18
    Twig_LoaderInterface
19
{
20
    /**
21
     * Determine if the variable is a template literal.
22
     *
23
     * This method looks for any tag delimiters in the given string,
24
     * which a file path would most likely not have.
25
     *
26
     * @todo   Add support for custom delimiters.
27
     * @param  string $ident The template being evaluated.
28
     * @return boolean
29
     */
30
    protected function isTemplateString($ident)
31
    {
32
        return strpos($ident, '{%') !== false || parent::isTemplateString($ident);
0 ignored issues
show
Bug introduced by
The method isTemplateString() cannot be called from this context as it is declared protected in class Charcoal\View\AbstractLoader.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
33
    }
34
35
    /**
36
     * Convert an identifier to a file path.
37
     *
38
     * @param string $ident The identifier to convert.
39
     * @return string
40
     */
41
    protected function filenameFromIdent($ident)
42
    {
43
        $filename = str_replace([ '\\' ], '.', $ident);
44
        $filename .= '.twig';
45
46
        return $filename;
47
    }
48
49
    /**
50
     * Twig_LoaderInterface > getSource()
51
     *
52
     * Gets the source code of a template, given its name.
53
     *
54
     * @param  string $name The name of the template to load.
55
     * @return string The template source code.
56
     */
57
    public function getSource($name)
58
    {
59
        return $this->load($name);
0 ignored issues
show
Deprecated Code introduced by
The method Charcoal\View\AbstractLoader::load() has been deprecated with message: $GLOBALS['widget_template']

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
60
    }
61
62
    /**
63
     * Twig_LoaderInterface > getSourceContext()
64
     *
65
     * Gets the source code of a template, given its name.
66
     * For Twig 2.x
67
     *
68
     * @param  string $name The name of the template to load.
69
     * @return Twig_Source The template source object.
70
     */
71
    public function getSourceContext($name)
72
    {
73
        $source = $this->load($name);
0 ignored issues
show
Deprecated Code introduced by
The method Charcoal\View\AbstractLoader::load() has been deprecated with message: $GLOBALS['widget_template']

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
74
        return new Twig_Source($source, $name);
75
    }
76
77
    /**
78
     * Twig_LoaderInterface > exists()
79
     *
80
     * For Twig 2.x
81
     *
82
     * @param  string $name The name of the template to load.
83
     * @return boolean
84
     */
85
    public function exists($name)
86
    {
87
        return !!$this->findTemplateFile($name);
88
    }
89
90
    /**
91
     * Twig_LoaderInterface > getCacheKey()
92
     *
93
     * Gets the cache key to use for the cache for a given template name.
94
     *
95
     * @param  string $name The name of the template to load.
96
     * @return string|null The cache key
97
     */
98
    public function getCacheKey($name)
99
    {
100
        $key = $this->findTemplateFile($name);
101
        return $key;
102
    }
103
104
    /**
105
     * Twig_LoaderInterface > isFresh()
106
     *
107
     * Returns true if the template is still fresh.
108
     *
109
     * @param string  $name The template name.
110
     * @param integer $time The last modification time of the cached template.
111
     * @return boolean
112
     */
113
    public function isFresh($name, $time)
114
    {
115
        $file = $this->findTemplateFile($name);
116
        $fresh = (filemtime($file) <= $time);
117
        return $fresh;
118
    }
119
}
120