11<?php
22class CoreBoxx {
3- // (A) PROPERTIES & CONSTRUCTOR
4- public $ error = "" ; // error message, if any
3+ // (A) PROPERTIES
4+ public $ loaded = []; // modules loaded
5+ public $ error = "" ; // error message, if any
56 public $ page = null ; // pagination data, if any
6- function __construct () {
7- $ GLOBALS ["_SESS " ] = []; // initialize global session variable
8- }
97
10- // (B) MODULES
11- // (B1) LOAD MODULE
8+ // (B) CONSTRUCTOR - INITIALIZE GLOBAL SESSION VARIABLE
9+ function __construct () { $ GLOBALS ["_SESS " ] = []; }
10+
11+ // (C) MODULES HANDLER
12+ // (C1) IS MODULE LOADED?
13+ // $module : module to check
14+ function loaded ($ module ) { return isset ($ this ->loaded [$ module ]); }
15+
16+ // (C2) LOAD MODULE
1217 // $module : module to load
13- function load ($ module ) { if (!$ this ->loaded ($ module )) {
18+ function load ($ module ) : void {
19+ if ($ this ->loaded ($ module )) { return ; }
1420 $ file = PATH_LIB . "LIB- $ module.php " ;
1521 if (file_exists ($ file )) {
1622 require $ file ;
17- $ this ->$ module = new $ module ($ this );
23+ $ this ->loaded [ $ module] = new $ module ($ this );
1824 } else { throw new Exception ("$ module module not found! " ); }
19- }}
25+ }
2026
21- // (B2) IS MODULE LOADED?
22- // $module : module to check
23- function loaded ($ module ) {
24- return isset ($ this ->$ module ) && is_object ($ this ->$ module );
27+ // (C3) "MAGIC LINK" TO MODULE
28+ function __get ($ name ) {
29+ if (isset ($ this ->loaded [$ name ])) { return $ this ->loaded [$ name ]; }
2530 }
2631
27- // (C ) FUNCTION MAPPING
28- // (C1 ) AUTO MAP $_POST OR $_GET TO MODULE FUNCTION
32+ // (D ) FUNCTION MAPPING
33+ // (D1 ) AUTO MAP $_POST OR $_GET TO MODULE FUNCTION
2934 // $module : module to load
3035 // $function : function to run
3136 // $mode : POST or GET
3237 function autoCall ($ module , $ function , $ mode ="POST " ) {
33- // (C1 -1) LOAD MODULE
38+ // (D1 -1) LOAD MODULE
3439 $ this ->load ($ module );
3540
36- // (C1 -2) MAP POST OR GET?
41+ // (D1 -2) MAP POST OR GET?
3742 if ($ mode =="POST " ) { $ target =& $ _POST ; }
3843 else { $ target =& $ _GET ; }
3944
40- // (C1 -3) GET FUNCTION PARAMETERS
45+ // (D1 -3) GET FUNCTION PARAMETERS
4146 $ reflect = new ReflectionMethod ($ module , $ function );
4247 $ params = $ reflect ->getParameters ();
4348
44- // (C1 -4) EVIL MAPPING
49+ // (D1 -4) EVIL MAPPING
4550 $ evil = "\$results = \$this-> $ module-> $ function( " ;
4651 if (count ($ params )==0 ) { $ evil .= "); " ; }
4752 else {
@@ -61,40 +66,40 @@ function autoCall ($module, $function, $mode="POST") {
6166 $ evil = substr ($ evil , 0 , -1 ) . "); " ;
6267 }
6368
64- // (C1 -5) EVIL RESULTS
69+ // (D1 -5) EVIL RESULTS
6570 eval ($ evil );
6671 return $ results ;
6772 }
6873
69- // (C2 ) AUTO MAP $_POST OR $_GET TO MODULE FUNCTION & API RESPOND
74+ // (D2 ) AUTO MAP $_POST OR $_GET TO MODULE FUNCTION & API RESPOND
7075 // $module : module to load
7176 // $function : function to run
7277 // $mode : POST or GET
73- function autoAPI ($ module , $ function , $ mode ="POST " ) {
78+ function autoAPI ($ module , $ function , $ mode ="POST " ) : void {
7479 $ result = $ this ->autoCall ($ module , $ function , $ mode );
7580 $ this ->respond ($ result , null , null ,
7681 $ this ->DB ->lastID !==null ? $ this ->DB ->lastID : null
7782 );
7883 }
7984
80- // (C3 ) SAME AS ABOVE, BUT FOR "GET ENTRIES" API FUNCTIONS
85+ // (D3 ) SAME AS ABOVE, BUT FOR "GET ENTRIES" API FUNCTIONS
8186 // $module : module to load
8287 // $function : function to run
8388 // $mode : POST or GET
84- function autoGETAPI ($ module , $ function , $ mode ="POST " ) {
89+ function autoGETAPI ($ module , $ function , $ mode ="POST " ) : void {
8590 $ results = $ this ->autoCall ($ module , $ function , $ mode );
8691 $ this ->respond ($ results !==false , null , $ results );
8792 }
8893
89- // (D ) SYSTEM
90- // (D1 ) STANDARD JSON RESPONSE
94+ // (E ) SYSTEM
95+ // (E1 ) STANDARD JSON RESPONSE
9196 // $status : 1 or 0, true or false
9297 // $msg : system message
9398 // $data : optional, data append
9499 // $more : optional, supplementary data
95100 // $http : optional, HTTP response code (401, 403, 500, etc...)
96101 // $exit : stop process, default true
97- function respond ($ status , $ msg =null , $ data =null , $ more =null , $ http =null , $ exit =true ) {
102+ function respond ($ status , $ msg =null , $ data =null , $ more =null , $ http =null , $ exit =true ) : void {
98103 if ($ http !==null ) { http_response_code ($ http ); }
99104 if ($ msg === null ) {
100105 if ($ status ==1 ) { $ msg = "OK " ; }
@@ -107,9 +112,9 @@ function respond ($status, $msg=null, $data=null, $more=null, $http=null, $exit=
107112 if ($ exit ) { exit (); }
108113 }
109114
110- // (D2 ) STANDARD ERROR HANDLER
111- function ouch ($ ex ) {
112- // (D2 -1) OUTPUT JSON ENCODED MESSAGE IN API MODE
115+ // (E2 ) STANDARD ERROR HANDLER
116+ function ouch ($ ex ) : void {
117+ // (E2 -1) OUTPUT JSON ENCODED MESSAGE IN API MODE
113118 if (defined ("API_MODE " )) {
114119 $ this ->respond (0 ,
115120 ERR_SHOW ? $ ex ->getMessage () : "OPPS! An error has occured. " ,
@@ -119,7 +124,7 @@ function ouch ($ex) {
119124 ] : null );
120125 }
121126
122- // (D2 -2) SHOW HTML ERROR MESSAGE IN WEB MODE
127+ // (E2 -2) SHOW HTML ERROR MESSAGE IN WEB MODE
123128 else if (defined ("WEB_MODE " )) { ?>
124129 <div style="box-sizing:border-box;position:fixed;top:0;left:0;width:100vw;height:100vh;z-index:9999;background:#fff;color:#000;padding:30px;font-family:arial">
125130 <h1 style="font-size:50px;padding:0;margin:0">(╯°□°)╯︵ ┻━┻</h1>
@@ -136,7 +141,7 @@ function ouch ($ex) {
136141 </div>
137142 <?php }
138143
139- // (D2 -3) OUTPUT PLAIN TEXT OTHERWISE
144+ // (E2 -3) OUTPUT PLAIN TEXT OTHERWISE
140145 else {
141146 echo "An error has occured. " ;
142147 if (ERR_SHOW ) {
@@ -148,51 +153,56 @@ function ouch ($ex) {
148153 }
149154 }
150155
151- // (E ) OTHER CONVENIENCE
152- // (E1 ) GENERATE RANDOM STRING
156+ // (F ) OTHER CONVENIENCE
157+ // (F1 ) GENERATE RANDOM STRING
153158 // $length : number of bytes
154- function random ($ length =8 ) {
155- return bin2hex (random_bytes ($ length ));
156- }
159+ function random ($ length =8 ) { return bin2hex (random_bytes ($ length )); }
157160
158- // (E2 ) PAGINATION CALCULATOR
161+ // (F2 ) PAGINATION CALCULATOR
159162 // $entries : total number of entries
160163 // $now : current page
161- function paginator ($ entries , $ now =1 ) {
162- // (E2 -1) TOTAL NUMBER OF PAGES
164+ function paginator ($ entries , $ now =1 ) : void {
165+ // (F2 -1) TOTAL NUMBER OF PAGES
163166 $ this ->page = [
164167 "entries " => (int ) $ entries ,
165168 "total " => ceil ($ entries / PAGE_PER )
166169 ];
167170
168- // (E2 -2) CURRENT PAGE
171+ // (F2 -2) CURRENT PAGE
169172 $ this ->page ["now " ] = $ now > $ this ->page ["total " ] ? $ this ->page ["total " ] : $ now ;
170173 if ($ this ->page ["now " ]<=0 ) { $ this ->page ["now " ] = 1 ; }
171174 $ this ->page ["now " ] = (int ) $ this ->page ["now " ];
172175
173- // (E2 -3) LIMIT X,Y
176+ // (F2 -3) LIMIT X,Y
174177 $ this ->page ["x " ] = ($ this ->page ["now " ] - 1 ) * PAGE_PER ;
175178 $ this ->page ["y " ] = PAGE_PER ;
176179 $ this ->page ["lim " ] = " LIMIT {$ this ->page ["x " ]}, {$ this ->page ["y " ]}" ;
177180 }
178181
179- // (E3 ) REDIRECT
180- function redirect ($ page ="" , $ url =HOST_BASE ) {
182+ // (F3 ) REDIRECT
183+ function redirect ($ page ="" , $ url =HOST_BASE ) : void {
181184 header ("Location: $ url$ page " );
182185 exit ();
183186 }
184187}
185188
186- // (F ) ALL LIBRARIES SHOULD EXTEND THIS CORE CLASS
189+ // (G ) ALL MODULES SHOULD EXTEND THIS CORE CLASS
187190class Core {
191+ // (G1) LINK MODULE TO CORE
192+ public $ Core ;
193+ public $ error ;
188194 function __construct ($ core ) {
189- $ this ->core =& $ core ; // Link to core
190- $ this ->error =& $ core ->error ; // Error message
191- if ($ core ->loaded ("DB " )) { $ this ->DB =& $ core ->DB ; } // Link to database module
195+ $ this ->Core =& $ core ;
196+ $ this ->error =& $ core ->error ;
197+ }
198+
199+ // (G2) MAKE MODULES LINKING EASIER
200+ function __get ($ name ) {
201+ if (isset ($ this ->Core ->loaded [$ name ])) { return $ this ->Core ->loaded [$ name ]; }
192202 }
193203}
194204
195- // (G ) CORE OBJECT + GLOBAL ERROR HANDLING
205+ // (H ) CORE OBJECT + GLOBAL ERROR HANDLING
196206$ _CORE = new CoreBoxx ();
197207function _CORERR ($ ex ) { global $ _CORE ; $ _CORE ->ouch ($ ex ); }
198208set_exception_handler ("_CORERR " );
0 commit comments