15
15
16
16
namespace FloatPHP \Kernel ;
17
17
18
- use FloatPHP \Classes \{
19
- Filesystem \ Exception as ErrorHandler ,
18
+ use FloatPHP \Interfaces \ Kernel \{
19
+ ViewInterface , CallableInterface
20
20
};
21
- use FloatPHP \Helpers \Html \Template ;
22
21
23
- class View extends Base
22
+ class View extends Base implements ViewInterface
24
23
{
24
+ use \FloatPHP \Helpers \Framework \inc \TraitViewable;
25
+
25
26
/**
26
- * @access protected
27
+ * @access private
27
28
* @var array $callables
28
29
* @var array $content
29
30
*/
30
- protected $ callables = false ;
31
- protected $ content = [];
31
+ private $ callables = [] ;
32
+ private $ content = [];
32
33
33
34
/**
34
- * Define custom callables.
35
- *
36
- * @access protected
37
- * @param array $callables
38
- * @return void
35
+ * @inheritdoc
39
36
*/
40
- protected function setCallables (array $ callables = [] )
37
+ public function setCallables (? CallableInterface $ callable = null )
41
38
{
42
- $ this ->callables = $ callables ;
39
+ $ default = $ this ->getDefaultCallables ();
40
+ $ callables = ($ callable ) ? $ callable ->getCallables () : [];
41
+ $ this ->callables = $ this ->mergeArray ($ default , $ callables );
43
42
}
44
43
45
44
/**
@@ -55,176 +54,177 @@ protected function setContent(array $content = [])
55
54
}
56
55
57
56
/**
58
- * Render view.
59
- *
60
- * @access protected
61
- * @param array $content
62
- * @param string $tpl
63
- * @return void
57
+ * @inheritdoc
64
58
*/
65
- protected function render ($ content = [], $ tpl = ' system/default ' )
59
+ public function render (string $ file = ' default ' , array $ content = [], bool $ end = false )
66
60
{
67
- echo $ this ->assign ($ content , $ tpl );
61
+ echo $ this ->assign ($ file , $ content );
62
+ if ( $ end ) {
63
+ die;
64
+ }
68
65
}
69
66
70
- /**
71
- * Aassign content to view.
72
- *
73
- * @access protected
74
- * @param array $content
75
- * @param string $tpl
76
- * @return string
67
+ /**
68
+ * @inheritdoc
77
69
*/
78
- protected function assign ($ content = [], $ tpl = ' system/default ' ) : string
70
+ public function assign (string $ file = ' default ' , array $ content = [] ) : string
79
71
{
80
- // Set View environment
81
- $ path = $ this ->applyFilter ('view-path ' , $ this ->getViewPath ());
82
- $ env = Template::getEnvironment ($ path , [
72
+ // Get environment
73
+ $ env = $ this ->getEnvironment ($ this ->getPath (), [
83
74
'cache ' => "{$ this ->getCachePath ()}/view " ,
84
75
'debug ' => $ this ->isDebug ()
85
76
]);
86
77
87
- // Set custom callables
88
- if ( $ this ->callables ) {
89
- foreach ($ this ->callables as $ name => $ callable ) {
90
- $ env ->addFunction (Template::extend ($ name , $ callable ));
91
- }
78
+ // Set callables
79
+ if ( !$ this ->callables ) {
80
+ $ this ->setCallables ();
92
81
}
93
-
94
- // Add view global functions
95
- $ env ->addFunction (Template::extend ('dump ' , function ($ var ) {
96
- var_dump ($ var );
97
- }));
98
-
99
- $ env ->addFunction (Template::extend ('die ' , function ($ var = null ) {
100
- die ($ var );
101
- }));
102
-
103
- $ env ->addFunction (Template::extend ('exit ' , function ($ status = null ) {
104
- exit ($ status );
105
- }));
106
-
107
- $ env ->addFunction (Template::extend ('getSession ' , function ($ var = null ) {
108
- return $ this ->getSession ($ var );
109
- }));
110
-
111
- $ env ->addFunction (Template::extend ('hasRole ' , function ($ role = null , $ userId = null ) {
112
- return $ this ->hasRole ($ role , $ userId );
113
- }));
114
-
115
- $ env ->addFunction (Template::extend ('hasCapability ' , function ($ capability = null , $ userId = null ) {
116
- return $ this ->hasCapability ($ capability , $ userId );
117
- }));
118
-
119
- $ env ->addFunction (Template::extend ('getLanguage ' , function () {
120
- return $ this ->getLanguage ();
121
- }));
122
-
123
- $ env ->addFunction (Template::extend ('isValidSession ' , function () {
124
- return $ this ->isValidSession ();
125
- }));
126
-
127
- $ env ->addFunction (Template::extend ('isDebug ' , function () {
128
- return $ this ->isDebug ();
129
- }));
130
-
131
- $ env ->addFunction (Template::extend ('getConfig ' , function ($ config = '' ) {
132
- return $ this ->getConfig ($ config );
133
- }));
134
-
135
- $ env ->addFunction (Template::extend ('getRoot ' , function () {
136
- return $ this ->getRoot ();
137
- }));
138
-
139
- $ env ->addFunction (Template::extend ('getBaseRoute ' , function () {
140
- return $ this ->getBaseRoute (false );
141
- }));
142
-
143
- $ env ->addFunction (Template::extend ('getBaseUrl ' , function () {
144
- return $ this ->getBaseUrl ();
145
- }));
146
-
147
- $ env ->addFunction (Template::extend ('getAssetUrl ' , function () {
148
- return $ this ->getAssetUrl ();
149
- }));
150
-
151
- $ env ->addFunction (Template::extend ('getFrontUploadUrl ' , function () {
152
- return $ this ->getFrontUploadUrl ();
153
- }));
154
-
155
- $ env ->addFunction (Template::extend ('getLoginUrl ' , function () {
156
- return $ this ->getLoginUrl ();
157
- }));
158
-
159
- $ env ->addFunction (Template::extend ('getAdminUrl ' , function () {
160
- return $ this ->getAdminUrl ();
161
- }));
162
-
163
- $ env ->addFunction (Template::extend ('getVerifyUrl ' , function () {
164
- return $ this ->getVerifyUrl ();
165
- }));
166
-
167
- $ env ->addFunction (Template::extend ('getTimeout ' , function () {
168
- return $ this ->getTimeout ();
169
- }));
170
-
171
- $ env ->addFunction (Template::extend ('getToken ' , function ($ source = '' ) {
172
- return $ this ->getToken ($ source );
173
- }));
174
82
175
- $ env ->addFunction (Template::extend ('decodeJSON ' , function ($ json = '' ) {
176
- return $ this ->decodeJson ($ json );
177
- }));
178
-
179
- $ env ->addFunction (Template::extend ('encodeJSON ' , function ($ array = []) {
180
- return $ this ->encodeJson ($ array );
181
- }));
182
-
183
- $ env ->addFunction (Template::extend ('serialize ' , function ($ data = []) {
184
- return $ this ->serialize ($ data );
185
- }));
186
-
187
- $ env ->addFunction (Template::extend ('unserialize ' , function ($ string = '' ) {
188
- return $ this ->unserialize ($ string );
189
- }));
190
-
191
- $ env ->addFunction (Template::extend ('doAction ' , function ($ hook = '' , $ args = []) {
192
- $ this ->doAction ($ hook , $ args );
193
- }));
194
-
195
- $ env ->addFunction (Template::extend ('hasAction ' , function ($ hook = '' , $ args = []) {
196
- $ this ->hasAction ($ hook , $ args );
197
- }));
198
-
199
- $ env ->addFunction (Template::extend ('applyFilter ' , function ($ hook = '' , $ method = '' ) {
200
- return $ this ->applyFilter ($ hook , $ method );
201
- }));
202
-
203
- $ env ->addFunction (Template::extend ('hasFilter ' , function ($ hook = '' , $ method = '' ) {
204
- return $ this ->hasFilter ($ hook , $ method );
205
- }));
206
-
207
- $ env ->addFunction (Template::extend ('doShortcode ' , function ($ shortcode = '' , $ ignoreHTML = false ) {
208
- return $ this ->doShortcode ($ shortcode , $ ignoreHTML );
209
- }));
210
-
211
- $ env ->addFunction (Template::extend ('translate ' , function ($ string = '' ) {
212
- return $ this ->translate ($ string );
213
- }));
83
+ // Load callables
84
+ foreach ($ this ->callables as $ name => $ callable ) {
85
+ $ env ->addFunction ($ this ->extend ($ name , $ callable ));
86
+ }
214
87
215
88
// Return rendered view
216
89
try {
217
- $ content = $ this ->mergeArray ($ this ->content , $ content );
218
- $ view = $ env ->load ("{$ tpl }{$ this ->getViewExtension ()}" );
90
+ $ view = $ env ->load ("{$ file }{$ this ->getViewExtension ()}" );
219
91
return $ view ->render ($ content );
220
92
221
- } catch (\Exception $ e ) {
93
+ } catch (\Exception | \ RuntimeException $ e ) {
222
94
if ( $ this ->isDebug () ) {
223
95
die ($ e ->getMessage ());
224
96
}
225
- ErrorHandler:: clearLastError ();
97
+ $ this -> clearLastError ();
226
98
}
227
99
228
100
return '{} ' ;
229
101
}
102
+
103
+ /**
104
+ * Get default callables.
105
+ *
106
+ * @access protected
107
+ * @return array
108
+ */
109
+ protected function getDefaultCallables () : array
110
+ {
111
+ $ global = [
112
+ 'dump ' => function ($ var ) {
113
+ var_dump ($ var );
114
+ },
115
+ 'die ' => function (?string $ string = null ) {
116
+ die ($ string );
117
+ },
118
+ 'isDebug ' => function () : bool {
119
+ return $ this ->isDebug ();
120
+ },
121
+ 'getConfig ' => function (?string $ key = null ) {
122
+ return $ this ->getConfig ($ key );
123
+ },
124
+ 'getRoot ' => function (?string $ sub = null ) : string {
125
+ return $ this ->getRoot ($ sub );
126
+ },
127
+ 'getBaseRoute ' => function () {
128
+ return $ this ->getBaseRoute (false );
129
+ },
130
+ 'getFrontUploadUrl ' => function () {
131
+ return $ this ->getFrontUploadUrl ();
132
+ },
133
+ 'getLoginUrl ' => function () {
134
+ return $ this ->getLoginUrl ();
135
+ },
136
+ 'getAdminUrl ' => function () {
137
+ return $ this ->getAdminUrl ();
138
+ },
139
+ 'getVerifyUrl ' => function () {
140
+ return $ this ->getVerifyUrl ();
141
+ },
142
+ 'getBaseUrl ' => function () : string {
143
+ return $ this ->getBaseUrl ();
144
+ },
145
+ 'getAssetUrl ' => function () : string {
146
+ return $ this ->getAssetUrl ();
147
+ },
148
+ 'getTimeout ' => function () {
149
+ return $ this ->getTimeout ();
150
+ },
151
+ 'getToken ' => function ($ source = '' ) {
152
+ return $ this ->getToken ($ source );
153
+ },
154
+ 'getLanguage ' => function () {
155
+ return $ this ->getLanguage ();
156
+ },
157
+ 'isValidSession ' => function () {
158
+ return $ this ->isValidSession ();
159
+ },
160
+ 'hasRole ' => function ($ role = null , $ userId = null ) {
161
+ return $ this ->hasRole ($ role , $ userId );
162
+ },
163
+ 'hasCapability ' => function ($ capability = null , $ userId = null ) {
164
+ return $ this ->hasCapability ($ capability , $ userId );
165
+ },
166
+ 'translate ' => function (?string $ string ) : string {
167
+ return $ this ->translate ($ string );
168
+ },
169
+ 'translateVar ' => function (string $ string , $ vars = null ) : string {
170
+ return $ this ->translateVar ($ string , $ vars );
171
+ },
172
+ 'unJson ' => function (string $ value , bool $ isArray = false ) {
173
+ return $ this ->decodeJson ($ value , $ isArray );
174
+ },
175
+ 'toJson ' => function ($ value ) {
176
+ return $ this ->encodeJson ($ value );
177
+ },
178
+ 'serialize ' => function ($ value ) {
179
+ return $ this ->serialize ($ value );
180
+ },
181
+ 'unserialize ' => function (string $ value ) {
182
+ return $ this ->unserialize ($ value );
183
+ },
184
+ 'limitString ' => function (?string $ string , int $ limit ) {
185
+ return $ this ->limitString ($ string , $ limit );
186
+ },
187
+ 'hasFilter ' => function (string $ hook , $ callback = false ) {
188
+ return $ this ->hasFilter ($ hook , $ callback );
189
+ },
190
+ 'applyFilter ' => function (string $ hook , $ value , ...$ args ) {
191
+ return $ this ->applyFilter ($ hook , $ value , ...$ args );
192
+ },
193
+ 'hasAction ' => function (string $ hook , $ callback = false ) {
194
+ return $ this ->hasAction ($ hook , $ callback );
195
+ },
196
+ 'doAction ' => function (string $ hook , ...$ args ) {
197
+ $ this ->doAction ($ hook , ...$ args );
198
+ },
199
+ 'doShortcode ' => function ($ shortcode = '' , $ ignoreHTML = false ) {
200
+ $ this ->doShortcode ($ shortcode , $ ignoreHTML );
201
+ }
202
+ ];
203
+
204
+ if ( $ this ->isAdmin () ) {
205
+ return $ this ->mergeArray ([], $ global );
206
+ }
207
+
208
+ return $ this ->mergeArray ([
209
+ 'exit ' => function (?int $ status = null ) {
210
+ exit ($ status );
211
+ },
212
+ 'getSession ' => function (?string $ key = null ) {
213
+ return $ this ->getSession ($ key );
214
+ }
215
+ ], $ global );
216
+ }
217
+
218
+ /**
219
+ * Get template path (Overridden),
220
+ * [Filter: template-path].
221
+ *
222
+ * @access protected
223
+ * @return mixed
224
+ */
225
+ protected function getPath ()
226
+ {
227
+ $ path = $ this ->getViewPath ();
228
+ return $ this ->applyFilter ('template-path ' , $ path );
229
+ }
230
230
}
0 commit comments