@@ -1134,11 +1134,15 @@ protected function parseGetParameterArray($get,$name,$characters) {
1134
1134
return $ values ;
1135
1135
}
1136
1136
1137
- protected function applyAfterWrite ( $ action , $ table , $ id , $ input ) {
1138
- $ callback = $ this -> settings [ ' after_write ' ];
1137
+ protected function applyAfterHandler ( $ parameters , $ output ) {
1138
+ $ callback = $ parameters [ ' after ' ];
1139
1139
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 );
1142
1146
}
1143
1147
}
1144
1148
@@ -1265,6 +1269,7 @@ protected function headersCommand($parameters) {
1265
1269
} else {
1266
1270
echo json_encode ($ headers );
1267
1271
}
1272
+ return false ;
1268
1273
}
1269
1274
1270
1275
protected function startOutput () {
@@ -1476,7 +1481,6 @@ protected function createObject($input,$tables) {
1476
1481
$ result = $ this ->db ->query ('INSERT INTO ! ( ' .$ keys .') VALUES ( ' .$ values .') ' ,$ params );
1477
1482
if (!$ result ) return null ;
1478
1483
$ insertId = $ this ->db ->insertId ($ result );
1479
- $ this ->applyAfterWrite ('create ' ,$ tables [0 ],$ insertId ,$ input );
1480
1484
return $ insertId ;
1481
1485
}
1482
1486
@@ -1513,7 +1517,6 @@ protected function updateObject($key,$input,$filters,$tables) {
1513
1517
$ this ->addWhereFromFilters ($ filters [$ table ],$ sql ,$ params );
1514
1518
$ result = $ this ->db ->query ($ sql ,$ params );
1515
1519
if (!$ result ) return null ;
1516
- $ this ->applyAfterWrite ('update ' ,$ tables [0 ],$ key [0 ],$ input );
1517
1520
return $ this ->db ->affectedRows ($ result );
1518
1521
}
1519
1522
@@ -1546,7 +1549,6 @@ protected function deleteObject($key,$filters,$tables) {
1546
1549
$ this ->addWhereFromFilters ($ filters [$ table ],$ sql ,$ params );
1547
1550
$ result = $ this ->db ->query ($ sql ,$ params );
1548
1551
if (!$ result ) return null ;
1549
- $ this ->applyAfterWrite ('delete ' ,$ tables [0 ],$ key [0 ],array ());
1550
1552
return $ this ->db ->affectedRows ($ result );
1551
1553
}
1552
1554
@@ -1591,7 +1593,6 @@ protected function incrementObject($key,$input,$filters,$tables,$fields) {
1591
1593
$ this ->addWhereFromFilters ($ filters [$ table ],$ sql ,$ params );
1592
1594
$ result = $ this ->db ->query ($ sql ,$ params );
1593
1595
if (!$ result ) return null ;
1594
- $ this ->applyAfterWrite ('increment ' ,$ tables [0 ],$ key [0 ],$ input );
1595
1596
return $ this ->db ->affectedRows ($ result );
1596
1597
}
1597
1598
@@ -1884,7 +1885,7 @@ protected function getParameters($settings) {
1884
1885
}
1885
1886
}
1886
1887
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 ' );
1888
1889
}
1889
1890
1890
1891
protected function addWhereFromFilters ($ filters ,&$ sql ,&$ params ) {
@@ -2037,37 +2038,34 @@ protected function readCommand($parameters) {
2037
2038
if (!$ object ) $ this ->exitWith404 ('object ' );
2038
2039
$ this ->startOutput ();
2039
2040
echo json_encode ($ object );
2041
+ return false ;
2040
2042
}
2041
2043
2042
2044
protected function createCommand ($ parameters ) {
2043
2045
extract ($ parameters );
2044
2046
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 );
2048
2049
}
2049
2050
2050
2051
protected function updateCommand ($ parameters ) {
2051
2052
extract ($ parameters );
2052
2053
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 );
2056
2056
}
2057
2057
2058
2058
protected function deleteCommand ($ parameters ) {
2059
2059
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 );
2063
2062
}
2064
2063
2065
2064
protected function incrementCommand ($ parameters ) {
2066
2065
extract ($ parameters );
2067
2066
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 );
2071
2069
}
2072
2070
2073
2071
protected function listCommand ($ parameters ) {
@@ -2083,6 +2081,7 @@ protected function listCommand($parameters) {
2083
2081
$ data = json_decode ($ content ,true );
2084
2082
echo json_encode (self ::php_crud_api_transform ($ data ));
2085
2083
}
2084
+ return false ;
2086
2085
}
2087
2086
2088
2087
protected function retrievePostData () {
@@ -2122,7 +2121,7 @@ public function __construct($config) {
2122
2121
$ input_validator = isset ($ input_validator )?$ input_validator :null ;
2123
2122
$ auto_include = isset ($ auto_include )?$ auto_include :null ;
2124
2123
$ allow_origin = isset ($ allow_origin )?$ allow_origin :null ;
2125
- $ after_write = isset ($ after_write )?$ after_write :null ;
2124
+ $ after = isset ($ after )?$ after :null ;
2126
2125
2127
2126
$ db = isset ($ db )?$ db :null ;
2128
2127
$ method = isset ($ method )?$ method :null ;
@@ -2174,7 +2173,7 @@ public function __construct($config) {
2174
2173
}
2175
2174
2176
2175
$ 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 ' );
2178
2177
}
2179
2178
2180
2179
public static function php_crud_api_transform (&$ tables ) {
@@ -2627,13 +2626,23 @@ public function executeCommand() {
2627
2626
} else {
2628
2627
$ parameters = $ this ->getParameters ($ this ->settings );
2629
2628
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
+ }
2637
2646
}
2638
2647
}
2639
2648
}
0 commit comments