|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Layout |
|
4
|
|
|
* @todo allow switching between layout templates in the theme and the views folder |
|
5
|
|
|
* |
|
6
|
|
|
* @package erdiko/core |
|
7
|
|
|
* @copyright 2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com |
|
8
|
|
|
* @author John Arroyo <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace erdiko\core; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class Layout extends Container |
|
14
|
|
|
{ |
|
15
|
|
|
/** Theme Template folder */ |
|
16
|
|
|
protected $_themeRootFolder; |
|
17
|
|
|
/** Regions */ |
|
18
|
|
|
protected $_regions = array(); |
|
19
|
|
|
/** Data */ |
|
20
|
|
|
protected $_data = array(); |
|
21
|
|
|
/** Title */ |
|
22
|
|
|
protected $_title = ""; |
|
23
|
|
|
/** Theme */ |
|
24
|
|
|
protected $_theme; |
|
25
|
|
|
/** Theme */ |
|
26
|
|
|
protected $_viewRootFolder; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $template , Theme Object (Contaier) |
|
33
|
|
|
* @param mixed $data |
|
34
|
|
|
* @param string $theme |
|
|
|
|
|
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($template = null, $data = array(), $themeName = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->initiate($template, $data); // @todo don't store in both _data and _regions |
|
39
|
|
|
$this->setRegions($data); |
|
40
|
|
|
$this->setThemeRootFolder('themes'); |
|
41
|
|
|
$this->setThemeName($themeName); |
|
42
|
|
|
$this->setTemplateFolder($this->getThemeRootFolder().'/'.$themeName.'/templates/layouts'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* setTitle |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $title |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setTitle($title) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->_title = $title; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* getTitle |
|
57
|
|
|
* |
|
58
|
|
|
* @return string $title |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getTitle() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->_title; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get template file |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $filename |
|
69
|
|
|
* @param mixed $data Typically a string, Container object or other object |
|
70
|
|
|
* @return string |
|
71
|
|
|
* @todo array merge regions with data |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getTemplateFile($filename, $data) |
|
74
|
|
|
{ |
|
75
|
|
|
$data['getRegion'] = function($name) { |
|
76
|
|
|
return $this->getRegion($name); |
|
77
|
|
|
}; // This is for mustache compatibility |
|
78
|
|
|
|
|
79
|
|
|
// Push the data into regions and then pass a pointer to this class to the layout |
|
80
|
|
|
// $this->setRegions($data); |
|
|
|
|
|
|
81
|
|
|
return parent::getTemplateFile($filename, $data); // Pass in layout object to template |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set View Root Folder |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $folder |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setViewRootFolder($folder) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->_viewRootFolder = $folder; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get Theme Root Folder |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $folder |
|
|
|
|
|
|
98
|
|
|
*/ |
|
99
|
|
|
public function getThemeRootFolder() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->_themeRootFolder; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set Theme Root Folder |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $folder |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setThemeRootFolder($folder) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->_themeRootFolder = $folder; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Set theme name |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $themeName |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setThemeName($themeName) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->_theme = $themeName; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get theme name |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getThemeName() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->_theme; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* set region |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $name |
|
136
|
|
|
* @param mixed $content , Typically a string, Container object or other object |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setRegion($name, $content) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->_regions[$name] = $content; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Set all regions at once |
|
145
|
|
|
* |
|
146
|
|
|
* @param array $data , Associative array of containers/strings |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setRegions($data) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->_regions = $data; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get regions |
|
155
|
|
|
* |
|
156
|
|
|
* @return array $this_regions |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getRegions() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->_regions; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* get rendered region |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $name |
|
167
|
|
|
* @return mixed $content, typically a string, Container object or other object |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getRegion($name) |
|
170
|
|
|
{ |
|
171
|
|
|
if(array_key_exists($name, $this->_regions)) { |
|
172
|
|
|
$html = (is_subclass_of($this->_regions[$name], 'erdiko\core\Container')) ? |
|
173
|
|
|
$this->_regions[$name]->toHtml() : $this->_regions[$name]; |
|
174
|
|
|
} else { |
|
175
|
|
|
throw new \Exception("Template region '{$name}' does not exits."); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $html; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Render container to HTML |
|
183
|
|
|
* |
|
184
|
|
|
* @return string $html |
|
185
|
|
|
*/ |
|
186
|
|
|
public function toHtml() |
|
187
|
|
|
{ |
|
188
|
|
|
$filename = $this->getTemplateFolder().$this->getTemplate(); |
|
189
|
|
|
// $data = (is_subclass_of($this->_data, 'erdiko\core\Container')) ? $this->_data->toHtml() : $this->_data; |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
return $this->getTemplateFile($filename, $this->_data); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.