|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2010-2017 Nevraxe inc. & Marc André Audet <[email protected]>. All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are |
|
9
|
|
|
* permitted provided that the following conditions are met: |
|
10
|
|
|
* |
|
11
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of |
|
12
|
|
|
* conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list |
|
15
|
|
|
* of conditions and the following disclaimer in the documentation and/or other materials |
|
16
|
|
|
* provided with the distribution. |
|
17
|
|
|
* |
|
18
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
19
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
21
|
|
|
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
|
22
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
23
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
24
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
25
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
26
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
27
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
namespace Cervo; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
use Cervo\Core as _; |
|
36
|
|
|
use Cervo\Exceptions\TemplateFileMissingException; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Template class for Cervo. |
|
41
|
|
|
* |
|
42
|
|
|
* @author Marc André Audet <[email protected]> |
|
43
|
|
|
*/ |
|
44
|
|
|
final class Template |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* The template path. |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $path; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The template's data. |
|
54
|
|
|
* Usually set from the View. |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $data = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Initialize the template. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $name The template (file)name. |
|
63
|
|
|
* |
|
64
|
|
|
* @throws TemplateFileMissingException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct($name) |
|
67
|
|
|
{ |
|
68
|
|
|
$config = _::getLibrary('Cervo/Config'); |
|
69
|
|
|
|
|
70
|
|
|
$ex_name = explode('/', $name); |
|
71
|
|
|
|
|
72
|
|
|
$this->path = $config->get('Cervo/Application/Directory') . $ex_name[0] . \DS . $config->get('Cervo/Application/TemplatesPath') . implode('/', array_slice($ex_name, 1)) . '.php'; |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
if (!file_exists($this->path)) { |
|
75
|
|
|
throw new TemplateFileMissingException(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Magic method to return the data. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $name |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
public function __get($name) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->get($name); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Not so magic method to return the data. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function get($name) |
|
99
|
|
|
{ |
|
100
|
|
|
if (isset($this->data[$name])) { |
|
101
|
|
|
return $this->data[$name]; |
|
102
|
|
|
} else { |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Assign an array as the template's data. |
|
109
|
|
|
* Usually set from the View. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $data |
|
112
|
|
|
* |
|
113
|
|
|
* @return $this |
|
114
|
|
|
*/ |
|
115
|
|
|
public function assign($data = []) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->data = array_merge($data, $this->data); |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Render the template. |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $data |
|
125
|
|
|
*/ |
|
126
|
|
|
public function render($data = []) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->data = array_merge($data, $this->data); |
|
129
|
|
|
require $this->path; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..