@@ -1134,11 +1134,15 @@ protected function parseGetParameterArray($get,$name,$characters) {
11341134 return $ values ;
11351135 }
11361136
1137- protected function applyAfterWrite ( $ action , $ table , $ id , $ input ) {
1138- $ callback = $ this -> settings [ ' after_write ' ];
1137+ protected function applyAfterHandler ( $ parameters , $ output ) {
1138+ $ callback = $ parameters [ ' after ' ];
11391139 if (is_callable ($ callback ,true )) {
1140- $ database = $ this ->settings ['database ' ];
1141- $ callback ($ action ,$ database ,$ table ,$ id ,$ input );
1140+ $ action = $ parameters ['action ' ];
1141+ $ database = $ parameters ['database ' ];
1142+ $ table = $ parameters ['tables ' ][0 ];
1143+ $ id = $ parameters ['key ' ][0 ];
1144+ $ input = isset ($ parameters ['inputs ' ])?$ parameters ['inputs ' ]:false ;
1145+ $ callback ($ action ,$ database ,$ table ,$ id ,$ input ,$ output );
11421146 }
11431147 }
11441148
@@ -1265,6 +1269,7 @@ protected function headersCommand($parameters) {
12651269 } else {
12661270 echo json_encode ($ headers );
12671271 }
1272+ return false ;
12681273 }
12691274
12701275 protected function startOutput () {
@@ -1476,7 +1481,6 @@ protected function createObject($input,$tables) {
14761481 $ result = $ this ->db ->query ('INSERT INTO ! ( ' .$ keys .') VALUES ( ' .$ values .') ' ,$ params );
14771482 if (!$ result ) return null ;
14781483 $ insertId = $ this ->db ->insertId ($ result );
1479- $ this ->applyAfterWrite ('create ' ,$ tables [0 ],$ insertId ,$ input );
14801484 return $ insertId ;
14811485 }
14821486
@@ -1513,7 +1517,6 @@ protected function updateObject($key,$input,$filters,$tables) {
15131517 $ this ->addWhereFromFilters ($ filters [$ table ],$ sql ,$ params );
15141518 $ result = $ this ->db ->query ($ sql ,$ params );
15151519 if (!$ result ) return null ;
1516- $ this ->applyAfterWrite ('update ' ,$ tables [0 ],$ key [0 ],$ input );
15171520 return $ this ->db ->affectedRows ($ result );
15181521 }
15191522
@@ -1546,7 +1549,6 @@ protected function deleteObject($key,$filters,$tables) {
15461549 $ this ->addWhereFromFilters ($ filters [$ table ],$ sql ,$ params );
15471550 $ result = $ this ->db ->query ($ sql ,$ params );
15481551 if (!$ result ) return null ;
1549- $ this ->applyAfterWrite ('delete ' ,$ tables [0 ],$ key [0 ],array ());
15501552 return $ this ->db ->affectedRows ($ result );
15511553 }
15521554
@@ -1591,7 +1593,6 @@ protected function incrementObject($key,$input,$filters,$tables,$fields) {
15911593 $ this ->addWhereFromFilters ($ filters [$ table ],$ sql ,$ params );
15921594 $ result = $ this ->db ->query ($ sql ,$ params );
15931595 if (!$ result ) return null ;
1594- $ this ->applyAfterWrite ('increment ' ,$ tables [0 ],$ key [0 ],$ input );
15951596 return $ this ->db ->affectedRows ($ result );
15961597 }
15971598
@@ -1884,7 +1885,7 @@ protected function getParameters($settings) {
18841885 }
18851886 }
18861887
1887- return compact ('action ' ,'database ' ,'tables ' ,'key ' ,'page ' ,'filters ' ,'fields ' ,'orderings ' ,'transform ' ,'multi ' ,'inputs ' ,'collect ' ,'select ' );
1888+ return compact ('action ' ,'database ' ,'tables ' ,'key ' ,'page ' ,'filters ' ,'fields ' ,'orderings ' ,'transform ' ,'multi ' ,'inputs ' ,'collect ' ,'select ' , ' after ' );
18881889 }
18891890
18901891 protected function addWhereFromFilters ($ filters ,&$ sql ,&$ params ) {
@@ -2037,37 +2038,34 @@ protected function readCommand($parameters) {
20372038 if (!$ object ) $ this ->exitWith404 ('object ' );
20382039 $ this ->startOutput ();
20392040 echo json_encode ($ object );
2041+ return false ;
20402042 }
20412043
20422044 protected function createCommand ($ parameters ) {
20432045 extract ($ parameters );
20442046 if (!$ inputs || !$ inputs [0 ]) $ this ->exitWith404 ('input ' );
2045- $ this ->startOutput ();
2046- if ($ multi ) echo json_encode ($ this ->createObjects ($ inputs ,$ tables ));
2047- else echo json_encode ($ this ->createObject ($ inputs [0 ],$ tables ));
2047+ if ($ multi ) return $ this ->createObjects ($ inputs ,$ tables );
2048+ return $ this ->createObject ($ inputs [0 ],$ tables );
20482049 }
20492050
20502051 protected function updateCommand ($ parameters ) {
20512052 extract ($ parameters );
20522053 if (!$ inputs || !$ inputs [0 ]) $ this ->exitWith404 ('subject ' );
2053- $ this ->startOutput ();
2054- if ($ multi ) echo json_encode ($ this ->updateObjects ($ key ,$ inputs ,$ filters ,$ tables ));
2055- else echo json_encode ($ this ->updateObject ($ key ,$ inputs [0 ],$ filters ,$ tables ));
2054+ if ($ multi ) return $ this ->updateObjects ($ key ,$ inputs ,$ filters ,$ tables );
2055+ return $ this ->updateObject ($ key ,$ inputs [0 ],$ filters ,$ tables );
20562056 }
20572057
20582058 protected function deleteCommand ($ parameters ) {
20592059 extract ($ parameters );
2060- $ this ->startOutput ();
2061- if ($ multi ) echo json_encode ($ this ->deleteObjects ($ key ,$ filters ,$ tables ));
2062- else echo json_encode ($ this ->deleteObject ($ key ,$ filters ,$ tables ));
2060+ if ($ multi ) return $ this ->deleteObjects ($ key ,$ filters ,$ tables );
2061+ return $ this ->deleteObject ($ key ,$ filters ,$ tables );
20632062 }
20642063
20652064 protected function incrementCommand ($ parameters ) {
20662065 extract ($ parameters );
20672066 if (!$ inputs || !$ inputs [0 ]) $ this ->exitWith404 ('subject ' );
2068- $ this ->startOutput ();
2069- if ($ multi ) echo json_encode ($ this ->incrementObjects ($ key ,$ inputs ,$ filters ,$ tables ,$ fields ));
2070- else echo json_encode ($ this ->incrementObject ($ key ,$ inputs [0 ],$ filters ,$ tables ,$ fields ));
2067+ if ($ multi ) return $ this ->incrementObjects ($ key ,$ inputs ,$ filters ,$ tables ,$ fields );
2068+ return $ this ->incrementObject ($ key ,$ inputs [0 ],$ filters ,$ tables ,$ fields );
20712069 }
20722070
20732071 protected function listCommand ($ parameters ) {
@@ -2083,6 +2081,7 @@ protected function listCommand($parameters) {
20832081 $ data = json_decode ($ content ,true );
20842082 echo json_encode (self ::php_crud_api_transform ($ data ));
20852083 }
2084+ return false ;
20862085 }
20872086
20882087 protected function retrievePostData () {
@@ -2122,7 +2121,7 @@ public function __construct($config) {
21222121 $ input_validator = isset ($ input_validator )?$ input_validator :null ;
21232122 $ auto_include = isset ($ auto_include )?$ auto_include :null ;
21242123 $ allow_origin = isset ($ allow_origin )?$ allow_origin :null ;
2125- $ after_write = isset ($ after_write )?$ after_write :null ;
2124+ $ after = isset ($ after )?$ after :null ;
21262125
21272126 $ db = isset ($ db )?$ db :null ;
21282127 $ method = isset ($ method )?$ method :null ;
@@ -2174,7 +2173,7 @@ public function __construct($config) {
21742173 }
21752174
21762175 $ this ->db = $ db ;
2177- $ this ->settings = compact ('method ' , 'request ' , 'get ' , 'post ' , 'origin ' , 'database ' , 'table_authorizer ' , 'record_filter ' , 'column_authorizer ' , 'tenancy_function ' , 'input_sanitizer ' , 'input_validator ' , 'after_write ' , 'auto_include ' , 'allow_origin ' );
2176+ $ this ->settings = compact ('method ' , 'request ' , 'get ' , 'post ' , 'origin ' , 'database ' , 'table_authorizer ' , 'record_filter ' , 'column_authorizer ' , 'tenancy_function ' , 'input_sanitizer ' , 'input_validator ' , 'after ' , 'auto_include ' , 'allow_origin ' );
21782177 }
21792178
21802179 public static function php_crud_api_transform (&$ tables ) {
@@ -2627,13 +2626,23 @@ public function executeCommand() {
26272626 } else {
26282627 $ parameters = $ this ->getParameters ($ this ->settings );
26292628 switch ($ parameters ['action ' ]){
2630- case 'list ' : $ this ->listCommand ($ parameters ); break ;
2631- case 'read ' : $ this ->readCommand ($ parameters ); break ;
2632- case 'create ' : $ this ->createCommand ($ parameters ); break ;
2633- case 'update ' : $ this ->updateCommand ($ parameters ); break ;
2634- case 'delete ' : $ this ->deleteCommand ($ parameters ); break ;
2635- case 'increment ' : $ this ->incrementCommand ($ parameters ); break ;
2636- case 'headers ' : $ this ->headersCommand ($ parameters ); break ;
2629+ case 'list ' : $ output = $ this ->listCommand ($ parameters ); break ;
2630+ case 'read ' : $ output = $ this ->readCommand ($ parameters ); break ;
2631+ case 'create ' : $ output = $ this ->createCommand ($ parameters ); break ;
2632+ case 'update ' : $ output = $ this ->updateCommand ($ parameters ); break ;
2633+ case 'delete ' : $ output = $ this ->deleteCommand ($ parameters ); break ;
2634+ case 'increment ' : $ output = $ this ->incrementCommand ($ parameters ); break ;
2635+ case 'headers ' : $ output = $ this ->headersCommand ($ parameters ); break ;
2636+ default : $ output = false ;
2637+ }
2638+ if ($ output !==false ) {
2639+ $ this ->startOutput ();
2640+ echo json_encode ($ output );
2641+ }
2642+ if ($ parameters ['after ' ]) {
2643+ if ($ output !==null ) {
2644+ $ this ->applyAfterHandler ($ parameters ,$ output );
2645+ }
26372646 }
26382647 }
26392648 }
0 commit comments