AbstractEngine   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 121
rs 10
c 0
b 0
f 0

10 Methods

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