PDA

View Full Version : Generate Ext.Direct API json Code



bywayboy
4 May 2010, 9:01 AM
If the code is helpful, and you improve my code, I hope you can send to share.

<?php
// config values
$direct_dir = "\\direct"; //class api dir
$site_root = substr(__FILE__,0,strlen(__FILE__)-strlen($_SERVER['PHP_SELF'])); //site root dir
$direct_dir=$site_root.$direct_dir;
$api = new API_Parser(isset($_GET['buffer']));
$api->ParseDir($direct_dir,isset($_GET['comment']));
$api->putJson();

class API_Parser{
private $api=array();
private $curnode=null;
private $hasComment=false;
private $buffered=null;
private $buffer=null;
private $ms=null;
private $file = null;
function __construct($buffered=true){
$this->buffered=$buffered;
if($this->buffered){
$this->file = $_SERVER['PATH_TRANSLATED'];
$this->file = substr($this->file,0,strlen($this->file)-strlen(basename($this->file)));
if(file_exists($this->file."modifytime.txt")){
$this->ms = unserialize(file_get_contents($this->file."modifytime.txt"));
}else{
$this->ms = array();
}
if(file_exists($this->file."buffered.txt")){
$this->api = unserialize(file_get_contents($this->file."buffered.txt"));
}else{
$this->api = array();
}
}
}
/**
* @param file string 文件名
* @param cls string 类名
* @return int
*/
function __destruct(){
if($this->buffered){
file_put_contents($this->file."modifytime.txt",serialize($this->ms));
file_put_contents($this->file."buffered.txt",serialize($this->api));
}
}
private function needReload(&$file,&$cls){
if($this->buffered){
$ct = filemtime($file);
if(array_key_exists($cls,$this->ms) && $this->ms[$cls]==$ct && array_key_exists($cls,$this->api))
{
return false; //不需要重新载入,条件 存在文件时间戳,时间相等 存在类数据
}
}
return true;
}
private function savemtime(&$file,&$cls){
if($this->buffered){
$this->ms[$cls]=filemtime($file);
}
}
private function ParseContent($file,$cls){
if(!$this->hasComment && !$this->needReload($file,$cls)){
return;
}
require_once $file;
$this->api[$cls]=array();
$this->curnode=&$this->api[$cls];
if(!class_exists($cls)){
return;
}
$c=new ReflectionClass($cls);
$methods = $c->getMethods();
foreach ($methods as $val){
if(!$val->isDestructor() &&!$val->isConstructor() && $val->isPublic()){
$n = $val->getName();
$argnum = $val->getNumberOfParameters();
if($this->hasComment)
{
$cmt = $val->getDocComment();
$this->curnode[$n]=array("len"=>$argnum,"comment"=>$cmt);
}else{
$this->curnode[$n]=array("len"=>$argnum);
}
}
}
$this->savemtime($file,$cls);
}
function ParseDir($direct_dir,$hasComment=false){
$this->hasComment=$hasComment;
$d=dir($direct_dir);
while (false !== ($entry = $d->read())) {
if($entry=="." || $entry=="..")continue;
$file="{$direct_dir}\\{$entry}";
$className = substr($entry,0,strpos($entry,"."));
$this->ParseContent($file,$className);
}
$d->close();
}
function getAPI(){
return $this->api;
}
function putJson(){
echo "Ext.app.REMOTING_API = ";
echo API_Parser::Array2Json($this->api);
echo ";";
}
private static function __array2j(&$arr)
{
$str='';
$first=true;
$is_obj=true;
foreach($arr as $key=>$value){
if($first){
$is_obj = !is_int($key);
$first=false;
$str=$is_obj?'{':'[';
}else{
$str.=',';
}
$t=gettype($value);
if($is_obj)
{
switch($t)
{
case 'boolean':
$str.='"'.$key.'":'.($value?'true':'false');break;
case 'NULL':
$str.='"'.$key.'":null';break;
case 'double':
case 'float':
case 'integer':
$str.='"'.$key.'":'.$value;break;
case 'string':
$str.='"'.$key.'":"'.API_Parser::__STRVAL($value).'"';break;
case 'array':
$str.='"'.$key.'":'.API_Parser::__array2j($value);break;
case 'object':
if('javaFunc' == get_class($value)){
$str.='"'.$key.'":'.$value->getStr();
}else{
$object_arr = get_object_vars($value);
$str.='"'.$key.'":'.API_Parser::__array2j($object_arr);
}
break;
}
}else{
switch($t)
{
case 'boolean':
$str.=$value?'true':'false';break;
case 'NULL':
$str.='null';break;
case 'double':
case 'float':
case 'integer':
$str.=$value;break;
case 'string':
$str.='"'.API_Parser::__STRVAL($value).'"';break;
case 'array':
$str.=API_Parser::__array2j($value);break;
case 'object':
if('javaFunc' == get_class($value)){
$str.='"'.$key.'":'.$value->getStr();
}else{
$object_arr = get_object_vars($value);
$str.='"'.$key.'":'.API_Parser::__array2j($object_arr);
}
break;
}
}
}
if($first==false)
return $str.($is_obj?'}':"]");
return '[]';
}
private static function __array2jucs(&$arr)
{
$str='';
$first=true;
$is_obj=true;
foreach($arr as $key=>$value){
if($first){
$is_obj = !is_int($key);
$first=false;
$str=$is_obj?'{':'[';
}else{
$str.=',';
}
$t=gettype($value);
if($is_obj)
{
switch($t)
{
case 'boolean':
$str.='"'.$key.'":'.($value?'true':'false');break;
case 'NULL':
$str.='"'.$key.'":null';break;
case 'double':
case 'float':
case 'integer':
$str.='"'.$key.'":'.$value;break;
case 'string':
if(strlen($value)>0){
$str.='"'.$key.'":"'.API_Parser::__STRVAL(iconv('UTF-8','GBK',$value)).'"';
}else{
$str.='"'.$key.'":""';
}break;
case 'array':
$str.='"'.$key.'":'.API_Parser::__array2jucs($value);break;
case 'object':
if('javaFunc' == get_class($value)){
$str.='"'.$key.'":'.$value->getStr();
}else{
$object_arr = get_object_vars($value);
$str.='"'.$key.'":'.API_Parser::__array2jucs($object_arr);
}
break;
}
}else{
switch($t)
{
case 'boolean':
$str.=$value?'true':'false';break;
case 'NULL':
$str.='null';break;
case 'double':
case 'float':
case 'integer':
$str.=$value;break;
case 'string':
if(strlen($value)>0){
$str.='"'.API_Parser::__STRVAL(iconv('UTF-8','GBK',$value)).'"';break;
}else{
$str.='""';break;
}break;
case 'array':
$str.=API_Parser::__array2jucs($value);break;
case 'object':
if('javaFunc' == get_class($value)){
$str.='"'.$key.'":'.$value->getStr();
}else{
$object_arr = get_object_vars($value);
$str.='"'.$key.'":'.API_Parser::__array2jucs($object_arr);
}
break;
}
}
}
if($first==false)
return $str.($is_obj?'}':"]");
return '[]';
}
private static function Array2Json(&$arr, $encode = 'GBK')
{
$t=gettype($arr);
switch($encode){
case 'GBK':
case 'gbk':
case 'GB2312':
case 'gb2312':
switch($t){
case 'array':
return API_Parser::__array2j($arr);break;
case 'object':
if('javaFunc' == get_class($arr)){
return $value->getStr();
}else{
$object_arr = get_object_vars($arr);
return API_Parser::__array2j($object_arr);
}
break;
default:
return $arr;
}
break;
case 'utf-8':
case 'utf8':
case 'UTF8':
case 'UTF-8':
switch($t){
case 'array':
return API_Parser::__array2jucs($arr);break;
case 'object':
if('javaFunc' == get_class($arr)){
return $value->getStr();
}else{
$object_arr = get_object_vars($arr);
return API_Parser::__array2jucs($object_arr);
}
break;
default:
return $arr;
}
break;
}
}

private static function __STRVAL($htmlStr){
$search = array ( "\\","\"","\r","\n");
$replace = array ('\\\\',"\\\"","\\r","\\n");

return str_replace($search,$replace,$htmlStr);
}
}
?>


<script type="text/javascript" src="api.php?buffer=1"></script>