├── .gitignore
├── App
├── Common
│ ├── Common
│ │ └── function.php
│ └── Conf
│ │ ├── config.php
│ │ └── const.php
├── Controller
│ ├── Index.php
│ └── SqlTest.php
└── Model
│ └── BookModel.php
├── Readme.md
├── SasPHP
├── Config.php
├── Model.php
└── SasPHP.php
├── composer.json
├── composer.lock
└── index.php
/.gitignore:
--------------------------------------------------------------------------------
1 | vendor/*
2 | .idea/*
--------------------------------------------------------------------------------
/App/Common/Common/function.php:
--------------------------------------------------------------------------------
1 | 86400,
9 | 'DB_MASTER' => array(
10 | 'server' => '127.0.0.1', // 服务器地址
11 | 'database' => 'bjbydc', // 数据库名
12 | 'username' => 'root', // 用户名
13 | 'password' => 'hufeng', // 密码
14 | 'port' => 3306, // 端口
15 | 'charset'=> 'utf8', // 字符集
16 | )
17 | );
--------------------------------------------------------------------------------
/App/Common/Conf/const.php:
--------------------------------------------------------------------------------
1 | findData();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/App/Model/BookModel.php:
--------------------------------------------------------------------------------
1 | dbConn->fetchRowMany($query);
15 | return json_encode($results);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Readme.md:
--------------------------------------------------------------------------------
1 | author : hufeng
2 | stime : 2015-12-15
3 | version : 1.0
4 | 简单高效php api server框架SasPHP 全拼 swoole api server
5 | 基于swoole扩展,composer开源的资源,组装的一个php api server
6 | 框架目录结构基于PSR-0规范。
7 | 简单易用,效率高,嗷嗷的,呵呵。
8 | 涉及相关知识点,请自行脑补:
9 | 1 命名空间
10 | 2 类自动加载
11 | 3 swoole
12 | 4 composer包管理工具
13 |
14 | 使用说明:
15 | 1 安装依赖 composer install
16 | 2 启动服务 php index.php
17 | 3 浏览器访问 http://ip:端口/controller/action?p1=v1&p2=v2
18 |
19 | ab压测:
20 | 通过使用apache bench工具进行压力测试,在 单核 + 1G内存的机器上,可以达到近1万QPS。性能几乎接近与Nginx的静态文件处理。
21 | ab -c 200 -n 200000 -k http://127.0.0.1:9502/index/index
22 | Concurrency Level: 200
23 | Time taken for tests: 16.349 seconds
24 | Complete requests: 200000
25 | Failed requests: 0
26 | Keep-Alive requests: 200000
27 | Total transferred: 35200000 bytes
28 | HTML transferred: 4600000 bytes
29 | Requests per second: 12232.97 [#/sec] (mean)
30 | Time per request: 16.349 [ms] (mean)
31 | Time per request: 0.082 [ms] (mean, across all concurrent requests)
32 | Transfer rate: 2102.54 [Kbytes/sec] received
33 |
34 |
35 | 迭代计划ing:
36 | 1 mysql连接池
37 | 2 mysql异步非阻塞
38 | 3 ORM
39 | 4 缓存
40 | 5 常用功能封装
41 | 6 日志
42 |
--------------------------------------------------------------------------------
/SasPHP/Config.php:
--------------------------------------------------------------------------------
1 | path = $path;
17 | }
18 |
19 | function offsetGet($key)
20 | {
21 | if (empty($this->configs[$key]))
22 | {
23 | $file_path = $this->path.'/'.$key.'.php';
24 | $config = require $file_path;
25 | $this->configs[$key] = $config;
26 | }
27 | return $this->configs[$key];
28 | }
29 |
30 | function offsetSet($key, $value)
31 | {
32 | throw new \Exception("cannot write config file.");
33 | }
34 |
35 | function offsetExists($key)
36 | {
37 | return isset($this->configs[$key]);
38 | }
39 |
40 | function offsetUnset($key)
41 | {
42 | unset($this->configs[$key]);
43 | }
44 | }
--------------------------------------------------------------------------------
/SasPHP/Model.php:
--------------------------------------------------------------------------------
1 | dbConn){
13 | echo "hufeng\n";
14 | return $this->dbConn;
15 | }else{
16 | $confObj = new Config(BASEDIR.'/App/Common/Conf');
17 | $conf = $confObj->offsetGet('config');
18 | $config = $conf['DB_MASTER'];
19 |
20 | $this->dbConn = new \Simplon\Mysql\Mysql($config['server'],$config['username'],$config['password'],$config['database']);
21 | return $this->dbConn;
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/SasPHP/SasPHP.php:
--------------------------------------------------------------------------------
1 | end($errorMsg['message']);
22 | }
23 |
24 | static public function autoload($class) {
25 | require_once BASEDIR.'/'.str_replace('\\', '/', $class).'.php';
26 | }
27 |
28 | static public function dispatch(){
29 | $uri = $_SERVER['request_uri'];
30 | $index = stripos($uri,'?');
31 | if($index){
32 | $uri = substr($uri,0,$index);
33 | }
34 | list($c, $v) = explode('/', trim($uri, '/'));
35 | $c = ucwords($c);
36 | return self::exec($c,$v);
37 | }
38 |
39 | static public function exec($c,$v){
40 | $class = '\\App\\Controller\\'.$c;
41 | $obj = new $class();
42 | return $obj->$v();
43 | }
44 | }
45 |
46 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "require": {
3 | "simplon/mysql": "^1.3",
4 | "desarrolla2/cache": "^2.0"
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/composer.lock:
--------------------------------------------------------------------------------
1 | {
2 | "_readme": [
3 | "This file locks the dependencies of your project to a known state",
4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5 | "This file is @generated automatically"
6 | ],
7 | "hash": "0dc732f707ffcab17c662968fa381b7c",
8 | "content-hash": "6f06a5f40f245178f963fa5c64439fe7",
9 | "packages": [
10 | {
11 | "name": "desarrolla2/cache",
12 | "version": "v2.0.0",
13 | "source": {
14 | "type": "git",
15 | "url": "https://github.com/desarrolla2/Cache.git",
16 | "reference": "40ce627d64d0bbcb3677903be9fc1139daad0a2f"
17 | },
18 | "dist": {
19 | "type": "zip",
20 | "url": "http://packagist.phpcomposer.com/files/desarrolla2/Cache/40ce627d64d0bbcb3677903be9fc1139daad0a2f.zip",
21 | "reference": "40ce627d64d0bbcb3677903be9fc1139daad0a2f",
22 | "shasum": ""
23 | },
24 | "require": {
25 | "php": ">=5.4.0"
26 | },
27 | "require-dev": {
28 | "predis/predis": "~1.0.0"
29 | },
30 | "suggest": {
31 | "predis/predis": "Predis support"
32 | },
33 | "type": "library",
34 | "autoload": {
35 | "psr-4": {
36 | "Desarrolla2\\Cache\\": "src/",
37 | "Desarrolla2\\Test\\Cache\\": "test/"
38 | }
39 | },
40 | "notification-url": "https://packagist.org/downloads/",
41 | "license": [
42 | "MIT"
43 | ],
44 | "authors": [
45 | {
46 | "name": "Daniel González",
47 | "homepage": "http://desarrolla2.com/"
48 | }
49 | ],
50 | "description": "Provides an cache interface for several adapters Apc, Apcu, File, Mongo, Memcache, Memcached, Mysql, Mongo, Redis is supported. New adapters is comming!",
51 | "homepage": "https://github.com/desarrolla2/Cache/",
52 | "keywords": [
53 | "apc",
54 | "apcu",
55 | "cache",
56 | "file",
57 | "memcache",
58 | "memcached",
59 | "mongo",
60 | "mysql",
61 | "redis"
62 | ],
63 | "time": "2015-10-27 09:58:14"
64 | },
65 | {
66 | "name": "simplon/mysql",
67 | "version": "1.3.1",
68 | "source": {
69 | "type": "git",
70 | "url": "https://github.com/fightbulc/simplon_mysql.git",
71 | "reference": "5a7cbbc5e7dee55c07ff5536afb9b072c492d54d"
72 | },
73 | "dist": {
74 | "type": "zip",
75 | "url": "http://packagist.phpcomposer.com/files/fightbulc/simplon_mysql/5a7cbbc5e7dee55c07ff5536afb9b072c492d54d.zip",
76 | "reference": "5a7cbbc5e7dee55c07ff5536afb9b072c492d54d",
77 | "shasum": ""
78 | },
79 | "require": {
80 | "ext-pdo": "*",
81 | "php": ">=5.4"
82 | },
83 | "type": "library",
84 | "autoload": {
85 | "psr-4": {
86 | "Simplon\\Mysql\\": "src/"
87 | }
88 | },
89 | "notification-url": "https://packagist.org/downloads/",
90 | "license": [
91 | "MIT"
92 | ],
93 | "authors": [
94 | {
95 | "name": "Tino Ehrich",
96 | "email": "tino@bigpun.me",
97 | "role": "developer"
98 | }
99 | ],
100 | "description": "Simplon MySQL Library",
101 | "homepage": "https://github.com/fightbulc/simplon_mysql",
102 | "keywords": [
103 | "db",
104 | "manager",
105 | "mysql",
106 | "pdo"
107 | ],
108 | "time": "2015-11-26 10:33:53"
109 | }
110 | ],
111 | "packages-dev": [],
112 | "aliases": [],
113 | "minimum-stability": "stable",
114 | "stability-flags": [],
115 | "prefer-stable": false,
116 | "prefer-lowest": false,
117 | "platform": [],
118 | "platform-dev": []
119 | }
120 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 | set(array(
6 | 'worker_num' => 5, //工作进程数量
7 | 'daemonize' => true //是否作为守护进程
8 | ));
9 |
10 | $globalRes;
11 | define('BASEDIR',__DIR__);
12 | require 'vendor/autoload.php';
13 | require './SasPHP/SasPHP.php';
14 |
15 | $http->on('request', function ($request, $response) {
16 | // 阻止google浏览器的ico请求
17 | if($request->server['request_uri'] == '/favicon.ico'){
18 | $response->end();exit;}
19 |
20 | global $globalRes;
21 | $globalRes = $response;
22 | $_SERVER = $request->server;
23 | $res = SasPHP\SasPHP::start();
24 | $response->end($res);
25 | });
26 |
27 | $http->start();
--------------------------------------------------------------------------------