三个核心文件

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

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

结果

aaaaaaaa 1212
postConstruct_bean

php ioc and web rest design的更多相关文章

  1. IIS配置Asp.net时,出现“未能加载文件或程序集“System.Web.Extensions.Design, Version=1.0.61025.0”

    如果出现未能加载文件或程序集“System.Web.Extensions.Design, Version=1.0.61025.0, 主要是没有安装.net framwork 3.5,安装一下就行了. ...

  2. Web API design

    Web API design 28 minutes to read Most modern web applications expose APIs that clients can use to i ...

  3. A web crawler design for data mining

    Abstract The content of the web has increasingly become a focus for academic research. Computer prog ...

  4. SpringMVC——DispatcherServlet的IoC容器(Web应用的IoC容器的子容器)创建过程

    在上一篇<Spring--Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)>中说到了Web应用中的IoC容器创建过程.这一篇主要讲Sprin ...

  5. Web UI Design Patterns 2014

    http://www.uxpin.com/web-design-patterns.html?utm_source=Interaction%20Design%20Best%20Practices%20V ...

  6. JAVA web 框架集合

    “框架”犹如滔滔江水连绵不绝, 知道有它就好,先掌握自己工作和主流的框架: 在研究好用和新框架. 主流框架教程分享在Java帮帮-免费资源网 其他教程需要时间制作,会陆续分享!!! 152款框架,你还 ...

  7. 25套用于 Web UI 设计的免费 PSD 网页元素模板

    Web 元素是任何网站相关项目都需要的,质量和良好设计的元素对于设计师来说就像宝贝一样.如果您正在为您的网站,博客,Web 应用程序或移动应用程序寻找完美设计的网页元素,那么下面这个列表会是你需要的. ...

  8. Web.config配置文件详解

    整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <?xml v ...

  9. Web.config配置文件详解(新手必看)(转)

    转于:http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html <?xml version="1.0" ...

随机推荐

  1. IIS7.5 发布程序后cookie丢失问题

    给客户部署网站程序时,发现网站的用户登录不了系统,就是跳转不到另外一个页面,一直保留在当前的页面.在本地的VS里面调试的时候是正常的,算在本地的IIS上发布了一下,测试也是这个问题. -------- ...

  2. GCD中的dispatch_sync、dispatch_sync 分别与串行、并行队列组合执行小实验

    平常开发中会经常用gcd做一下多线程任务,但一直没有对同步.异步任务在串行.并行队列的执行情况做个全面的认识,今天写了个demo跑了下,还是有些新发现的. 代码如下: - (void)touchesB ...

  3. js时间函数

    时间 new Date() 获取时间,可以理解为是系统默认的函数. 从小括号里面获取系统时间日期,相当于在调用系统默认的函数. 年 getFullYear() 注意,有Full. 月 getMonth ...

  4. paip.提升性能---并行多核编程哈的数据结构list,set,map

    paip.提升性能---并行多核编程哈的数据结构list,set,map vector/copyonwritearraylist 都是线程安全的. 或者经过包装的list ::: collection ...

  5. Liferay7 BPM门户开发之27: MVC Portlet插件工程开发

    官网上的教材说实话实在精简不清晰. https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/creating-an-mvc-por ...

  6. Android SQLite3工具常用命令行总结

    Android SDK的tools目录下提供了一个sqlite3.exe工具,这是一个简单的sqlite数据库管理工具.开发者可以方便的使用其对sqlite数据库进行命令行的操作. 程序运行生成的*. ...

  7. js系列(8)简介

        JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HT ...

  8. RTL8710 ROM 符号表 函数,常量,变量

    函数 Name Address Ordinal ---- ------- ------- __vectors_table Reset_Handler NMI_Handler HardFault_Han ...

  9. Explain in detail the steps/processes that occur from the moment you type a URL in a browser and hit enter

    In an extremely rough and simplified sketch, assuming the simplest possible HTTP request, no proxies ...

  10. tbb flow graph node types