Completed
Pull Request — master (#1)
by Mathieu
04:21
created

AbstractEngine::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 $loader
32
     */
33
    private $loader;
34
35
    /**
36
     * @return string
37
     */
38
    abstract public function type();
39
40
    /**
41
     * Build the object with an array of dependencies.
42
     *
43
     * ## Required parameters:
44
     * - `logger` a PSR-3 logger
45
     *
46
     * ## Optional parameters:
47
     * - `loader` a Loader object
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
57
    /**
58
     * @param LoaderInterface $loader A loader instance.
59
     * @return void
60
     */
61
    private function setLoader(LoaderInterface $loader)
62
    {
63
        $this->loader = $loader;
64
    }
65
66
    /**
67
     * @return LoaderInterface
68
     */
69
    protected function loader()
70
    {
71
        return $this->loader;
72
    }
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($templateIdent)
81
    {
82
        return $this->loader()->load($templateIdent);
83
    }
84
85
    /**
86
     * @param string $templateIdent The template identifier to load and render.
87
     * @param mixed  $context       The rendering context.
88
     * @return string The rendered template string.
89
     */
90
    public function render($templateIdent, $context)
91
    {
92
        $template = $this->loadTemplate($templateIdent);
93
        return $this->renderTemplate($template, $context);
94
    }
95
96
    /**
97
     * @param string $templateString The template string to render.
98
     * @param mixed  $context        The rendering context.
99
     * @return string The rendered template string.
100
     */
101
    abstract public function renderTemplate($templateString, $context);
102
}
103