{"id":13699896,"url":"https://github.com/lizhichao/one","last_synced_at":"2025-05-15T07:05:56.358Z","repository":{"id":59974149,"uuid":"156318343","full_name":"lizhichao/one","owner":"lizhichao","description":"A minimalist high-performance php framework that supports the [swoole | php-fpm] environment","archived":false,"fork":false,"pushed_at":"2024-07-20T07:23:54.000Z","size":367,"stargazers_count":888,"open_issues_count":1,"forks_count":127,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-05-11T14:48:09.309Z","etag":null,"topics":["framework","one","orm","php","router","swoole"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lizhichao.png","metadata":{"files":{"readme":"README-CN.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://www.vicsdf.com/img/z.jpg","https://www.vicsdf.com/img/w.jpg"]}},"created_at":"2018-11-06T03:08:18.000Z","updated_at":"2025-05-01T11:18:13.000Z","dependencies_parsed_at":"2025-03-20T03:45:32.492Z","dependency_job_id":"08a02081-8be3-4f2e-9ed0-d7100f64a8b7","html_url":"https://github.com/lizhichao/one","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lizhichao%2Fone","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lizhichao%2Fone/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lizhichao%2Fone/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lizhichao%2Fone/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lizhichao","download_url":"https://codeload.github.com/lizhichao/one/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254292042,"owners_count":22046426,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","one","orm","php","router","swoole"],"created_at":"2024-08-02T20:00:45.421Z","updated_at":"2025-05-15T07:05:51.349Z","avatar_url":"https://github.com/lizhichao.png","language":"PHP","funding_links":["https://www.vicsdf.com/img/z.jpg","https://www.vicsdf.com/img/w.jpg"],"categories":["Frameworks","基础框架"],"sub_categories":["构建/部署"],"readme":"[English](https://github.com/lizhichao/one/blob/master/README.md) | [中文](https://github.com/lizhichao/one/blob/master/README-CN.md)\n\n## One - 一个极简高性能php框架，支持[swoole | php-fpm ]环境\n\n- 快   - 即使在`php-fpm`下也能`1ms`内响应请求\n- 简单 - 让你重点关心用`one`做什么，而不是怎么用`one`\n- 灵活 - 各个组件松耦合，可以灵活搭配使用，使用方法保持一致\n    - 原生sql可以和模型关系`with`搭配使用，关系可以跨数据库类型\n    - session 可以在http,websocket甚至tcp,udp和cli下使用\n    - ...\n- 高效 - 运行性能，开发效率，易维护。\n- 轻量 - 无其他依赖，从路由、orm所有组件代码量一共不超过500k，若二次开发没有复杂的调用关系，可快速掌握设计原理\n- 分库分表 - orm支持无线分库分表,上层使用方式依然保持一致\n    \nQQ交流群：731475644\n\n珍爱生命，抵制996\n\u003ca href=\"https://github.com/996icu/996.ICU/blob/master/LICENSE\"\u003e\u003cimg src=\"https://camo.githubusercontent.com/41215df7ff78cefe41536bf897fe1c7e55b10bd2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d416e74692532303939362d626c75652e737667\" alt=\"LICENSE\" data-canonical-src=\"https://img.shields.io/badge/license-Anti%20996-blue.svg\" style=\"max-width:100%;\"\u003e\u003c/a\u003e\n\n\n## hello world\n\n安装\n\n```shell\ncomposer create-project lizhichao/one-app app\ncd app\nphp App/swoole.php \n\n# 停止 ： `php App/swoole.php -o stop`  \n# 重启 ： `php App/swoole.php -o reload`  \n# 守护进程启动 ： `php App/swoole.php -o start`  \n\ncurl http://127.0.0.1:8081/\n```\n\n## 性能\n\n**参考：**\n\n* [性能测试1 (mysql + orm)](https://www.techempower.com/benchmarks/#section=test\u0026runid=57b25c85-082a-4013-b572-b0939006eaff\u0026hw=ph\u0026test=query\u0026d=dz\u0026a=2\u0026o=e)\n* [性能测试2](https://github.com/the-benchmarker/web-frameworks)\n\n\n## 主要组件\n\n- 路由\n    - 支持贪婪匹配和优先级\n    - 支持ws/tcp/http……任意协议\n    - 性能好，添加几万条路由也不会降低解析性能\n    - 路由分组，中间件……该有的都有\n- ORM模型\n    - 支持数据库：mysql,clickHouse，\n    - 关系处理：一对一，一对多，多对一，多态…… 各种关系的都有，可以跨数据库类型关联\n    - 缓存：自动刷新数据 支持配置各种缓存粒度\n    - 事件：所有操作都能捕获 包括你用原生sql操作数据库\n    - 数据库连接：同步、异步、阻塞、断线重连都支持\n    - sql模板： 自动生成模板id，可了解项目有哪些类型sql，以及调用次数占比情况，对后期数据优化提供数据支持。\n    - statement复用：提高sql执行性能\n    - 模型支持动态分库分表，支持海量数据\n- rpc\n    - 可自动生成远程方法映射，支持ide提示\n    - 直接调用映射方法 == 调用远程方法，支持链式调用\n    - 支持`rpc中间件`，鉴权、加解密、缓存……\n- 日志\n    - 信息完整：记录完整的文件名+行号可快速定位代码位置\n    - requestId：可轻松查看整个请求日志信息和服务关系\n\n- ...\n    - QQ交流群: 731475644\n\n\n## 路由\n\n```php\n\nRouter::get('/', \\App\\Controllers\\IndexController::class . '@index');\n\n// 带参数路由\nRouter::get('/user/{id}', \\App\\Controllers\\IndexController::class . '@user');\n\n// 路由分组 \nRouter::group(['namespace'=\u003e'App\\\\Test\\\\WebSocket'],function (){\n\t// websocket 路由\n    Router::set('ws','/a','TestController@abc'); \n    Router::set('ws','/b','TestController@bbb'); \n});\n\n// 中间件\nRouter::group([\n    'middle' =\u003e [\n        \\App\\Test\\MixPro\\TestMiddle::class . '@checkSession'\n    ]\n], function () {\n    Router::get('/mix/ws', HttpController::class . '@ws');\n    Router::get('/user/{id}', \\App\\Controllers\\IndexController::class . '@user');\n    Router::post('/mix/http/send', HttpController::class . '@httpSend');\n});\n\n```\n\n## orm 模型\n\n### 定义模型\n```php\nnamespace App\\Model;\n\nuse One\\Database\\Mysql\\Model;\n\n// 模型里面不需要指定主键，框架会缓存数据库结构\n// 自动匹配主键，自动过滤非表结构里的字段\nclass User extends Model\n{\n\t// 定义模型对应的表名\n    CONST TABLE = 'users';\n\n\t// 定义关系\n    public function articles()\n    {\n        return $this-\u003ehasMany('id',Article::class,'user_id');\n    }\n    \n    // 定义事件 \n    // 是否开启自动化缓存 \n    // ……\n}\n```\n\n### 使用模型\n\n- 在`fpm`下数据库连接为单列,\n- 在`swoole`模式下所有数据库操作自动切换为连接池\n\n```php\n// 查询一条记录\n$user = User::find(1);\n\n// 关联查询\n$user_list = User::whereIn('id',[1,2,3])-\u003ewith('articles')-\u003efindAll()-\u003etoArray();\n\n// 更新\n$r = $user-\u003eupdate(['name' =\u003e 'aaa']);\n// 或者\n$r = user::where('id',1)-\u003eupdate(['name' =\u003e 'aaa']);\n// $r 为影响记录数量\n\n```\n\n## 缓存\n```php\n// 设置缓存 无过期时间\nCache::set('ccc',1);\n\n// 设置缓存 1分钟过期\nCache::set('ccc',1,60);\n\n// 获取\nCache::get('ccc');\n\n// 或者 缓存ccc 过期10s 在tag1下面\nCache::get('ccc',function (){\n    return '缓存的信息';\n},10,['tag1']);\n\n// 刷新tag1下的所有缓存\nCache::flush('tag1');\n\n```\n        \n## HTTP/TCP/WEBSOCKET/UDP服务器\n\n启动一个websocket服务器，\n添加http服务监听，\n添加tcp服务监听\n\n```php\n\n[\n\t // 主服务器\n    'server' =\u003e [\n        'server_type' =\u003e \\One\\Swoole\\OneServer::SWOOLE_WEBSOCKET_SERVER,\n        'port' =\u003e 8082,\n        // 事件回调\n        'action' =\u003e \\One\\Swoole\\Server\\WsServer::class,\n        'mode' =\u003e SWOOLE_PROCESS,\n        'sock_type' =\u003e SWOOLE_SOCK_TCP,\n        'ip' =\u003e '0.0.0.0',\n        // swoole 服务器设置参数\n        'set' =\u003e [\n            'worker_num' =\u003e 5\n        ]\n    ],\n    // 添加监听\n    'add_listener' =\u003e [\n        [\n            'port' =\u003e 8081,\n            // 事件回调\n            'action' =\u003e \\App\\Server\\AppHttpPort::class,\n            'type' =\u003e SWOOLE_SOCK_TCP,\n            'ip' =\u003e '0.0.0.0',\n            // 给监听设置参数\n            'set' =\u003e [\n                'open_http_protocol' =\u003e true,\n                'open_websocket_protocol' =\u003e false\n            ]\n        ],\n        [\n            'port' =\u003e 8083,\n            // 打包 解包协议\n            'pack_protocol' =\u003e \\One\\Protocol\\Text::class,\n            // 事件回调\n            'action' =\u003e \\App\\Test\\MixPro\\TcpPort::class,\n            'type' =\u003e SWOOLE_SOCK_TCP,\n            'ip' =\u003e '0.0.0.0',\n            // 给监听设置参数\n            'set' =\u003e [\n                'open_http_protocol' =\u003e false,\n                'open_websocket_protocol' =\u003e false\n            ]\n        ]\n    ]\n];\n\n\n```\n\n## RPC\n\n像调用本项目的方法一样调用远程服务器的方法。跨语言，跨机器。\n\n### 服务端\n启动rpc服务，框架已经内置了各个协议的rpc服务，添加到到上面配置文件的`action`即可。列如: 支持`http`调用，又支持`tcp`调用。\n\n```php\n// http 协议 rpc服务\n[\n    'port'   =\u003e 8082,\n    'action' =\u003e \\App\\Server\\RpcHttpPort::class,\n    'type'   =\u003e SWOOLE_SOCK_TCP,\n    'ip'     =\u003e '0.0.0.0',\n    'set'    =\u003e [\n        'open_http_protocol'      =\u003e true,\n        'open_websocket_protocol' =\u003e false\n    ]\n],\n// tcp 协议 rpc服务\n[\n    'port'          =\u003e 8083,\n    'action'        =\u003e \\App\\Server\\RpcTcpPort::class,\n    'type'          =\u003e SWOOLE_SOCK_TCP,\n    'pack_protocol' =\u003e \\One\\Protocol\\Frame::class, // tcp 打包 解包协议\n    'ip'            =\u003e '0.0.0.0',\n    'set'           =\u003e [\n        'open_http_protocol'      =\u003e false,\n        'open_websocket_protocol' =\u003e false,\n        'open_length_check'       =\u003e 1,\n        'package_length_func'     =\u003e '\\One\\Protocol\\Frame::length',\n        'package_body_offset'     =\u003e \\One\\Protocol\\Frame::HEAD_LEN,\n    ]\n]\n```\n添加具体服务到rpc，\n例如有个类`Abc`\n\n```php \n\nclass Abc\n{\n    private $a;\n\n    // 初始值\n    public function __construct($a = 0)\n    {\n        $this-\u003ea = $a;\n    }\n\n    // 加法\n    public function add($a, $b)\n    {\n        return $this-\u003ea + $a + $b;\n    }\n\n    public function time()\n    {\n        return date('Y-m-d H:i:s');\n    }\n\n    // 重新设初始值\n    public function setA($a)\n    {\n        $this-\u003ea = $a;\n        return $this;\n    }\n}\n\n```\n把`Abc`添加到rpc服务\n\n```php\n\n// 添加Abc到rpc服务\nRpcServer::add(Abc::class);\n\n// 如果你不希望把Abc下的所有方法都添加到rpc服务，也可以指定添加。\n// 未指定的方法客户端无法调用.\n//RpcServer::add(Abc::class,'add');\n\n// 分组添加\n//RpcServer::group([\n//    // 中间件 在这里可以做 权限验证 数据加解密 等等\n//    'middle' =\u003e [\n//        TestMiddle::class . '@aa'\n//    ],\n//    // 缓存 如果设置了 当以同样的参数调用时 会返回缓存信息 不会真正调用 单位:秒\n//    'cache'  =\u003e 10\n//], function () {\n//    RpcServer::add(Abc::class);\n//    RpcServer::add(User::class);\n//});\n```\n\n### 客户端调用\n\n为了方便调用我们建立一个映射类（one框架可自动生成）\n\n```php\nclass ClientAbc extends RpcClientHttp {\n\n    // rpc服务器地址\n    protected $_rpc_server = 'http://127.0.0.1:8082/';\n\n    // 远程的类 不设置 默认为当前类名\n    protected $_remote_class_name = 'Abc';\n}\n```\n调用rpc服务的远程方法， 和调用本项目的方法一样的。你可以想象这个方法就在你的项目里面。\n\n```php\n$abc = new ClientAbc(5);\n\n// $res === 10\n$res = $abc-\u003eadd(2,3);\n\n// 链式调用 $res === 105\n$res = $abc-\u003esetA(100)-\u003eadd(2,3);\n\n// 如果把上面的模型的User添加到rpc\n// RpcServer::add(User::class);\n// 下面运行结果和上面一样\n// $user_list = User::whereIn('id',[1,2,3])-\u003ewith('articles')-\u003efindAll()-\u003etoArray();\n\n```\n\n上面是通过http协议调用的。你也可以通过其他协议调用。例如Tcp协议\n\n```php\nclass ClientAbc extends RpcClientTcp {\n\n    // rpc服务器地址\n    protected $_rpc_server = 'tcp://127.0.0.1:8083/';\n\n    // 远程的类 不设置 默认为当前类名\n    protected $_remote_class_name = 'Abc';\n}\n```\n\n其中类 `RpcClientHttp`,`RpcClientTcp`在框架里。   \n你也可以复制到任何其他地方使用。\n\n    \n## 更多\n\n* [各种协议通讯列子](https://github.com/lizhichao/one-demo)\n* [rpc使用例子](https://github.com/lizhichao/one-app/tree/test_rpc)\n* [分布式长连接（tcp）例子](https://github.com/lizhichao/one-app/tree/cloud_demo)\n* [Actor例子](https://github.com/lizhichao/one-app/tree/actor_demo)\n\n## 文档\n\n* [文档地址](https://github.com/lizhichao/one-doc/blob/master/SUMMARY.md)\n* [参数验证器](https://segmentfault.com/a/1190000018434298)\n\n\n## TODO\n\n* 支持[Workerman](https://github.com/walkor/Workerman)\n* orm支持[postgresql](https://www.postgresql.org/)\n\nQQ交流群: 731475644\n\n## 我的其他开源项目\n* [纯php的分词](https://github.com/lizhichao/VicWord)\n* [clickhouse tcp 客户端](https://github.com/lizhichao/one-ck)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flizhichao%2Fone","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flizhichao%2Fone","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flizhichao%2Fone/lists"}