33class Pajax
44{
55
6+ const GATEWAY_DAPP = "PeC85pqFgRxmevonG6diUwT4AfF7YUPSm3 " ;
7+
68 static $ options ;
79 static $ data ;
810 static $ ajax = false ;
911 static $ class ;
12+ static $ scripts = [];
13+ static $ process ;
1014
1115 static function app ($ class , $ options = [])
1216 {
13- self ::$ options = $ options ;
1417 self ::$ class = new $ class ();
18+ self ::$ options = $ options ;
1519 self ::processAjax ();
20+ try {
1621 self ::render ();
22+ } catch (Exception $ t ) {
23+ print_r ($ t );
24+ }
1725
1826 }
1927
@@ -26,6 +34,7 @@ static function processAjax()
2634 self ::$ options = json_decode (base64_decode ($ pAjax ['options ' ]), true );
2735 $ action = $ pAjax ['action ' ];
2836 $ actionData = $ pAjax ['actionData ' ];
37+ self ::$ process = $ pAjax ['process ' ];
2938 if (!class_exists ($ class )) {
3039 $ class_file = dirname ($ _SERVER ['SCRIPT_FILENAME ' ]) ."/inc/class/ $ class.php " ;
3140 if (file_exists ($ class_file )) {
@@ -44,6 +53,7 @@ static function processAjax()
4453 self ::$ class ->$ k = $ v ;
4554 }
4655 }
56+ try {
4757 if (method_exists (self ::$ class , $ action )) {
4858 if (empty ($ actionData )) {
4959 call_user_func ([self ::$ class , $ action ]);
@@ -56,7 +66,6 @@ static function processAjax()
5666 }
5767 ob_clean ();
5868 ob_start ();
59- try {
6069 self ::render ();
6170 $ content = ob_get_contents ();
6271 if (self ::$ class ) {
@@ -66,7 +75,7 @@ static function processAjax()
6675 }
6776 ob_end_clean ();
6877 header ('Content-Type: application/json ' );
69- echo json_encode (['content ' => $ content , 'data ' => $ data ]);
78+ echo json_encode (['content ' => $ content , 'data ' => $ data, ' scripts ' => self :: $ scripts ]);
7079 } catch (Throwable $ t ) {
7180 ob_end_clean ();
7281 header ('Content-Type: application/json ' );
@@ -76,13 +85,70 @@ static function processAjax()
7685 }
7786 }
7887
88+ static function redirect ($ redirect , $ js =true ) {
89+ if ($ js ) {
90+ header ('Content-Type: application/json ' );
91+ echo json_encode (['redirect ' => $ redirect ]);
92+ exit ;
93+ } else {
94+ header ("location: $ redirect " );
95+ exit ;
96+ }
97+ }
98+
99+ static function executeScript ($ method , ...$ params ) {
100+ self ::$ scripts [] = ['method ' =>$ method , 'params ' =>$ params ];
101+ }
102+
103+ static function login ($ appName , $ redirect ) {
104+ $ request_code = uniqid ();
105+ $ _SESSION ['request_code ' ] = $ request_code ;
106+ $ url = '/dapps.php?url= ' .self ::GATEWAY_DAPP .'/gateway/auth.php?app= ' .$ appName .'&request_code= ' . $ request_code . '&redirect= ' . $ redirect ;
107+ self ::redirect ($ url );
108+ }
109+
110+
111+ static function logout ($ redirect ) {
112+ session_destroy ();
113+ self ::redirect ($ redirect );
114+ }
115+
116+
117+ static function handleAuth ($ redirect , $ callback = null ) {
118+ if (isset ($ _GET ['auth_data ' ])) {
119+ $ auth_data = json_decode (base64_decode ($ _GET ['auth_data ' ]), true );
120+ if ($ auth_data ['request_code ' ] == $ _SESSION ['request_code ' ]) {
121+ $ _SESSION ['account ' ] = $ auth_data ['account ' ];
122+ if ($ callback != null && is_callable ($ callback )) {
123+ call_user_func ($ callback , $ auth_data );
124+ }
125+ self ::redirect ($ redirect , false );
126+ }
127+ }
128+ }
129+
79130 static function render ()
80131 {
132+ set_error_handler (function ($ errno , $ errstr , $ errfile , $ errline ) {
133+ if (in_array ($ errno , [E_ERROR , E_USER_ERROR , E_RECOVERABLE_ERROR ])) {
134+ throw new \ErrorException ($ errstr , 0 , $ errno , $ errfile , $ errline );
135+ }
136+ error_log ("PHP Warning: [ $ errno] $ errstr in $ errfile on line $ errline " );
137+ return false ;
138+ });
81139 $ view = self ::getViewId ();
82140 ob_flush ();
83141 ob_clean ();
84142 ob_start ();
143+ try {
85144 call_user_func (self ::getTemplate ());
145+ } catch (Throwable $ t ) {
146+ echo "Caught warning from reflected getTemplate(): " . $ t ->getMessage ().'<br/> ' ;
147+ echo $ t ->getFile () .": " . $ t ->getLine ().'<br/> ' ;
148+ echo $ t ->getTraceAsString ();
149+ } finally {
150+ restore_error_handler ();
151+ }
86152 $ body = ob_get_contents ();
87153 ob_clean ();
88154 ob_start ();
@@ -140,6 +206,13 @@ public static function component($class, $props = []) {
140206 call_user_func ($ template );
141207 }
142208
209+ public static function block ($ name , Closure $ closure ) {
210+ if (!self ::$ ajax || (self ::$ ajax && empty (self ::$ process ) || (!empty (self ::$ process ) && in_array ($ name , self ::$ process )))) {
211+ $ class = self ::$ class ;
212+ $ closure ->call ($ class );
213+ }
214+ }
215+
143216}
144217
145218function pd ($ name , ...$ val )
0 commit comments