1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\View; |
4
|
|
|
|
5
|
|
|
// PSR-3 (logger) dependencies |
6
|
|
|
use Psr\Log\LoggerAwareInterface; |
7
|
|
|
use Psr\Log\LoggerAwareTrait; |
8
|
|
|
|
9
|
|
|
// Local namespace dependencies |
10
|
|
|
use Charcoal\View\EngineInterface; |
11
|
|
|
use Charcoal\View\LoaderInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Default implementation, as abstract class, of the `EngineInterface`. |
15
|
|
|
* |
16
|
|
|
* View Engines are comprised of 2 things: |
17
|
|
|
* - A template loader, wich is a `LoaderInterfaceObject` |
18
|
|
|
* - Set with `set_loader()` / Get with `loader()` |
19
|
|
|
* - Provides `loadtemplate()` method |
20
|
|
|
* - A `render()` method, which takes a $template and a $context arguments |
21
|
|
|
* |
22
|
|
|
* > Engines implements the `LoggerAwareInterface`. A logger can be accessed with the `logger()` method. |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractEngine implements |
25
|
|
|
EngineInterface, |
26
|
|
|
LoggerAwareInterface |
27
|
|
|
{ |
28
|
|
|
use LoggerAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var LoaderInterface |
32
|
|
|
*/ |
33
|
|
|
private $loader; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The cache option. |
37
|
|
|
* |
38
|
|
|
* @var mixed |
39
|
|
|
*/ |
40
|
|
|
private $cache; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
abstract public function type(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Build the object with an array of dependencies. |
49
|
|
|
* |
50
|
|
|
* ## Required parameters: |
51
|
|
|
* - `logger` a PSR-3 logger |
52
|
|
|
* - `loader` a Loader object, to load templates. |
53
|
|
|
* |
54
|
|
|
* @param array $data Engine dependencie. |
55
|
|
|
*/ |
56
|
|
|
public function __construct(array $data) |
57
|
|
|
{ |
58
|
|
|
$this->setLogger($data['logger']); |
59
|
|
|
$this->setLoader($data['loader']); |
60
|
|
|
|
61
|
|
|
if (isset($data['cache'])) { |
62
|
|
|
$this->setCache($data['cache']); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the engine's cache implementation. |
68
|
|
|
* |
69
|
|
|
* @param mixed $cache A engine cache implementation, |
70
|
|
|
* an absolute path to the compiled views, |
71
|
|
|
* a boolean to enable/disable cache. |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
protected function setCache($cache) |
75
|
|
|
{ |
76
|
|
|
$this->cache = $cache; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the engine's cache implementation. |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
protected function cache() |
85
|
|
|
{ |
86
|
|
|
return $this->cache; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param LoaderInterface $loader A loader instance. |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
private function setLoader(LoaderInterface $loader) |
94
|
|
|
{ |
95
|
|
|
$this->loader = $loader; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return LoaderInterface |
100
|
|
|
*/ |
101
|
|
|
protected function loader() |
102
|
|
|
{ |
103
|
|
|
return $this->loader; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Delegates template loading to the engine's Loader object. |
108
|
|
|
* |
109
|
|
|
* @param string $templateIdent The template identifier to load. |
110
|
|
|
* @return string The template string, loaded from identifier. |
111
|
|
|
*/ |
112
|
|
|
public function loadTemplate($templateIdent) |
113
|
|
|
{ |
114
|
|
|
return $this->loader()->load($templateIdent); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Render a template (from ident) with a given context. |
119
|
|
|
* |
120
|
|
|
* @param string $templateIdent The template identifier to load and render. |
121
|
|
|
* @param mixed $context The rendering context. |
122
|
|
|
* @return string The rendered template string. |
123
|
|
|
*/ |
124
|
|
|
public function render($templateIdent, $context) |
125
|
|
|
{ |
126
|
|
|
$template = $this->loadTemplate($templateIdent); |
127
|
|
|
return $this->renderTemplate($template, $context); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $templateString The template string to render. |
132
|
|
|
* @param mixed $context The rendering context. |
133
|
|
|
* @return string The rendered template string. |
134
|
|
|
*/ |
135
|
|
|
abstract public function renderTemplate($templateString, $context); |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $varName The name of the variable to set this template unto. |
139
|
|
|
* @param string|null $templateIdent The "dynamic template" to set. null to clear. |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public function setDynamicTemplate($varName, $templateIdent) |
143
|
|
|
{ |
144
|
|
|
$this->loader()->setDynamicTemplate($varName, $templateIdent); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|