Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
taoItems_models_classes_TemplateRenderer
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 setContext
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 setTemplate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 render
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung
19 *                         (under the project TAO-TRANSFER);
20 *               2009-2012 (update and modification) Public Research Centre Henri Tudor
21 *                         (under the project TAO-SUSTAIN & TAO-DEV);
22 */
23
24/**
25 * This class is a simple "search and replace" PHP-Like template renderer.
26 * It parses a file with php short tags and replace the variables by the
27 * in attributes
28 *
29 * @access public
30 * @author Joel Bout, <joel@taotesting.com>
31 * @package taoItems
32
33 */
34class taoItems_models_classes_TemplateRenderer
35{
36    // --- ASSOCIATIONS ---
37
38
39    // --- ATTRIBUTES ---
40
41    /**
42     * Short description of attribute context
43     *
44     * @access protected
45     * @var array
46     */
47    protected static $context = [];
48
49    /**
50     * ClearFW Renderer
51     *
52     * @access private
53     */
54    private $renderer = null;
55
56    // --- OPERATIONS ---
57
58    /**
59     * Short description of method __construct
60     *
61     * @access public
62     * @author Joel Bout, <joel@taotesting.com>
63     * @param  string templatePath
64     * @param  array variables
65     * @return mixed
66     */
67    public function __construct($templatePath, $variables = [])
68    {
69
70
71        if (
72            !file_exists($templatePath)
73            || !is_readable($templatePath)
74            || !preg_match("/\.tpl\.php$/", basename($templatePath))
75        ) {
76            common_Logger::w('Template ', $templatePath . ' not found');
77            throw new InvalidArgumentException("Unable to load the template file from $templatePath");
78        }
79
80        if (!tao_helpers_File::securityCheck($templatePath)) {
81            throw new Exception("Security warning: $templatePath is not safe.");
82        }
83
84        $this->renderer = new Renderer($templatePath, $variables);
85    }
86
87    /**
88     * Short description of method setContext
89     *
90     * @access public
91     * @author Joel Bout, <joel@taotesting.com>
92     * @param  array parameters
93     * @param  string prefix
94     * @return mixed
95     */
96    public static function setContext($parameters, $prefix = '')
97    {
98
99
100        self::$context = [];
101
102        foreach ($parameters as $key => $value) {
103            self::$context[$prefix . $key] = $value;
104        }
105    }
106
107    /**
108     * sets the template to be used
109     *
110     * @access public
111     * @author Joel Bout, <joel@taotesting.com>
112     * @param  string templatePath
113     * @return mixed
114     */
115    public function setTemplate($templatePath)
116    {
117
118        $this->renderer->setTemplate($templatePath);
119    }
120
121    /**
122     * adds or replaces the data for a specific key
123     *
124     * @access public
125     * @author Joel Bout, <joel@taotesting.com>
126     * @param  string key
127     * @param  value
128     * @return mixed
129     */
130    public function setData($key, $value)
131    {
132
133        $this->renderer->setData($key, $value);
134    }
135
136    /**
137     * Short description of method render
138     *
139     * @access public
140     * @author Joel Bout, <joel@taotesting.com>
141     * @return string
142     */
143    public function render()
144    {
145        $returnValue = (string) '';
146
147
148        $this->renderer->setMultipleData(self::$context);
149        $returnValue = $this->renderer->render();
150
151
152        return (string) $returnValue;
153    }
154}