Completed
Push — dev ( a486ee...7113e4 )
by James Ekow Abaka
03:55
created

TemplateRenderer::loadEngine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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