三個核心文件php
1.公開訪問web service配置 config.phpweb
2.管理BEAN,掃描,註冊,初始化等流程 ioc.phpapp
3.管理 rest 攔載處理 ws.phppost
config.php測試
<?php /** * @author solq * @deprecated blog http://solq.cnblogs.com */ $_suffix = ".php"; $_beans=array( 'TestRef', ); /**容器註冊類*/ $_ioc= array(); $_app_path_index=1; ?>
ioc.phpthis
<?php /** * @author solq * @deprecated blog http://solq.cnblogs.com */ include_once "config.php"; /** 掃描BEAN */ function scan_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); } } /**註冊BEAN*/ function register_bean($name,$bean){ global $_ioc; $_ioc[$name]=$bean; } /**獲取BEAN*/ function get_bean($name){ global $_ioc; return $_ioc[$name]; } /**容器註冊後期階段*/ function postConstruct_bean(){ global $_ioc; foreach($_ioc as $bean){ if (is_subclass_of($bean, 'Ioc')) { $bean->{"setIoc"}($_ioc); $bean->{"postConstruct"}(); } } } /**容器銷燬階段*/ function preDestroy_bean(){ global $_ioc; foreach($_ioc as $bean){ if (is_subclass_of($bean, 'Ioc')) { $bean->{"preDestroy"}(); } } } interface Ioc{ public function postConstruct(); public function preDestroy(); public function setIoc($_ioc); } abstract class AbstractIoc implements Ioc{ public function postConstruct(){} public function preDestroy(){} public function setIoc($_ioc){} } ?>
ws.phpurl
<?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; //應用 $app = $segments[$_app_path_index]; //服務 $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); preDestroy_bean(); /** 獲取請求方式 */ function get_request_method(){ return strtolower($_SERVER['REQUEST_METHOD']); } /** 動態映射處理 */ function ___call($bean,$method, $get_params = array(), $post_params = array()){ $method = get_request_method().'_'.$method; $reflection = new ReflectionMethod($bean, $method); $pass = array(); if(strpos($method,"post_")){ $args = $post_params; }else{ $args = $get_params; } foreach($reflection->getParameters() as $param) { //數據類型注入分解 $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.phpspa
<?php include_once "ioc.php"; class TestRef extends AbstractIoc { public $one = 'aaaaaaaa'; public function __construct(){ } /** 書寫約定 [請求方式]_[服務] */ public function get_test1($a,$b,$c=null){ echo $this->one."\n"; echo $b."\n"; echo $c."\n"; } public function preDestroy(){ echo "<br/>postConstruct_bean"; } } ?>
測試url : http://127.0.0.1/ws.php/TestRef/test1/?a=121212&b=1212rest
結果code
aaaaaaaa 1212
postConstruct_bean