afridlund85/asd-framework

适用于PHP7的最简框架

dev-master 2016-09-06 21:45 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:53:17 UTC


README

PHP7框架,用于API应用程序。

安装

无,尚未准备好发布。

##一些关键概念

  • 仅适用于PHP 7
  • 无依赖(预期PSR-7接口)
  • PSR: 1,2,4,7
  • 无静态,无全局变量
  • 使用时无强制命名/路由、文件夹结构等严格约定
  • 无设置或配置
  • 透明性,无不必要的抽象/包装
  • 使用PHP 7严格类型(PSR-7类除外,因为它们不是PHP 7)

预期用途

假设:已配置htaccess和PSR-4自动加载

使用HTTP方法、路径和控制器类+方法定义路由

index.php

require_once('vendor/autoload.php');

use Asd\Asd;
use Asd\Router\Route;
use Asd\FunctionCallback;
use Asd\MethodCallback;

$app = new Asd();

//Controller class extending from Asd\Controller and has dependency to Asd\Session
$app->addRoute(new Route('GET', '/', new MethodCallback('MyApp\Controllers', 'Welcome', 'start')));

//Just some class with a method
$app->addRoute(new Route('GET', 'about', new MethodCallback('MyApp', 'SomeClass', 'about')));

//Closure/Anonymus function
$app->addRoute(new Route('post', 'blog', new FunctionCallback(function($req, $res){
  //do stuff
  return $this->res; //return response
})));

//Closure/anonymus function with dependency
$app->addRoute(new Route('post', 'auth', new FunctionCallback(function($req, $res, Asd\Session $session){
  //session class automatically injected through DI
  return $this->res;//return response
})));

$app->run();

MyApp/Controllers/Welcome.php

namespace MyApp\controllers;

use Asd\Controller;

class Welcome extends Controller
{
  public function __construct(Asd\Session $session)
  {
    //session class automatically injected through DI
  }
  public function start($req, $res)
  {
    return $res->withJsonResponse('Hello!');
  }
}

MyApp/SomeClass.php

namespace MyApp;

class SomeClass
{
  public function about($req, $res)
  {
    //do stuff;
    return $res;
  }
}

设置开发环境

需要PHP7

git clone https://github.com/afridlund85/php7-api-framework.git
cd php7-api-framework
composer install

测试

三个级别的测试:单元、集成和系统。

运行测试

运行所有测试套件

composer test

运行单元测试套件

composer unit

运行集成测试套件

composer integration

运行系统测试套件

composer system

PSR-2代码风格检查

检查PSR-2错误

composer sniff

代码覆盖率

生成代码覆盖率

composer coverage