1515
1616namespace FloatPHP \Kernel ;
1717
18- use FloatPHP \Classes \{
19- Filesystem \ Exception as ErrorHandler ,
18+ use FloatPHP \Interfaces \ Kernel \{
19+ ViewInterface , CallableInterface
2020};
21- use FloatPHP \Helpers \Html \Template ;
2221
23- class View extends Base
22+ class View extends Base implements ViewInterface
2423{
24+ use \FloatPHP \Helpers \Framework \inc \TraitViewable;
25+
2526 /**
26- * @access protected
27+ * @access private
2728 * @var array $callables
2829 * @var array $content
2930 */
30- protected $ callables = false ;
31- protected $ content = [];
31+ private $ callables = [] ;
32+ private $ content = [];
3233
3334 /**
34- * Define custom callables.
35- *
36- * @access protected
37- * @param array $callables
38- * @return void
35+ * @inheritdoc
3936 */
40- protected function setCallables (array $ callables = [] )
37+ public function setCallables (? CallableInterface $ callable = null )
4138 {
42- $ this ->callables = $ callables ;
39+ $ default = $ this ->getDefaultCallables ();
40+ $ callables = ($ callable ) ? $ callable ->getCallables () : [];
41+ $ this ->callables = $ this ->mergeArray ($ default , $ callables );
4342 }
4443
4544 /**
@@ -55,176 +54,177 @@ protected function setContent(array $content = [])
5554 }
5655
5756 /**
58- * Render view.
59- *
60- * @access protected
61- * @param array $content
62- * @param string $tpl
63- * @return void
57+ * @inheritdoc
6458 */
65- protected function render ($ content = [], $ tpl = ' system/default ' )
59+ public function render (string $ file = ' default ' , array $ content = [], bool $ end = false )
6660 {
67- echo $ this ->assign ($ content , $ tpl );
61+ echo $ this ->assign ($ file , $ content );
62+ if ( $ end ) {
63+ die;
64+ }
6865 }
6966
70- /**
71- * Aassign content to view.
72- *
73- * @access protected
74- * @param array $content
75- * @param string $tpl
76- * @return string
67+ /**
68+ * @inheritdoc
7769 */
78- protected function assign ($ content = [], $ tpl = ' system/default ' ) : string
70+ public function assign (string $ file = ' default ' , array $ content = [] ) : string
7971 {
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 (), [
8374 'cache ' => "{$ this ->getCachePath ()}/view " ,
8475 'debug ' => $ this ->isDebug ()
8576 ]);
8677
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 ();
9281 }
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- }));
17482
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+ }
21487
21588 // Return rendered view
21689 try {
217- $ content = $ this ->mergeArray ($ this ->content , $ content );
218- $ view = $ env ->load ("{$ tpl }{$ this ->getViewExtension ()}" );
90+ $ view = $ env ->load ("{$ file }{$ this ->getViewExtension ()}" );
21991 return $ view ->render ($ content );
22092
221- } catch (\Exception $ e ) {
93+ } catch (\Exception | \ RuntimeException $ e ) {
22294 if ( $ this ->isDebug () ) {
22395 die ($ e ->getMessage ());
22496 }
225- ErrorHandler:: clearLastError ();
97+ $ this -> clearLastError ();
22698 }
22799
228100 return '{} ' ;
229101 }
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+ }
230230}
0 commit comments