1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Ajde_Template extends Ajde_Object_Standard |
4
|
|
|
{ |
5
|
|
|
protected $_contents = null; |
6
|
|
|
protected $_table = []; |
7
|
|
|
|
8
|
|
|
public function __construct($base, $action, $format = 'html') |
9
|
|
|
{ |
10
|
|
|
$this->set('base', $base); |
11
|
|
|
$this->set('action', $action); |
12
|
|
|
$this->set('format', $format); |
13
|
|
|
$this->setFileinfo(); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
protected function setFileinfo() |
17
|
|
|
{ |
18
|
|
|
if (($fileInfo = $this->getFileInfo()) === false) { |
19
|
|
|
$exception = new Ajde_Core_Exception_Routing(sprintf('Template file in %s, |
20
|
|
|
for action %s with format %s not found', |
21
|
|
|
$this->getBase(), $this->getAction(), $this->getFormat()), 90010); |
22
|
|
|
Ajde::routingError($exception); |
23
|
|
|
} |
24
|
|
|
$className = 'Ajde_Template_Parser_'.$fileInfo['parser']; |
25
|
|
|
$parser = new $className($this); |
26
|
|
|
|
27
|
|
|
$this->setFilename($fileInfo['filename']); |
28
|
|
|
$this->setParser($parser); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setBase($base) |
32
|
|
|
{ |
33
|
|
|
$this->set('base', $base); |
34
|
|
|
$this->setFileinfo(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setAction($action) |
38
|
|
|
{ |
39
|
|
|
$this->set('action', $action); |
40
|
|
|
$this->setFileinfo(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setFormat($format) |
44
|
|
|
{ |
45
|
|
|
$this->set('format', $format); |
46
|
|
|
$this->setFileinfo(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
public function __fallback($method, $arguments) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$helper = $this->getParser()->getHelper(); |
52
|
|
|
if (method_exists($helper, $method)) { |
53
|
|
|
return call_user_func_array([$helper, $method], $arguments); |
54
|
|
|
} else { |
55
|
|
|
throw new Ajde_Exception('Call to undefined method '.get_class($this)."::$method()", 90006); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function getFileInfo() |
60
|
|
|
{ |
61
|
|
|
return $this->_getFileInfo($this->getBase(), $this->getAction(), $this->getFormat()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private static function _getFileInfo($base, $action, $format = 'html') |
65
|
|
|
{ |
66
|
|
|
// go see what templates are available |
67
|
|
|
$dirPrefixPatterns = [ |
68
|
|
|
APP_DIR, |
69
|
|
|
CORE_DIR, |
70
|
|
|
]; |
71
|
|
|
$fileNamePatterns = [ |
72
|
|
|
$action.'.'.$format, |
73
|
|
|
$action, |
74
|
|
|
]; |
75
|
|
|
$fileTypes = [ |
76
|
|
|
'phtml' => 'Phtml', |
77
|
|
|
'xhtml' => 'Xhtml', |
78
|
|
|
]; |
79
|
|
|
foreach ($dirPrefixPatterns as $dirPrefixPattern) { |
80
|
|
|
$prefixedBase = $dirPrefixPattern.$base; |
81
|
|
|
foreach ($fileNamePatterns as $fileNamePattern) { |
82
|
|
|
foreach ($fileTypes as $fileType => $parserType) { |
83
|
|
|
$filePattern = $fileNamePattern.'.'.$fileType; |
84
|
|
|
if (!substr_count($prefixedBase, DIRECTORY_SEPARATOR.'layout'.DIRECTORY_SEPARATOR)) { |
85
|
|
|
$layoutDir = 'layout.'.Ajde::app()->getDocument()->getLayout()->getName().DIRECTORY_SEPARATOR; |
86
|
|
View Code Duplication |
if ($fileMatch = Ajde_Fs_Find::findFile($prefixedBase.TEMPLATE_DIR.$layoutDir, |
|
|
|
|
87
|
|
|
$filePattern) |
88
|
|
|
) { |
89
|
|
|
return ['filename' => $fileMatch, 'parser' => $parserType]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
View Code Duplication |
if ($fileMatch = Ajde_Fs_Find::findFile($prefixedBase.TEMPLATE_DIR, $filePattern)) { |
|
|
|
|
93
|
|
|
return ['filename' => $fileMatch, 'parser' => $parserType]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function setParser(Ajde_Template_Parser $parser) |
103
|
|
|
{ |
104
|
|
|
$this->set('parser', $parser); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return Ajde_Template_Parser |
109
|
|
|
*/ |
110
|
|
|
public function getParser() |
111
|
|
|
{ |
112
|
|
|
return $this->get('parser'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function exist($base, $action, $format = 'html') |
116
|
|
|
{ |
117
|
|
|
return self::_getFileInfo($base, $action, $format); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function setFilename($filename) |
121
|
|
|
{ |
122
|
|
|
$this->set('filename', $filename); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getFilename() |
126
|
|
|
{ |
127
|
|
|
return $this->get('filename'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getBase() |
131
|
|
|
{ |
132
|
|
|
return $this->get('base'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getAction() |
136
|
|
|
{ |
137
|
|
|
return $this->get('action'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getFormat() |
141
|
|
|
{ |
142
|
|
|
return $this->get('format'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function assign($key, $value) |
146
|
|
|
{ |
147
|
|
|
$this->_table[$key] = $value; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function assignArray($array) |
151
|
|
|
{ |
152
|
|
|
foreach ($array as $key => $value) { |
153
|
|
|
$this->assign($key, $value); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function hasAssigned($key) |
158
|
|
|
{ |
159
|
|
|
return array_key_exists($key, $this->_table); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getAssigned($key) |
163
|
|
|
{ |
164
|
|
|
return $this->_table[$key]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getAllAssigned() |
168
|
|
|
{ |
169
|
|
|
return $this->_table; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getContents() |
173
|
|
|
{ |
174
|
|
|
if (!isset($this->_contents)) { |
175
|
|
|
Ajde_Event::trigger($this, 'beforeGetContents'); |
176
|
|
|
Ajde_Cache::getInstance()->addFile($this->getFilename()); |
177
|
|
|
$contents = $this->getParser()->parse($this); |
|
|
|
|
178
|
|
|
$this->setContents($contents); |
179
|
|
|
Ajde_Event::trigger($this, 'afterGetContents'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this->_contents; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Alias for $this->getContents(). |
187
|
|
|
* |
188
|
|
|
* @see self::getContents() |
189
|
|
|
*/ |
190
|
|
|
public function render() |
191
|
|
|
{ |
192
|
|
|
return $this->getContents(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function setContents($contents) |
196
|
|
|
{ |
197
|
|
|
$this->_contents = $contents; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function getDefaultResourcePosition() |
201
|
|
|
{ |
202
|
|
|
return Ajde_Document_Format_Html::RESOURCE_POSITION_DEFAULT; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.