php ioc and web rest design

來源:互聯網
上載者:User

標籤:

三個核心檔案

1.公開訪問web service配置 config.php

2.管理BEAN,掃描,註冊,初始化等流程 ioc.php

3.管理 rest 攔載處理 ws.php

config.php

<?php    /**     * @author solq     * @deprecated blog http://solq.cnblogs.com     */    $_suffix = ".php";        $_beans=array(        ‘TestRef‘,    );     /**容器註冊類*/    $_ioc= array();    $_app_path_index=1;?>

ioc.php

 

<?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.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;//應用$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.php

<?phpinclude_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=1212

結果

aaaaaaaa 1212 postConstruct_bean

 

php ioc and web rest design

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.