Completed
Push — dev ( 48451b...8a2d43 )
by James Ekow Abaka
08:16
created

TemplateRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * View Templating System
5
 * Copyright (c) 2008-2015 James Ekow Abaka Ainooson
6
 * 
7
 * Permission is hereby granted, free of charge, to any person obtaining
8
 * a copy of this software and associated documentation files (the
9
 * "Software"), to deal in the Software without restriction, including
10
 * without limitation the rights to use, copy, modify, merge, publish,
11
 * distribute, sublicense, and/or sell copies of the Software, and to
12
 * permit persons to whom the Software is furnished to do so, subject to
13
 * the following conditions:
14
 * 
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 * 
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
25
 */
26
27
namespace ntentan\honam;
28
29
use ntentan\honam\exceptions\TemplateResolutionException;
30
use ntentan\honam\exceptions\UnknownTemplateExtensionException;
31
32
/**
33
 * The TemplateEngine class does the work of resolving templates, loading template files,
34
 * loading template engines and rendering templates. The `ntentan/views` package takes a reference to a 
35
 * template and tries to find a specific template file to be used for the rendering. 
36
 * `ntentan/views` does not expect to find all view templates in a single directory.
37
 * It uses a heirachy of directories which it searches for the template to render.
38
 * Templates in directories closer to the beginning of the array have a higher
39
 * priority over those closer to the end of the array.
40
 * 
41
 */
42
class TemplateRenderer
43
{
44
45
    /**
46
     * An array of loaded template engine instances.
47
     * @var array<\ntentan\honam\TemplateEngine>
48
     */
49
    private $loadedInstances;
50
51
    /**
52
     * Instance of the engine registry for loading template engines.
53
     * @var EngineRegistry
54
     */
55
    private $registry;
56
57
    /**
58
     * Instance of the template file resolver for finding template files.
59
     * @var TemplateFileResolver
60
     */
61
    private $templateFileResolver;
62
63
    private $tempDirectory;
64
65
    /**
66
     * TemplateRenderer constructor.
67
     *
68
     * @param EngineRegistry $registry
69
     * @param TemplateFileResolver $templateFileResolver
70
     */
71
    public function __construct(EngineRegistry $registry, TemplateFileResolver $templateFileResolver)
72
    {
73
        $this->registry = $registry;
74
        $this->templateFileResolver = $templateFileResolver;
75
        $registry->setTemplateRenderer($this);
76
    }
77
78
    /**
79
     * @param $templateFile
80
     * @return string|null
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
81
     * @throws UnknownTemplateExtensionException
82
     */
83
    private function getTemplateExtension($templateFile)
84
    {
85
        $supportedExtensions = $this->registry->getSupportedExtensions();
86
        foreach($supportedExtensions as $extension) {
87
            if($extension == substr($templateFile, -strlen($extension))) return $extension;
88
        }
89
        throw new UnknownTemplateExtensionException("There are currently no registered engines that can render $templateFile");
90
    }
91
92
93
    /**
94
     * Check if an engine exists that can render the given file.
95
     * @param $templateFile
96
     * @return bool
97
     * @throws UnknownTemplateExtensionException
98
     */
99
    public function canRender($templateFile)
100
    {
101
        return $this->getTemplateExtension($templateFile) !== null;
102
    }
103
104
105
    /**
106
     * Load and cache an instance of the a template engine.
107
     *
108
     * @param $extension
109
     * @return mixed
110
     * @throws exceptions\TemplateEngineNotFoundException
111
     */
112
    private function loadEngine($extension)
113
    {
114
        if(!isset($this->loadedInstances[$extension])) {
115
            $this->loadedInstances[$extension] = $this->registry->getEngineInstance($extension);
116
        }
117
        return $this->loadedInstances[$extension];
118
    }
119
120
    public function setTempDirectory(string $tempDirectory)
121
    {
122
        $this->tempDirectory = $tempDirectory;
123
    }
124
125
    public function getTempDirectory()
126
    {
127
        return $this->tempDirectory;
128
    }
129
130
131
    /**
132
     * Renders a given template reference with associated template data. This render
133
     * function combs through the template directory heirachy to find a template
134
     * file which matches the given template reference and uses it for the purpose
135
     * of rendering the view.
136
     *
137
     * @param string $template The template reference file.
138
     * @param $data
139
     * @param bool $fromString
140
     * @param null $extension
141
     * @return string
142
     * @throws TemplateResolutionException
143
     * @throws UnknownTemplateExtensionException
144
     * @throws exceptions\TemplateEngineNotFoundException
145
     */
146
    public function render($template, $data, $fromString = false, $extension=null)
147
    {
148
        if($fromString) {
149
            $engine = $this->loadEngine($extension);
150
            return $engine->renderFromFileTemplate($template, $data);
151
        } else {
152
            $templateFile = $this->templateFileResolver->resolveTemplateFile($template);
153
            $engine = $this->loadEngine($this->getTemplateExtension($templateFile));
154
            return $engine->renderFromFileTemplate($templateFile, $data);
155
        }
156
    }
157
}
158