1: <?php
2:
3: namespace Charcoal\View\Twig;
4:
5: // From `twig/twig`
6: use Twig_LoaderInterface;
7: use Twig_Source;
8:
9: // Parent namespace dependencies
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: /**
22: * Convert an identifier to a file path.
23: *
24: * @param string $ident The identifier to convert.
25: * @return string
26: */
27: protected function filenameFromIdent($ident)
28: {
29: $filename = str_replace([ '\\' ], '.', $ident);
30: $filename .= '.twig';
31:
32: return $filename;
33: }
34:
35: /**
36: * Twig_LoaderInterface > getSource()
37: *
38: * Gets the source code of a template, given its name.
39: *
40: * @param string $name The name of the template to load.
41: * @return string The template source code.
42: */
43: public function getSource($name)
44: {
45: return $this->load($name);
46: }
47:
48: /**
49: * Twig_LoaderInterface > getSourceContext()
50: *
51: * Gets the source code of a template, given its name.
52: * For Twig 2.x
53: *
54: * @param string $name The name of the template to load.
55: * @return Twig_Source The template source object.
56: */
57: public function getSourceContext($name)
58: {
59: $source = $this->load($name);
60: return new Twig_Source($source, $name);
61: }
62:
63: /**
64: * Twig_LoaderInterface > exists()
65: *
66: * For Twig 2.x
67: *
68: * @param string $name The name of the template to load.
69: * @return boolean
70: */
71: public function exists($name)
72: {
73: return !!$this->findTemplateFile($name);
74: }
75:
76: /**
77: * Twig_LoaderInterface > getCacheKey()
78: *
79: * Gets the cache key to use for the cache for a given template name.
80: *
81: * @param string $name The name of the template to load.
82: * @return string The cache key
83: */
84: public function getCacheKey($name)
85: {
86: $key = $this->findTemplateFile($name);
87: return $key;
88: }
89:
90: /**
91: * Twig_LoaderInterface > isFresh()
92: *
93: * Returns true if the template is still fresh.
94: *
95: * @param string $name The template name.
96: * @param integer $time The last modification time of the cached template.
97: * @return boolean
98: */
99: public function isFresh($name, $time)
100: {
101: $file = $this->findTemplateFile($name);
102: $fresh = (filemtime($file) <= $time);
103: return $fresh;
104: }
105: }
106: