Completed
Pull Request — master (#4)
by James Ekow Abaka
03:06 queued 36s
created

TemplateRenderer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 65.52%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 116
ccs 19
cts 29
cp 0.6552
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A loadEngine() 0 6 2
A setTempDirectory() 0 3 1
A getTemplateExtension() 0 7 3
A render() 0 9 2
A canRender() 0 7 2
A __construct() 0 5 1
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\engines\AbstractEngine;
30
use ntentan\honam\exceptions\TemplateEngineNotFoundException;
31
use ntentan\honam\exceptions\TemplateResolutionException;
32
33
/**
34
 * The TemplateEngine class does the work of resolving templates, loading template files,
35
 * loading template engines and rendering templates. The `ntentan/views` package takes a reference to a 
36
 * template and tries to find a specific template file to be used for the rendering. 
37
 * `ntentan/views` does not expect to find all view templates in a single directory.
38
 * It uses a heirachy of directories which it searches for the template to render.
39
 * Templates in directories closer to the beginning of the array have a higher
40
 * priority over those closer to the end of the array.
41
 * 
42
 */
43
class TemplateRenderer
44
{
45
46
    /**
47
     * An array of loaded template engine instances.
48
     * @var array<AbstractEngine>
49
     */
50
    private $loadedInstances;
51
52
    /**
53
     * Instance of the engine registry for loading template engines.
54
     * @var EngineRegistry
55
     */
56
    private $registry;
57
58
    /**
59
     * Instance of the template file resolver for finding template files.
60
     * @var TemplateFileResolver
61
     */
62
    private $templateFileResolver;
63
64
    private $tempDirectory;
65
66
    /**
67
     * TemplateRenderer constructor.
68
     *
69
     * @param EngineRegistry $registry
70
     * @param TemplateFileResolver $templateFileResolver
71
     */
72 37
    public function __construct(EngineRegistry $registry, TemplateFileResolver $templateFileResolver)
73
    {
74 37
        $this->registry = $registry;
75 37
        $this->templateFileResolver = $templateFileResolver;
76 37
        $this->tempDirectory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "honam_temp";
77
78
        //$registry->setTemplateRenderer($this);
79 37
    }
80
81
    /**
82
     * @param $templateFile
83
     * @return string|null
84
     * @throws TemplateEngineNotFoundException
85
     */
86 29
    private function getTemplateExtension($templateFile)
87
    {
88 29
        $supportedExtensions = $this->registry->getSupportedExtensions();
89 29
        foreach($supportedExtensions as $extension) {
90 29
            if($extension == substr($templateFile, -strlen($extension))) return $extension;
91
        }
92 1
        throw new TemplateEngineNotFoundException("There are currently no registered engines that can render $templateFile");
93
    }
94
95
96
    /**
97
     * Check if an engine exists that can render the given file.
98
     * @param $templateFile
99
     * @return bool
100
     * @throws TemplateEngineNotFoundException
101
     */
102
    public function canRender($templateFile)
103
    {
104
        try {
105
            $this->getTemplateExtension($templateFile);
106
            return true;
107
        } catch(TemplateEngineNotFoundException $exception) {
108
            return false;
109
        }
110
111
    }
112
113
114
    /**
115
     * Load and cache an instance of the a template engine.
116
     *
117
     * @param $extension
118
     * @return mixed
119
     * @throws exceptions\TemplateEngineNotFoundException
120
     * @throws exceptions\FactoryException
121
     */
122 28
    private function loadEngine($extension)
123
    {
124 28
        if(!isset($this->loadedInstances[$extension])) {
125 28
            $this->loadedInstances[$extension] = $this->registry->getEngineInstance($extension);
126
        }
127 28
        return $this->loadedInstances[$extension];
128
    }
129
130
    public function setTempDirectory(string $tempDirectory)
131
    {
132
        $this->tempDirectory = $tempDirectory;
133
    }
134
135
    /**
136
     * Renders a given template reference with associated template data. This render
137
     * function combs through the template directory heirachy to find a template
138
     * file which matches the given template reference and uses it for the purpose
139
     * of rendering the view.
140
     *
141
     * @param string $template The template reference file.
142
     * @param $data
143
     * @param bool $fromString
144
     * @param string $extension
145
     * @return string
146
     * @throws TemplateResolutionException
147
     * @throws exceptions\TemplateEngineNotFoundException
148
     * @throws exceptions\FactoryException
149
     */
150 30
    public function render($template, $data, $fromString = false, $extension=null)
151
    {
152 30
        if($fromString) {
153
            $engine = $this->loadEngine($extension);
154
            return $engine->renderFromStringTemplate($template, $data);
155
        } else {
156 30
            $templateFile = $this->templateFileResolver->resolveTemplateFile($template);
157 29
            $engine = $this->loadEngine($this->getTemplateExtension($templateFile));
158 28
            return $engine->renderFromFileTemplate($templateFile, $data);
159
        }
160
    }
161
}
162