Passed
Pull Request — master (#7)
by Chauncey
02:26
created

AbstractEngine::templateRegistry()   A

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\View;
4
5
// From PSR-3
6
use Psr\Log\LoggerAwareInterface;
7
use Psr\Log\LoggerAwareTrait;
8
9
// From 'charcoal-view'
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
     * Build the object with an array of dependencies.
44
     *
45
     * ## Required parameters:
46
     * - `logger` a PSR-3 logger
47
     * - `loader` a Loader object, to load templates.
48
     *
49
     * @param array $data Engine dependencie.
50
     */
51
    public function __construct(array $data)
52
    {
53
        $this->setLogger($data['logger']);
54
        $this->setLoader($data['loader']);
55
56
        if (array_key_exists('cache', $data)) {
57
            $this->setCache($data['cache']);
58
        }
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    abstract public function type();
65
66
    /**
67
     * Render a template (from ident) with a given context.
68
     *
69
     * @param string $templateIdent The template identifier to load and render.
70
     * @param mixed  $context       The rendering context.
71
     * @return string The rendered template string.
72
     */
73
    public function render($templateIdent, $context)
74
    {
75
        $template = $this->loadTemplate($templateIdent);
76
        return $this->renderTemplate($template, $context);
77
    }
78
79
    /**
80
     * @param string $templateString The template string to render.
81
     * @param mixed  $context        The rendering context.
82
     * @return string The rendered template string.
83
     */
84
    abstract public function renderTemplate($templateString, $context);
85
86
    /**
87
     * Delegates template loading to the engine's Loader object.
88
     *
89
     * @param string $templateIdent The template identifier to load.
90
     * @return string The template string, loaded from identifier.
91
     */
92
    public function loadTemplate($templateIdent)
93
    {
94
        return $this->loader()->load($templateIdent);
95
    }
96
97
    /**
98
     * @param string $varName The name of the variable to get template ident from.
99
     * @return string
100
     */
101
    public function dynamicTemplate($varName)
102
    {
103
        return $this->loader()->dynamicTemplate($varName);
104
    }
105
106
    /**
107
     * @param string $varName The name of the variable to get template ident from.
108
     * @return boolean
109
     */
110
    public function hasDynamicTemplate($varName)
111
    {
112
        return $this->loader()->hasDynamicTemplate($varName);
113
    }
114
115
    /**
116
     * @param string      $varName       The name of the variable to set this template unto.
117
     * @param string|null $templateIdent The "dynamic template" to set. null to clear.
118
     * @return void
119
     */
120
    public function setDynamicTemplate($varName, $templateIdent)
121
    {
122
        $this->loader()->setDynamicTemplate($varName, $templateIdent);
123
    }
124
125
    /**
126
     * @return \Charcoal\View\LoaderRegistryInterface
127
     */
128
    public function templateRegistry()
129
    {
130
        return $this->loader()->templateRegistry();
131
    }
132
133
    /**
134
     * Set the engine's cache implementation.
135
     *
136
     * @param  mixed $cache A engine cache implementation,
137
     *                      an absolute path to the compiled views,
138
     *                      a boolean to enable/disable cache.
139
     * @return void
140
     */
141
    protected function setCache($cache)
142
    {
143
        $this->cache = $cache;
144
    }
145
146
    /**
147
     * Get the engine's cache implementation.
148
     *
149
     * @return mixed
150
     */
151
    protected function cache()
152
    {
153
        return $this->cache;
154
    }
155
156
    /**
157
     * @param LoaderInterface $loader A loader instance.
158
     * @return void
159
     */
160
    private function setLoader(LoaderInterface $loader)
161
    {
162
        $this->loader = $loader;
163
    }
164
165
    /**
166
     * @return LoaderInterface
167
     */
168
    protected function loader()
169
    {
170
        return $this->loader;
171
    }
172
}
173