Completed
Branch dev (374206)
by James Ekow Abaka
06:04
created

TemplateRenderer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getTemplateExtension() 0 7 3
A canRender() 0 4 1
A loadEngine() 0 7 2
A render() 0 11 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
31
/**
32
 * The TemplateEngine class does the work of resolving templates, loading template files,
33
 * loading template engines and rendering templates. The `ntentan/views` package takes a reference to a 
34
 * template and tries to find a specific template file to be used for the rendering. 
35
 * `ntentan/views` does not expect to find all view templates in a single directory.
36
 * It uses a heirachy of directories which it searches for the template to render.
37
 * Templates in directories closer to the beginning of the array have a higher
38
 * priority over those closer to the end of the array.
39
 * 
40
 */
41
class TemplateRenderer
42
{
43
44
    /**
45
     * An array of loaded template engine instances.
46
     * @var array<\ntentan\honam\TemplateEngine>
47
     */
48
    private $loadedInstances;
49
50
    /**
51
     * Instance of the engine registry for loading template engines.
52
     * @var EngineRegistry
53
     */
54
    private $registry;
55
56
    /**
57
     * Instance of the template file resolver for finding template files.
58
     * @var TemplateFileResolver
59
     */
60
    private $templateFileResolver;
61
62
    /**
63
     * TemplateRenderer constructor.
64
     *
65
     * @param EngineRegistry $registry
66
     */
67
    public function __construct(EngineRegistry $registry, TemplateFileResolver $templateFileResolver)
68
    {
69
        $this->registry = $registry;
70
        $this->templateFileResolver = $templateFileResolver;
71
        $registry->setTemplateRenderer($this);
72
    }
73
74
    /**
75
     * @return string|null
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string|null?

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...
76
     */
77
    private function getTemplateExtension($templateFile)
78
    {
79
        foreach($this->registry->getSupportedExtensions() as $extension) {
80
            if($extension == substr($templateFile, -strlen($extension))) return $extension;
81
        }
82
        return null;
83
    }
84
85
86
    /**
87
     * Check if an engine exists that can render the given file.
88
     * @param $file
89
     * @return bool
90
     */
91
    public function canRender($templateFile)
92
    {
93
        return $this->getTemplateExtension($templateFile) !== null;
94
    }
95
96
97
    /**
98
     * Load and cache an instance of the a template engine.
99
     *
100
     * @param $extension
101
     * @return mixed
102
     * @throws exceptions\TemplateEngineNotFoundException
103
     */
104
    private function loadEngine($extension)
105
    {
106
        if(!isset($this->loadedInstances[$extension])) {
107
            $this->loadedInstances[$extension] = $this->registry->getEngineInstance($extension);
108
        }
109
        return $this->loadedInstances[$extension];
110
    }
111
112
113
    /**
114
     * Renders a given template reference with associated template data. This render
115
     * function combs through the template directory heirachy to find a template
116
     * file which matches the given template reference and uses it for the purpose
117
     * of rendering the view.
118
     * 
119
     * @param string $template The template reference file.
120
     * @param array $templateData The data to be passed to the template.
0 ignored issues
show
Documentation introduced by
There is no parameter named $templateData. Did you maybe mean $template?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
121
     * @return string
122
     * @throws \ntentan\honam\exceptions\FileNotFoundException
123
     */
124
    public function render($template, $data, $fromString = false, $extension=null)
125
    {
126
        if($fromString) {
127
            $engine = $this->loadEngine($extension);
128
            return $engine->renderFromFileTemplate($template, $data);
129
        } else {
130
            $templateFile = $this->templateFileResolver->resolveTemplateFile($template);
131
            $engine = $this->loadEngine($this->getTemplateExtension($templateFile));
132
            return $engine->renderFromFileTemplate($templateFile, $data);
133
        }
134
    }
135
}
136