Three core files
1. Public access to the Web service configuration config.php
2. Manage beans, scan, register, initialize and other processes ioc.php
3. Manage Rest block handling ws.php
config.php
<? PHP /* * * @author SOLQ * @deprecated blog http://solq.cnblogs.com */ $_suffix = ". php"; $_beans=array( ' testref ', ); /* * Container Registration class */ $_IOC Array (); $_app_path_index=1;? >
ioc.php
<?PHP/** * @author SOLQ * @deprecated blog http://solq.cnblogs.com*/include_once"Config.php";/** Scan Bean*/functionScan_bean () {Global $_suffix; Global $_beans; Global $_IOC; for($i= 0;$i<Count($_beans);$i++){ $name=$_beans[$i]; $file=$name.$_suffix; include_once $file; Register_bean ($name,New $name); }}/** Register Bean*/functionRegister_bean ($name,$bean){ Global $_IOC; $_IOC[$name]=$bean;}/** Get Bean*/functionGet_bean ($name){ Global $_IOC; return $_IOC[$name];}/** Container registration later stage*/functionPostconstruct_bean () {Global $_IOC; foreach($_IOC as $bean){ if(is_subclass_of($bean, ' Ioc ')) { $bean->{"Setioc"} ($_IOC); $bean->{"Postconstruct"}(); } }}/** Container Destruction Phase*/functionPredestroy_bean () {Global $_IOC; foreach($_IOC as $bean){ if(is_subclass_of($bean, ' Ioc ')) { $bean->{"Predestroy"}(); } }}Interfaceioc{ Public functionpostconstruct (); Public functionPredestroy (); Public functionSETIOC ($_IOC);}Abstract classAbstractiocImplementsioc{ Public functionpostconstruct () {} Public functionPredestroy () {} Public functionSETIOC ($_IOC){}}?>
ws.php
<?PHP/** * @author SOLQ * @deprecated blog http://solq.cnblogs.com*/include_once"Ioc.php"; Scan_bean ();$page=$_server[' Request_uri '];$segments=Explode(‘/‘,Trim($page,‘/‘));Global $_app_path_index;//Application$app=$segments[$_app_path_index];//Service$service=$segments[$_app_path_index+1];$method=$service;$get _params=$_get;$post _params=$_post;$bean= Get_bean ($app);if($bean==NULL){ Throw New Exception("Bean [".)$app."] Not find "); }postconstruct_bean (); ___call ($bean,$method,$get _params,$post _params);p Redestroy_bean ();/** Get Request Method*/functionGet_request_method () {return Strtolower($_server[' Request_method ']);}/** Dynamic Mapping processing*/function___call ($bean,$method,$get _params=Array(),$post _params=Array()){ $method= Get_request_method (). ' _‘.$method; $reflection=NewReflectionmethod ($bean,$method); $pass=Array(); if(Strpos($method, "Post_")){ $args=$post _params; }Else{ $args=$get _params; } foreach($reflection->getparameters () as $param) { //data type injection decomposition $value=$args[$param-GetName ()]; if($value==NULL&&!$param-isdefaultvalueavailable ()) { Throw New Exception("method [".)$method."] Param is not: ".$param-getName ()); } $pass[] =$value; } return $reflection->invokeargs ($bean,$pass); } ?>
testref.php
<?PHPinclude_once"Ioc.php";classTestrefextendsabstractioc{ Public $one= ' AAAAAAAA '; Public function__construct () {}/** Writing agreement [request Way]_[service]*/ Public functionGet_test1 ($a,$b,$c=NULL){ Echo $this->one. " \ n "; Echo $b." \ n "; Echo $c." \ n "; } Public functionPredestroy () {Echo"<br/>postconstruct_bean"; }}?>
Test url:http://127.0.0.1/ws.php/testref/test1/?a=121212&b=1212
Results
AAAAAAAA 1212 Postconstruct_bean
PHP IOC and web Rest design