新一代轻量级PHP扩展框架 Asf

一、Asf 是什么?

全称 API Services Framework, 用C语言编写的轻量级PHP扩展框架, 专注于 API 开发。

二、解决了什么问题?

  • 把复杂的逻辑简单化(降低错误率, 减少代码量)
  • 提升项目开发效率(您是否经历过,老板上午提需求,下午就得对外发布)
  • 规范不合理的开发方式
  • 解决输入输出的安全性问题
  • 解决框架带来的性能消耗

三、有那些优点呢?

  • 与原生PHP做比较,只有少量的性能消耗
  • 框架对常用类进行了实用的封装, 类随PHP进程启动就常驻内存
  • 支持本地类库自动加载规则
  • 支持多种配置文件格式(INI, PHP, PHP Array)
  • 支持多种路由协议, 默认使用RESTful路由模式, 提供方便的路由配置器
  • 提供实用的机制: GPC, Loader, Constants, Config, SG, 999, Utili
  • 提供日志记录功能, 包括PHP错误日志, SQL CURD日志
  • 提供DB辅助函数(MySQL, Sqlite, Pgsql), 人性化Query Builder
  • 支持Log buffer cache, Log resources are automatically cleared
  • 遵循PHP PSR Standards, PHP Coding Standards
  • 框架结构简单, PHP标准的扩展安装方式, 框架上手快。框架执行速度快, 更少的内存、CPU使用

四、流程图

五、性能

5.1 得出结论

5.1.1 压测结论

没有挑取漂亮的数据, 或者配置一个为了压测的最优环境. 只是简单地采用以大部分web机器使用的环境进行测评。
这里只是给出了一种测试方法, 通过多次不同并发数测试结果得知, Asf 与原生 PHP 性能消耗是 6% ~ 15%

5.1.2 业务开发速度结论

采用Asf框架开发业务, 代码量能节约 20% ~ 25%。假如项目开发需要4天 * 8小时, 能节约出整整1天 * 8小时的工作时间。

六、安装

6.1 环境要求

  • PHP 7.0 +
  • GCC 4.4.0+ (Recommended GCC 4.8+)

6.2 下载

git clone https://github.com/yulonghu/asf.git

6.3 在Linux/Unix/Mac下编译

$ /path/to/phpize
$ ./configure --with-php-config=/path/to/php-config
$ make && make install

6.4 文档

http://www.box3.cn/phpasf/index.html

七、开始使用

7.1 使用内置工具生成空项目

/php-bin-path/php /tools/asf_project.php /to-path/project_name

7.1.1 目录结构

+ public
  | - index.php
+ config
  | - config.php
+ library
+ modules
    | - Bootstrap.php
    | - Constants.php
  + api
    |+ services
	   |- Index.php  // Default service
    |+ logics
    |+ daos

7.1.2 config/config.php

<?php
$configs = array(
    'asf' => array(
        'root_path' => realpath(dirname(__FILE__)),
    )
);

return $configs;

7.1.3 public/index.php

<?php
define('APP_PATH', dirname(__DIR__));

$app = new Asf_Application(APP_PATH . '/config/config.php');
$app->run();

7.1.4 Default service

<?php
class IndexService
{
    public function indexAction()
    {
        return 'Hello World';
    }
}

八、在Nginx/Apache/Lighttpd中运行

http://www.your-domain.com

8.1 输出结果

{
    "errno": 0,
    "data": "Hello World"
}

九、License

Asf is open source software under the PHP License v3.01

猜你喜欢

转载自my.oschina.net/fanjiapeng/blog/1810537