示例#1
0
文件: index.php 项目: jzxyouok/phprs
$lib_path = $project_path . '/../lib/';
require_once $lib_path . '/caoym/AutoLoad.php';
ClassLoader::addInclude($project_path . '/apis/');
//Logger::$writer = Logger::$to_void; //日志不输出
//CLI调试时模拟请求
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/hw';
//依赖注入工厂, 通过此工厂创建的类自动注入配置中指定的属性(如果有的话)
//设置配置文件的替换字典, 如果配置文件中有用{}符号标记的变量, 配置文件加载时{}将被替换成指定的值
$factory = new IoCFactory($project_path . '/conf.json', array('lib_path' => $lib_path, 'app_root' => $project_path));
//创建路由器,第二个参数可选,表示用指定的属性列表覆盖配置文件中定义的属性列表
//对于RouterWithCache, 其属性url_begin用于指定路由的从哪一级开始匹配
//RouterWithCache创建时自动扫描其属性api_path指定的目录下的所有PHP文件(不扫描子目录),然后创建Router对象
//Router记录路由规则及对应的实现方法,Router将被缓存,当api_path目录下已加载的php文件修改时,缓存失效
//每次处理请求,api的实现类将被重新创建
$router = $factory->create('caoym\\phprs\\RouterWithCache');
$err = null;
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
//执行请求
try {
    $router();
} catch (NotFound $e) {
    header($protocol . ' 404 Not Found');
    $err = $e;
} catch (BadRequest $e) {
    header($protocol . ' 400 Bad Request');
    $err = $e;
} catch (Forbidden $e) {
    header($protocol . ' 403 Forbidden');
    $err = $e;
}
示例#2
0
 /**
  * 测试绑定引用类型的请求
  */
 public function testReferenceReqParam()
 {
     $factory = new IoCFactory(array('caoym\\phprs\\Router' => array('properties' => array('api_path' => dirname(__FILE__) . '/rest_test', 'apis' => 'MyTestApi', 'api_method' => 'funReferenceReqParam'))));
     $route = $factory->create('caoym\\phprs\\Router');
     //输入数据
     $req_buffer = ['_SERVER' => ['REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/testapi/func10'], 'header' => 'test header'];
     $res_buffer = array();
     //输出数据缓存
     $sender = array('status' => function () use(&$res_buffer) {
         $res_buffer[] = ['status', func_get_args()];
     }, 'body' => function () use(&$res_buffer) {
         $res_buffer[] = ['body', func_get_args()];
     });
     $res = new \caoym\phprs\Response($sender);
     //替换默认的输出方式, 以便记录输出结果
     $req = new \caoym\phprs\Request($req_buffer);
     $route($req, $res);
     $check_buffer = ['_SERVER' => ['REQUEST_METHOD' => 'GET', 'REQUEST_URI' => 'funReferenceReqParam arg0'], 'arg1' => array('arg1' => 'funReferenceReqParam arg1'), 'header' => 'test header'];
     $res = $req->toArray();
     unset($res['router']);
     unset($res['path']);
     $this->assertEquals($check_buffer, $res);
 }