Passed
Push — master ( b912aa...9d745d )
by Mathieu
01:50
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\Loader\LoaderInterface as TwigLoaderInterface;
7
use Twig\Loader\ExistsLoaderInterface as TwigExistsLoaderInterface;
8
use Twig\Loader\SourceContextLoaderInterface as TwigSourceContextLoaderInterface;
9
use Twig\Source as TwigSource;
10
11
// From 'charcoal-view'
12
use Charcoal\View\AbstractLoader;
13
use Charcoal\View\LoaderInterface;
14
15
/**
16
 * Twig Template Loader
17
 *
18
 * Finds a Twig template file in a collection of directory paths.
19
 */
20
class TwigLoader extends AbstractLoader implements
21
    LoaderInterface,
22
    TwigLoaderInterface,
23
    TwigExistsLoaderInterface,
0 ignored issues
show
Deprecated Code introduced by
The interface Twig\Loader\ExistsLoaderInterface has been deprecated with message: since 1.12 (to be removed in 3.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
24
    TwigSourceContextLoaderInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Twig\Loader\SourceContextLoaderInterface has been deprecated with message: since 1.27 (to be removed in 3.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
25
{
26
    /**
27
     * Determine if the variable is a template literal.
28
     *
29
     * This method looks for any tag delimiters in the given string,
30
     * which a file path would most likely not have.
31
     *
32
     * @todo   Add support for custom delimiters.
33
     * @param  string $ident The template being evaluated.
34
     * @return boolean
35
     */
36
    protected function isTemplateString($ident)
37
    {
38
        return strpos($ident, '{%') !== false || parent::isTemplateString($ident);
39
    }
40
41
    /**
42
     * Convert an identifier to a file path.
43
     *
44
     * @param  string $ident The identifier to convert.
45
     * @return string
46
     */
47
    protected function filenameFromIdent($ident)
48
    {
49
        $filename = str_replace([ '\\' ], '.', $ident);
50
        $filename .= '.twig';
51
52
        return $filename;
53
    }
54
55
    /**
56
     * Gets the source code of a template, given its name.
57
     *
58
     * @see TwigLoaderInterface::getSource()
59
     *     Deprecated since Twig v1.27 (to be removed in Twig v2.0)
60
     *
61
     * @param  string $name The name of the template to load.
62
     * @return string The template source code.
63
     */
64
    public function getSource($name)
65
    {
66
        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...
67
    }
68
69
    /**
70
     * Returns the source context for a given template logical name.
71
     *
72
     * @see TwigSourceContextLoaderInterface::getSourceContext()
73
     *     Deprecated since Twig v1.27 (to be removed in Twig v3.0).
74
     *
75
     * @param  string $name The name of the template to load.
76
     * @return TwigSource The template source object.
77
     */
78
    public function getSourceContext($name)
79
    {
80
        $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...
81
        return new TwigSource($source, $name);
82
    }
83
84
    /**
85
     * Check if we have the source code of a template, given its name.
86
     *
87
     * @see TwigExistsLoaderInterface::exists()
88
     *     Deprecated since Twig v1.12 (to be removed in Twig v3.0).
89
     *
90
     * @param  string $name The name of the template to load.
91
     * @return boolean
92
     */
93
    public function exists($name)
94
    {
95
        return !!$this->findTemplateFile($name);
96
    }
97
98
    /**
99
     * Gets the cache key to use for the cache for a given template name.
100
     *
101
     * @see TwigLoaderInterface::getCacheKey()
102
     *
103
     * @param  string $name The name of the template to load.
104
     * @return string|null The cache key
105
     */
106
    public function getCacheKey($name)
107
    {
108
        $key = $this->findTemplateFile($name);
109
        return $key;
110
    }
111
112
    /**
113
     * Returns true if the template is still fresh.
114
     *
115
     * @see TwigLoaderInterface::isFresh()
116
     *
117
     * @param  string  $name The template name.
118
     * @param  integer $time The last modification time of the cached template.
119
     * @return boolean
120
     */
121
    public function isFresh($name, $time)
122
    {
123
        $file = $this->findTemplateFile($name);
124
        $fresh = (filemtime($file) <= $time);
125
        return $fresh;
126
    }
127
}
128