|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CedricZiel\TwigLoaderFlysystem; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\Filesystem; |
|
6
|
|
|
use Twig_Error_Loader; |
|
7
|
|
|
use Twig_LoaderInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Provides a template loader for twig that allows to use flysystem |
|
11
|
|
|
* instances to load templates. |
|
12
|
|
|
* |
|
13
|
|
|
* @package CedricZiel\TwigLoaderFlysystem |
|
14
|
|
|
*/ |
|
15
|
|
|
class FlysystemLoader implements Twig_LoaderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Filesystem |
|
19
|
|
|
*/ |
|
20
|
|
|
private $filesystem; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $templatePath; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* FlysystemLoader constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Filesystem $filesystem |
|
31
|
|
|
*/ |
|
32
|
15 |
|
public function __construct(Filesystem $filesystem, $templatePath = '') |
|
33
|
|
|
{ |
|
34
|
15 |
|
$this->filesystem = $filesystem; |
|
35
|
15 |
|
$this->templatePath = $templatePath; |
|
36
|
15 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Gets the source code of a template, given its name. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name The name of the template to load |
|
42
|
|
|
* |
|
43
|
|
|
* @return string The template source code |
|
44
|
|
|
* |
|
45
|
|
|
* @throws Twig_Error_Loader When $name is not found |
|
46
|
|
|
*/ |
|
47
|
9 |
|
public function getSource($name) |
|
48
|
|
|
{ |
|
49
|
9 |
|
$this->getFileOrFail($name); |
|
50
|
|
|
|
|
51
|
6 |
|
return $this->filesystem->read($this->resolveTemplateName($name)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Checks if the underlying flysystem contains a file of the given name. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name |
|
58
|
|
|
* |
|
59
|
|
|
* @return \League\Flysystem\File|\League\Flysystem\Handler |
|
60
|
|
|
* @throws Twig_Error_Loader |
|
61
|
|
|
*/ |
|
62
|
15 |
|
protected function getFileOrFail($name) |
|
63
|
|
|
{ |
|
64
|
15 |
|
if (!$this->filesystem->has($this->resolveTemplateName($name))) { |
|
65
|
3 |
|
throw new Twig_Error_Loader('Template could not be found on the given filesystem'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
12 |
|
$fileObject = $this->filesystem->get($this->resolveTemplateName($name)); |
|
69
|
12 |
|
if ($fileObject->isDir()) { |
|
70
|
|
|
throw new Twig_Error_Loader('Cannot use directory as template'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
12 |
|
return $fileObject; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Gets the cache key to use for the cache for a given template name. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $name The name of the template to load |
|
80
|
|
|
* |
|
81
|
|
|
* @return string The cache key |
|
82
|
|
|
* |
|
83
|
|
|
* @throws Twig_Error_Loader When $name is not found |
|
84
|
|
|
*/ |
|
85
|
3 |
|
public function getCacheKey($name) |
|
86
|
|
|
{ |
|
87
|
3 |
|
$this->getFileOrFail($name); |
|
88
|
|
|
|
|
89
|
3 |
|
return $name; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns true if the template is still fresh. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $name The template name |
|
96
|
|
|
* @param int $time Timestamp of the last modification time of the |
|
97
|
|
|
* cached template |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool true if the template is fresh, false otherwise |
|
100
|
|
|
* |
|
101
|
|
|
* @throws Twig_Error_Loader When $name is not found |
|
102
|
|
|
*/ |
|
103
|
3 |
|
public function isFresh($name, $time) |
|
104
|
|
|
{ |
|
105
|
3 |
|
$object = $this->getFileOrFail($name); |
|
106
|
|
|
|
|
107
|
3 |
|
return (int)$time >= (int)$object->getTimestamp(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $name |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
15 |
|
protected function resolveTemplateName($name) |
|
116
|
|
|
{ |
|
117
|
15 |
|
$prefix = $this->templatePath; |
|
118
|
15 |
|
if ($this->templatePath !== null && $this->templatePath !== '') { |
|
119
|
3 |
|
$prefix = rtrim($prefix, '/').'/'; |
|
120
|
3 |
|
} |
|
121
|
|
|
|
|
122
|
15 |
|
return $prefix.$name; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|