Browse Source

Add imi (#5091)

* Add imi

* Fixed

* remove redis ext
Yurun 5 years ago
parent
commit
a51a9b807f

+ 1 - 1
.travis.yml

@@ -102,7 +102,7 @@ env:
     - "TESTLANG=Perl"
     - 'TESTDIR="PHP/php"'
     - 'TESTDIR="PHP/cakephp PHP/codeigniter PHP/fat-free PHP/fuel PHP/kumbiaphp PHP/phpixie PHP/slim PHP/symfony PHP/yii2 PHP/zend PHP/spiral"'
-    - 'TESTDIR="PHP/amp PHP/hamlet PHP/laravel PHP/lumen PHP/hhvm PHP/peachpie PHP/php-ngx PHP/swoole PHP/workerman PHP/phalcon PHP/ubiquity PHP/hyperf PHP/sw-fw-less"'
+    - 'TESTDIR="PHP/amp PHP/hamlet PHP/laravel PHP/lumen PHP/hhvm PHP/peachpie PHP/php-ngx PHP/swoole PHP/workerman PHP/phalcon PHP/ubiquity PHP/hyperf PHP/sw-fw-less PHP/imi"'
     - 'TESTDIR="Python/aiohttp Python/api_hour Python/apidaora Python/blacksheep Python/bottle Python/cherrypy Python/django Python/eve Python/falcon Python/fastapi Python/flask"'
     - 'TESTDIR="Python/hug Python/japronto Python/klein Python/morepath Python/pyramid Python/quart Python/responder Python/sanic Python/spyne Python/starlette"'
     - 'TESTDIR="Python/tornado Python/turbogears Python/uvicorn Python/uwsgi Python/vibora Python/web2py Python/webware Python/weppy Python/wsgi"'

+ 2 - 0
frameworks/PHP/imi/.gitignore

@@ -0,0 +1,2 @@
+/.runtime
+/vendor

+ 0 - 0
frameworks/PHP/imi/.runtime/.gitkeep


+ 113 - 0
frameworks/PHP/imi/ApiServer/Controller/IndexController.php

@@ -0,0 +1,113 @@
+<?php
+namespace ImiApp\ApiServer\Controller;
+
+use ImiApp\Model\World;
+use ImiApp\Model\Fortune;
+use Imi\Controller\HttpController;
+use Imi\Db\Annotation\Transaction;
+use Imi\Server\View\Annotation\View;
+use Imi\Server\Route\Annotation\Action;
+use Imi\Server\Route\Annotation\Controller;
+
+/**
+ * @Controller("/")
+ */
+class IndexController extends HttpController
+{
+    /**
+     * @Action
+     *
+     * @return void
+     */
+    public function json()
+    {
+        return ['message' => 'Hello, World!'];
+    }
+
+    /**
+     * @Action
+     * @View(renderType="html")
+     *
+     * @return void
+     */
+    public function plaintext()
+    {
+        return $this->response->withHeader('Content-Type', 'text/plain')->write('Hello, World!');
+    }
+
+    /**
+     * @Action
+     *
+     * @return void
+     */
+    public function db()
+    {
+        return World::find(mt_rand(1, 10000));
+    }
+
+    /**
+     * @Action
+     *
+     * @return void
+     */
+    public function query($queries)
+    {
+        $queryCount = 1;
+        if($queries > 1)
+        {
+            $queryCount = min($queries, 500);
+        }
+        $list = [];
+        while ($queryCount--)
+        {
+            $list[] = World::find(mt_rand(1, 10000));
+        }
+        return $list;
+    }
+
+    /**
+     * @Action
+     * @View(renderType="html")
+     *
+     * @return void
+     */
+    public function fortunes()
+    {
+        $this->response = $this->response->withHeader('Content-Type', 'text/html; charset=UTF-8');
+        $list = Fortune::select();
+        $rows = [];
+        foreach($list as $item)
+        {
+            $rows[$item->id] = $item->message;
+        }
+        $rows[0] = 'Additional fortune added at request time.';
+        asort($rows);
+        return [
+            'rows'  =>  $rows,
+        ];
+    }
+
+    /**
+     * @Action
+     * @Transaction
+     *
+     * @return void
+     */
+    public function update($queries)
+    {
+        $queryCount = 1;
+        if($queries > 1)
+        {
+            $queryCount = min($queries, 500);
+        }
+        $list = [];
+        while ($queryCount--)
+        {
+            $list[] = $row = World::find(mt_rand(1, 10000));
+            $row->randomNumber = mt_rand(1, 10000);
+            $row->update();
+        }
+        return $list;
+    }
+
+}

+ 13 - 0
frameworks/PHP/imi/ApiServer/Main.php

@@ -0,0 +1,13 @@
+<?php
+namespace ImiApp\ApiServer;
+
+use Imi\Main\BaseMain;
+
+class Main extends BaseMain
+{
+    public function __init()
+    {
+        // 可以做一些初始化操作
+    }
+
+}

+ 26 - 0
frameworks/PHP/imi/ApiServer/config/config.php

@@ -0,0 +1,26 @@
+<?php
+return [
+    'configs'    =>    [
+    ],
+    // bean扫描目录
+    'beanScan'    =>    [
+        'ImiApp\ApiServer\Controller',
+        'ImiApp\Model',
+    ],
+    'beans'    =>    [
+        'HttpDispatcher'    =>    [
+            'middlewares'    =>    [
+                \Imi\Server\Http\Middleware\RouteMiddleware::class,
+            ],
+        ],
+        'HtmlView'    =>    [
+            'templatePath'    =>    dirname(__DIR__) . '/template/',
+            // 支持的模版文件扩展名,优先级按先后顺序
+            'fileSuffixs'        =>    [
+                'tpl',
+                'html',
+                'php'
+            ],
+        ]
+    ],
+];

+ 13 - 0
frameworks/PHP/imi/ApiServer/template/fortunes.tpl

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+<head><title>Fortunes</title></head>
+<body>
+<table>
+	<tr><th>id</th><th>message</th></tr>
+
+	<?php foreach($rows as $id => $fortune):?>
+		<tr><td><?=$id?></td><td><?=htmlspecialchars($fortune, ENT_QUOTES, 'UTF-8')?></td></tr>
+    <?php endforeach;?>
+</table>
+</body>
+</html>

+ 15 - 0
frameworks/PHP/imi/Main.php

@@ -0,0 +1,15 @@
+<?php
+namespace ImiApp;
+
+use Imi\App;
+use Imi\Main\AppBaseMain;
+
+class Main extends AppBaseMain
+{
+    public function __init()
+    {
+        // 这里可以做一些初始化操作,如果需要的话
+        App::setDebug(true);
+    }
+
+}

+ 74 - 0
frameworks/PHP/imi/Model/Base/FortuneBase.php

@@ -0,0 +1,74 @@
+<?php
+namespace ImiApp\Model\Base;
+
+use Imi\Model\Model;
+use Imi\Model\Annotation\Table;
+use Imi\Model\Annotation\Column;
+use Imi\Model\Annotation\Entity;
+
+/**
+ * FortuneBase
+ * @Entity
+ * @Table(name="fortune", id={"id"})
+ * @property int $id 
+ * @property string $message 
+ */
+abstract class FortuneBase extends Model
+{
+    /**
+     * id
+     * @Column(name="id", type="int", length=10, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true)
+     * @var int
+     */
+    protected $id;
+
+    /**
+     * 获取 id
+     *
+     * @return int
+     */ 
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    /**
+     * 赋值 id
+     * @param int $id id
+     * @return static
+     */ 
+    public function setId($id)
+    {
+        $this->id = $id;
+        return $this;
+    }
+
+    /**
+     * message
+     * @Column(name="message", type="varchar", length=2048, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
+     * @var string
+     */
+    protected $message;
+
+    /**
+     * 获取 message
+     *
+     * @return string
+     */ 
+    public function getMessage()
+    {
+        return $this->message;
+    }
+
+    /**
+     * 赋值 message
+     * @param string $message message
+     * @return static
+     */ 
+    public function setMessage($message)
+    {
+        $this->message = $message;
+        return $this;
+    }
+
+}

+ 74 - 0
frameworks/PHP/imi/Model/Base/WorldBase.php

@@ -0,0 +1,74 @@
+<?php
+namespace ImiApp\Model\Base;
+
+use Imi\Model\Model;
+use Imi\Model\Annotation\Table;
+use Imi\Model\Annotation\Column;
+use Imi\Model\Annotation\Entity;
+
+/**
+ * WorldBase
+ * @Entity
+ * @Table(name="world", id={"id"})
+ * @property int $id 
+ * @property int $randomNumber 
+ */
+abstract class WorldBase extends Model
+{
+    /**
+     * id
+     * @Column(name="id", type="int", length=10, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true)
+     * @var int
+     */
+    protected $id;
+
+    /**
+     * 获取 id
+     *
+     * @return int
+     */ 
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    /**
+     * 赋值 id
+     * @param int $id id
+     * @return static
+     */ 
+    public function setId($id)
+    {
+        $this->id = $id;
+        return $this;
+    }
+
+    /**
+     * randomNumber
+     * @Column(name="randomNumber", type="int", length=11, accuracy=0, nullable=false, default="0", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
+     * @var int
+     */
+    protected $randomNumber;
+
+    /**
+     * 获取 randomNumber
+     *
+     * @return int
+     */ 
+    public function getRandomNumber()
+    {
+        return $this->randomNumber;
+    }
+
+    /**
+     * 赋值 randomNumber
+     * @param int $randomNumber randomNumber
+     * @return static
+     */ 
+    public function setRandomNumber($randomNumber)
+    {
+        $this->randomNumber = $randomNumber;
+        return $this;
+    }
+
+}

+ 14 - 0
frameworks/PHP/imi/Model/Fortune.php

@@ -0,0 +1,14 @@
+<?php
+namespace ImiApp\Model;
+
+use Imi\Bean\Annotation\Inherit;
+use ImiApp\Model\Base\FortuneBase;
+
+/**
+ * Fortune
+ * @Inherit
+ */
+class Fortune extends FortuneBase
+{
+
+}

+ 14 - 0
frameworks/PHP/imi/Model/World.php

@@ -0,0 +1,14 @@
+<?php
+namespace ImiApp\Model;
+
+use Imi\Bean\Annotation\Inherit;
+use ImiApp\Model\Base\WorldBase;
+
+/**
+ * World
+ * @Inherit
+ */
+class World extends WorldBase
+{
+
+}

+ 137 - 0
frameworks/PHP/imi/README.md

@@ -0,0 +1,137 @@
+<p align="center">
+    <a href="https://www.imiphp.com" target="_blank">
+        <img src="https://raw.githubusercontent.com/Yurunsoft/IMI/dev/res/logo.png" alt="imi" />
+    </a>
+</p>
+
+[![Latest Version](https://img.shields.io/packagist/v/yurunsoft/imi.svg)](https://packagist.org/packages/yurunsoft/imi)
+[![Travis](https://img.shields.io/travis/Yurunsoft/IMI.svg)](https://travis-ci.org/Yurunsoft/IMI)
+[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg)](https://secure.php.net/)
+[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.3.0-brightgreen.svg)](https://github.com/swoole/swoole-src)
+[![imi Doc](https://img.shields.io/badge/docs-passing-green.svg)](https://doc.imiphp.com)
+[![imi License](https://img.shields.io/badge/license-MulanPSL%201.0-brightgreen.svg)](https://github.com/Yurunsoft/imi/blob/master/LICENSE)
+
+## 介绍
+
+imi 是基于 PHP Swoole 的高性能协程应用开发框架,它支持 HttpApi、WebSocket、TCP、UDP 服务的开发。
+
+在 Swoole 的加持下,相比 php-fpm 请求响应能力,I/O密集型场景处理能力,有着本质上的提升。
+
+imi 框架拥有丰富的功能组件,可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。可以使企业 IT 研发团队的效率大大提升,更加专注于开发创新产品。
+
+imi 框架交流群:17916227 [![点击加群](https://pub.idqqimg.com/wpa/images/group.png "点击加群")](https://jq.qq.com/?_wv=1027&k=5wXf4Zq)
+
+### 核心组件
+
+* HttpApi、WebSocket、TCP、UDP 服务器
+* MySQL 连接池 (主从+负载均衡)
+* Redis 连接池 (主从+负载均衡)
+* 超好用的 ORM (Db、Redis、Tree)
+* 毫秒级热更新
+* AOP
+* Bean 容器
+* 缓存 (Cache)
+* 配置读写 (Config)
+* 枚举 (Enum)
+* 事件 (Event)
+* 门面 (Facade)
+* 验证器 (Validate)
+* 锁 (Lock)
+* 日志 (Log)
+* 异步任务 (Task)
+
+### 扩展组件
+
+* [RPC](https://github.com/imiphp/imi-rpc)
+* [Hprose](https://github.com/imiphp/imi-hprose)
+* [权限控制](https://github.com/imiphp/imi-access-control)
+* [Smarty 模版引擎](https://github.com/imiphp/imi-smarty)
+* [限流](https://github.com/imiphp/imi-rate-limit)
+* [跨进程变量共享](https://github.com/imiphp/imi-shared-memory)
+* [Swoole Tracker](https://github.com/imiphp/imi-swoole-tracker)
+
+## 开始使用
+
+创建 Http Server 项目:`composer create-project imiphp/project-http`
+
+创建 WebSocket Server 项目:`composer create-project imiphp/project-websocket`
+
+创建 TCP Server 项目:`composer create-project imiphp/project-tcp`
+
+创建 UDP Server 项目:`composer create-project imiphp/project-udp`
+
+[完全开发手册](https://doc.imiphp.com)
+
+## 运行环境
+
+- Linux 系统 (Swoole 不支持在 Windows 上运行)
+- [PHP](https://php.net/) >= 7.1
+- [Composer](https://getcomposer.org/)
+- [Swoole](https://www.swoole.com/) >= 4.3.0
+- Redis、PDO 扩展
+
+## 版权信息
+
+imi 遵循 木兰宽松许可证(Mulan PSL v1) 开源协议发布,并提供免费使用。
+
+## 鸣谢
+
+感谢以下开源项目 (按字母顺序排列) 为 imi 提供强力支持!
+
+- [doctrine/annotations](https://github.com/doctrine/annotations) (PHP 注解处理类库)
+- [PHP](https://php.net/) (没有 PHP 就没有 imi)
+- [Swoole](https://www.swoole.com/) (没有 Swoole 就没有 imi)
+
+## 贡献者
+
+<a href="https://github.com/Yurunsoft/IMI/graphs/contributors"><img src="https://opencollective.com/IMI/contributors.svg?width=890&button=false" /></a>
+
+你想出现在贡献者列表中吗?
+
+你可以做的事(包括但不限于以下):
+
+* 纠正拼写、错别字
+* 完善注释
+* bug修复
+* 功能开发
+* 文档编写(<https://github.com/Yurunsoft/imidoc>)
+* 教程、博客分享
+
+> 最新代码以 `dev` 分支为准,提交 `PR` 也请合并至 `dev` 分支!
+
+提交 `Pull Request` 到本仓库,你就有机会成为 imi 的作者之一!
+
+## 关于测试脚本
+
+### 环境要求
+
+Redis、MySQL
+
+### 首次运行测试
+
+* 创建 `db_imi_test` 数据库,将 `tests/db/db.sql` 导入到数据库
+
+* 配置系统环境变量,如果默认值跟你的一样就无需配置了
+
+名称 | 描述 | 默认值
+-|-|-
+MYSQL_SERVER_HOST | MySQL 主机名 | 127.0.0.1 |
+MYSQL_SERVER_PORT | MySQL 端口 | 3306 |
+MYSQL_SERVER_USERNAME | MySQL 用户名 | root |
+MYSQL_SERVER_PASSWORD | MySQL 密码 | root |
+REDIS_SERVER_HOST | Redis 主机名 | 127.0.0.1 |
+REDIS_SERVER_PORT | Redis 端口 | 6379 |
+REDIS_SERVER_PASSWORD | Redis 密码 |  |
+REDIS_CACHE_DB | Redis 缓存用的 `db`,该 `db` 会被清空数据,请慎重设置 | 1 |
+
+配置命令:`export NAME=VALUE`
+
+* 首次运行测试脚本:`composer install-test`
+
+* 首次之后再运行测试的命令:`composer test`
+
+## 捐赠
+
+<img src="https://raw.githubusercontent.com/Yurunsoft/IMI/dev/res/pay.png"/>
+
+开源不求盈利,多少都是心意,生活不易,随缘随缘……

+ 30 - 0
frameworks/PHP/imi/benchmark_config.json

@@ -0,0 +1,30 @@
+{
+  "framework": "imi",
+  "tests": [
+    {
+      "default": {
+        "json_url": "/json",
+        "plaintext_url": "/plaintext",
+        "db_url": "/db",
+        "query_url": "/query?queries=",
+        "fortune_url": "/fortunes",
+        "update_url": "/update?queries=",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Fullstack",
+        "database": "MySQL",
+        "framework": "imi",
+        "language": "PHP",
+        "flavor": "None",
+        "orm": "Full",
+        "platform": "Swoole",
+        "webserver": "None",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "imi",
+        "notes": "",
+        "versus": "Swoole"
+      }
+    }
+  ]
+}

+ 10 - 0
frameworks/PHP/imi/composer.json

@@ -0,0 +1,10 @@
+{
+	"require": {
+		"yurunsoft/imi": "~1.0"
+	},
+    "autoload": {
+        "psr-4" : {
+			"ImiApp\\" : "./"
+        }
+	}
+}

+ 11 - 0
frameworks/PHP/imi/config/beans.php

@@ -0,0 +1,11 @@
+<?php
+return [
+    'hotUpdate'    =>    [
+        'status'    =>    false, // 关闭热更新去除注释,不设置即为开启,建议生产环境关闭
+    ],
+    'Logger'    =>    [
+        'coreHandlers'    =>    [
+            
+        ],
+    ],
+];

+ 67 - 0
frameworks/PHP/imi/config/config.php

@@ -0,0 +1,67 @@
+<?php
+$dbPoolConfig = [
+    // 池子中最多资源数
+    'maxResources' => 10000,
+    // 池子中最少资源数
+    'minResources' => 0,
+];
+$dbResourceConfig = [
+    'host'        => 'tfb-database',
+    'username'    => 'benchmarkdbuser',
+    'password'    => 'benchmarkdbpass',
+    'database'    => 'hello_world',
+];
+return [
+    // 项目根命名空间
+    'namespace'    =>    'ImiApp',
+
+    // 配置文件
+    'configs'    =>    [
+        'beans'        =>    __DIR__ . '/beans.php',
+    ],
+
+    // 扫描目录
+    'beanScan'    =>    [],
+
+    // 组件命名空间
+    'components'    =>  [],
+
+    // 主服务器配置
+    'mainServer'    =>    [
+        'namespace' =>  'ImiApp\ApiServer',
+        'type'      =>  Imi\Server\Type::HTTP,
+        'host'      =>  '0.0.0.0',
+        'port'      =>  8080,
+        'configs'   =>  [
+            'worker_num'        => swoole_cpu_num(),
+            'open_tcp_nodelay'  => true,
+            'tcp_fastopen'      => true,
+        ],
+    ],
+
+    'db'    => [
+        'defaultPool'   => 'db', // 默认连接池
+    ],
+    'pools' => [
+        // 连接池名称
+        'db' => [
+            'sync' => [
+                'pool'    =>    [
+                    'class'        =>    \Imi\Db\Pool\SyncDbPool::class,
+                    'config'    =>    $dbPoolConfig,
+                ],
+                // resource也可以定义多个连接
+                'resource'    =>    $dbResourceConfig,
+            ],
+            // 异步池子,worker进程使用
+            'async' => [
+                'pool'    =>    [
+                    'class'        =>    \Imi\Db\Pool\CoroutineDbPool::class,
+                    'config'    =>    $dbPoolConfig,
+                ],
+                // resource也可以定义多个连接
+                'resource'    =>    $dbResourceConfig,
+            ],
+        ],
+    ],
+];

+ 21 - 0
frameworks/PHP/imi/imi.dockerfile

@@ -0,0 +1,21 @@
+FROM php:7.3
+
+RUN apt -yqq update
+RUN apt -yqq install git
+
+RUN docker-php-ext-install pdo_mysql > /dev/null
+
+RUN pecl install swoole-4.4.6
+RUN docker-php-ext-enable swoole
+
+WORKDIR /imi
+
+COPY . /imi
+
+RUN chmod -R ug+rwx /imi/.runtime
+
+RUN curl -sSL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
+RUN composer install --no-dev --classmap-authoritative
+RUN composer dumpautoload -o
+
+CMD php vendor/bin/imi server/start -name main