Completed
Push — master ( 637f9b...59f005 )
by arto
02:08
created

ExpressiveTemplateAdapter::addPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2016-02-03
5
 */
6
namespace Net\Bazzline\Component\Template;
7
8
use Zend\Expressive\Template\TemplatePath;
9
use Zend\Expressive\Template\TemplateRendererInterface;
10
11
class ExpressiveTemplateAdapter implements TemplateRendererInterface
12
{
13
    /** @var AbstractFileBasedTemplate */
14
    private $baseTemplate;
15
16
    /** @var array */
17
    private $defaultParameters = array();
18
19
    /** @var bool */
20
    private $defaultParametersProvided = false;
21
22
    /** @var array */
23
    private $fullQualifiedPathNameWithDefaultParameters = array();
24
25
    /** @var array|TemplatePath[] */
26
    private $fullQualifiedPathNameWithTemplatePath = array();
27
28
    /**
29
     * @param AbstractFileBasedTemplate $template
30
     */
31
    public function injectBaseTemplate(AbstractFileBasedTemplate $template)
32
    {
33
        $this->baseTemplate = $template;
34
    }
35
36
    /**
37
     * Render a template, optionally with parameters.
38
     *
39
     * Implementations MUST support the `namespace::template` naming convention,
40
     * and allow omitting the filename extension.
41
     *
42
     * @param string $name
43
     * @param array|object $params
44
     * @return string
45
     */
46
    public function render($name, $params = [])
47
    {
48
        if (isset($this->fullQualifiedPathNameWithTemplatePath[$name])) {
49
            $template   = $this->getNewTemplate();
50
            $path       = $this->fullQualifiedPathNameWithTemplatePath[$name];
51
52
            $template->setFilePath($path->getPath() . '.phtml');
53
54
            if ($this->defaultParametersProvided) {
55
                $template->assignMany($this->defaultParameters);
56
            }
57
58
            if (isset($this->fullQualifiedPathNameWithDefaultParameters[$name])) {
59
                $template->assignMany($this->fullQualifiedPathNameWithDefaultParameters[$name]);
60
            }
61
62
            if (!empty($params)) {
63
                $template->assignMany($params);
0 ignored issues
show
Bug introduced by
It seems like $params defined by parameter $params on line 46 can also be of type object; however, Net\Bazzline\Component\T...tTemplate::assignMany() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
64
            }
65
66
            $content = $template->render();
67
        } else {
68
            $content = '';
69
        }
70
71
        return $content;
72
    }
73
74
    /**
75
     * Add a template path to the engine.
76
     *
77
     * Adds a template path, with optional namespace the templates in that path
78
     * provide.
79
     *
80
     * @param string $path
81
     * @param string $namespace
82
     */
83
    public function addPath($path, $namespace = null)
84
    {
85
        $key = (is_null($namespace)) ? $path : $namespace . '::' . $path;
86
87
        $this->fullQualifiedPathNameWithTemplatePath[$key] = new TemplatePath($path, $namespace);
88
    }
89
90
    /**
91
     * Retrieve configured paths from the engine.
92
     *
93
     * @return TemplatePath[]
94
     */
95
    public function getPaths()
96
    {
97
        return $this->fullQualifiedPathNameWithTemplatePath;
98
    }
99
100
    /**
101
     * Add a default parameter to use with a template.
102
     *
103
     * Use this method to provide a default parameter to use when a template is
104
     * rendered. The parameter may be overridden by providing it when calling
105
     * `render()`, or by calling this method again with a null value.
106
     *
107
     * The parameter will be specific to the template name provided. To make
108
     * the parameter available to any template, pass the TEMPLATE_ALL constant
109
     * for the template name.
110
     *
111
     * If the default parameter existed previously, subsequent invocations with
112
     * the same template name and parameter name will overwrite.
113
     *
114
     * @param string $templateName Name of template to which the param applies;
115
     *     use TEMPLATE_ALL to apply to all templates.
116
     * @param string $param Param name.
117
     * @param mixed $value
118
     */
119
    public function addDefaultParam($templateName, $param, $value)
120
    {
121
        $isDefaultForAllTemplates = ($templateName === self::TEMPLATE_ALL);
122
123
        if ($isDefaultForAllTemplates) {
124
            $this->defaultParameters[$param] = $value;
125
            $this->defaultParametersProvided = true;
126
        } else {
127
            $this->fullQualifiedPathNameWithDefaultParameters[$templateName] = array($param => $value);
128
        }
129
    }
130
131
    /**
132
     * @return AbstractFileBasedTemplate
133
     */
134
    private function getNewTemplate()
135
    {
136
        return clone $this->baseTemplate;
137
    }
138
}