Created
February 3, 2010 14:09
-
-
Save ddallala/293619 to your computer and use it in GitHub Desktop.
ExtDirect PHP Stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require('config.php'); | |
| header('Content-Type: text/javascript'); | |
| // convert API config to Ext.Direct spec | |
| $actions = array(); | |
| foreach($API as $aname=>&$a){ | |
| $methods = array(); | |
| foreach($a['methods'] as $mname=>&$m){ | |
| $md = array( | |
| 'name'=>$mname, | |
| 'len'=>$m['len'] | |
| ); | |
| if(isset($m['formHandler']) && $m['formHandler']){ | |
| $md['formHandler'] = true; | |
| } | |
| $methods[] = $md; | |
| } | |
| $actions[$aname] = $methods; | |
| } | |
| $cfg = array( | |
| 'url'=>'php/router.php', | |
| 'type'=>'remoting', | |
| 'actions'=>$actions | |
| ); | |
| echo 'Ext.app.REMOTING_API = '; | |
| echo json_encode($cfg); | |
| echo ';'; | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $API = array( | |
| 'MyClassAction'=>array( | |
| 'methods'=>array( | |
| 'myFunction'=>array( | |
| 'len'=>2 | |
| ) | |
| ), | |
| ) | |
| ); | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .... | |
| <script type="text/javascript" src="php/direct/api.php"></script> | |
| <script type="text/javascript"> | |
| Ext.onReady(function() { | |
| /*----------------------- | |
| * Ext Direct | |
| ------------------------*/ | |
| Ext.Direct.addProvider(Ext.app.REMOTING_API); | |
| // calling PHP function directly from ExtJs | |
| MyClassAction.myFunction(1,2, function(ret){ | |
| console.debug(ret); | |
| },this); | |
| // ... your code | |
| }); | |
| <script> | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class MyClassAction{ | |
| /** | |
| * Method | |
| * | |
| * @return json | |
| */ | |
| function myFunction($a,$b) | |
| { | |
| return array(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require('config.php'); | |
| class BogusAction { | |
| public $action; | |
| public $method; | |
| public $data; | |
| public $tid; | |
| } | |
| $isForm = false; | |
| $isUpload = false; | |
| if(isset($HTTP_RAW_POST_DATA)){ | |
| header('Content-Type: text/javascript'); | |
| $data = json_decode($HTTP_RAW_POST_DATA); | |
| }else if(isset($_POST['extAction'])){ // form post | |
| $isForm = true; | |
| $isUpload = $_POST['extUpload'] == 'true'; | |
| $data = new BogusAction(); | |
| $data->action = $_POST['extAction']; | |
| $data->method = $_POST['extMethod']; | |
| $data->tid = $_POST['extTID']; | |
| $data->data = array($_POST, $_FILES); | |
| }else{ | |
| die('Invalid request.'); | |
| } | |
| function doRpc($cdata){ | |
| global $API; | |
| try { | |
| if(!isset($API[$cdata->action])){ | |
| throw new Exception('Call to undefined action: ' . $cdata->action); | |
| } | |
| $action = $cdata->action; | |
| $a = $API[$action]; | |
| doAroundCalls($a['before'], $cdata); | |
| $method = $cdata->method; | |
| $mdef = $a['methods'][$method]; | |
| if(!$mdef){ | |
| throw new Exception("Call to undefined method: $method on action $action"); | |
| } | |
| doAroundCalls($mdef['before'], $cdata); | |
| $r = array( | |
| 'type'=>'rpc', | |
| 'tid'=>$cdata->tid, | |
| 'action'=>$action, | |
| 'method'=>$method | |
| ); | |
| require_once("classes/$action.php"); | |
| $o = new $action(); | |
| $params = isset($cdata->data) && is_array($cdata->data) ? $cdata->data : array(); | |
| $r['result'] = call_user_func_array(array($o, $method), $params); | |
| doAroundCalls($mdef['after'], $cdata, $r); | |
| doAroundCalls($a['after'], $cdata, $r); | |
| } | |
| catch(Exception $e){ | |
| $r['type'] = 'exception'; | |
| $r['message'] = $e->getMessage(); | |
| $r['where'] = $e->getTraceAsString(); | |
| } | |
| return $r; | |
| } | |
| function doAroundCalls(&$fns, &$cdata, &$returnData=null){ | |
| if(!$fns){ | |
| return; | |
| } | |
| if(is_array($fns)){ | |
| foreach($fns as $f){ | |
| $f($cdata, $returnData); | |
| } | |
| }else{ | |
| $fns($cdata, $returnData); | |
| } | |
| } | |
| $response = null; | |
| if(is_array($data)){ | |
| $response = array(); | |
| foreach($data as $d){ | |
| $response[] = doRpc($d); | |
| } | |
| }else{ | |
| $response = doRpc($data); | |
| } | |
| if($isForm && $isUpload){ | |
| echo '<html><body><textarea>'; | |
| echo json_encode($response); | |
| echo '</textarea></body></html>'; | |
| }else{ | |
| echo prepareJSON($response); | |
| } | |
| /* Function to permit json encoding of javascript functions */ | |
| function prepareJSON($resp) { | |
| $value_arr = array(); | |
| $replace_keys = array(); | |
| // replacing all values defined as js functions by trailing ___ with a random number | |
| recursiveReplace($value_arr,$replace_keys,$resp); | |
| // transforming the data into json | |
| $encoded_json = json_encode($resp); | |
| // replacing the random numbers with their corresponding values from the value_array | |
| $encoded_json = str_replace($replace_keys,$value_arr,$encoded_json); | |
| return $encoded_json; | |
| } | |
| function recursiveReplace(&$value_arr,&$replace_keys,&$resp) { | |
| foreach($resp as $key => &$value){ | |
| // if value is array recursively check for a function type | |
| if(is_array($value)) { | |
| recursiveReplace($value_arr,$replace_keys,$value); | |
| } | |
| // Look for values starting with '___' | |
| else if(strpos($value, '___')===0){ | |
| $rand = rand(); | |
| // Store function string. | |
| $value_arr[$rand] = substr($value,3); | |
| // Replace function string in $foo with a 'unique' special key. | |
| $value = $rand; | |
| // Later on, we'll look for the value, and replace it. | |
| $replace_keys[] = $value; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment