{"id":18258081,"url":"https://github.com/guanhui07/dcr-swoole","last_synced_at":"2025-04-04T18:31:32.627Z","repository":{"id":65685213,"uuid":"596520203","full_name":"guanhui07/dcr-swoole","owner":"guanhui07","description":"💪高性能PHP Framework ，Cli模式，基于Swoole实现，常驻内存，协程，支持容器、切面、PHP8反射特性attributes实现了路由注解、中间件注解、支持Laravel Orm等特性   ","archived":false,"fork":false,"pushed_at":"2024-08-23T14:42:20.000Z","size":359,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-23T16:18:44.065Z","etag":null,"topics":["annotation","command","container","coroutine","crontab","dependency-injection-container","event","http","illuminate-database","middleware","mysql","orm-framework","rabbitmq","redis","swoole","swoole-framework","websocket"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/dcrswoole/framework","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/guanhui07.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2023-02-02T11:15:17.000Z","updated_at":"2024-08-23T14:42:24.000Z","dependencies_parsed_at":"2024-08-23T16:05:22.539Z","dependency_job_id":"406488c7-ce9e-4767-a26f-763b86891def","html_url":"https://github.com/guanhui07/dcr-swoole","commit_stats":{"total_commits":147,"total_committers":2,"mean_commits":73.5,"dds":"0.020408163265306145","last_synced_commit":"139b8f8f30f5c14e8ef742536a40f58a875e074b"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guanhui07%2Fdcr-swoole","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guanhui07%2Fdcr-swoole/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guanhui07%2Fdcr-swoole/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guanhui07%2Fdcr-swoole/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/guanhui07","download_url":"https://codeload.github.com/guanhui07/dcr-swoole/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247229399,"owners_count":20905042,"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":["annotation","command","container","coroutine","crontab","dependency-injection-container","event","http","illuminate-database","middleware","mysql","orm-framework","rabbitmq","redis","swoole","swoole-framework","websocket"],"created_at":"2024-11-05T10:28:57.735Z","updated_at":"2025-04-04T18:31:32.621Z","avatar_url":"https://github.com/guanhui07.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 整合各种包，基于Swoole 实现的框架\n\n\n-  laravel orm \n-  restful route\n-  predis\n-  guzzle\n-  monolog\n- http websocket\n- rabbitmq\n- container\n- event\n- middleware 支持注解配置中间件\n- validate\n- crontab\n- command\n- collection\n- carbon\n- dotenv config\n- 支持注解配置路由 注解配置中间件\n- aop 切面\n- process \n\n### 安装\n```\ncomposer create-project dcrswoole/framework skeleton\ncp .env.example  .env\n```\n\n### 分层 (demo未按此方式)\ncontroller-\u003eservice -\u003erepository-\u003emodel\n\n### http:\n\n```\nphp ./bin/start.php http:start \n```\n\n### websocket:\n\n```\nphp ./bin/start.php ws:start \n```\n\n### console:\n\n```\nphp artisan test\n```\n\n### crontab:\n\n```\n/config/crontab.php  enable 改为 true 开启\n```\n\n### migrate:\n\n```shell\nphp migrate.php  migrations:generate\nphp migrate.php migrations:migrate\n\n```\n\n### container\n\n```php\nuse DcrSwoole\\Utils\\ApplicationContext;\n\nApplicationContext::getContainer();\n//或 di();\n\n\n```\n\n## 路由注解和中间件注解\n```php\n    #[RequestMapping(methods: \"GET , POST\" , path:\"/api/json\")]\n    #[Middlewares(AuthMiddleware::class , TestMiddleware::class)]\n    public function test()\n    {\n        return 'hello';\n    }\n```\n\n\n## 路由注解 和 中间件注解 以及Inject注解  使用\n```php\n\u003c?php\ndeclare(strict_types=1);\nnamespace App\\Controller;\n\nuse App\\Middleware\\AuthMiddleware;\nuse App\\Middleware\\TestMiddleware;\nuse App\\Service\\TestService;\nuse DcrSwoole\\Annotation\\Mapping\\Middlewares;\nuse DcrSwoole\\Annotation\\Mapping\\RequestMapping;\nuse DI\\Attribute\\Inject;\n\nclass MiddlewareController extends Controller\n{\n    #[Inject]\n    public TestService $testService;\n    \n    #[RequestMapping(methods: \"GET , POST\", path:\"/test/middleware\")]\n    #[Middlewares(AuthMiddleware::class, TestMiddleware::class)]\n    public function test()\n    {\n        return 'hello world';\n    }\n}\n```\n\n## 多个中间件注解\n```php\n    use App\\Middleware\\AuthMiddleware;\n    use App\\Middleware\\TestMiddleware;\n    use DcrSwoole\\Annotation\\Mapping\\Middlewares;\n    use DcrSwoole\\Annotation\\Mapping\\RequestMapping;\n\n    #[RequestMapping(methods: \"GET , POST\", path:\"/test/middleware\")]\n    #[Middlewares(AuthMiddleware::class, TestMiddleware::class)]\n    public function test(): string\n    {\n        return 'ok';\n    }\n```\n\n\n## 从容器 拿对象 获取 参数\n```php\nuse DcrSwoole\\Utils\\ApplicationContext;\n\n\n//-\u003eall()  -\u003eget()  -\u003epost() 等方法\nApplicationContext::getContainer()-\u003eget(Request::class)-\u003eall();\n//di()-\u003e(Request::class)-\u003eall();\n```\n\n## redis 操作\n```php\nuse DcrSwoole\\Utils\\ApplicationContext;\n\n\nApplicationContext::getContainer()-\u003eget(DataRedis::class);\n//-\u003esetex -\u003eget -\u003edel -\u003esetnx 等方法 和predis一致\n```\n\n## orm model ，使用和laravel orm一致\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Model;\n\nuse DcrSwoole\\DbConnection\\Model;\n/**\n * Class UserModel\n * @see https://github.com/illuminate/database\n * @property int $id\n * @property string $created_at\n */\nclass UserModel extends Model\n{\n    protected $table = 'user';\n}\n\n\n```\n\n### 控制器validate\n```php\n    #[RequestMapping(methods: \"GET , POST\", path:\"/test/test4\")]\n    public function test4($request, $response)\n    {\n        $validate = Validation::check($this-\u003erequest-\u003epost ?? [], [\n            // add rule\n            ['title', 'min', 40],\n            ['freeTime', 'number'],\n        ]);\n\n        if ($validate-\u003eisFail()) {\n            var_dump($validate-\u003egetErrors());\n            var_dump($validate-\u003efirstError());\n        }\n\n        // $postData = $v-\u003eall(); // 原始数据\n        $safeData = $validate-\u003egetSafeData(); // 验证通过的安全数据\n\n        return $safeData;\n    }\n```\n\n### 获取配置 需要`use DI\\Attribute\\Inject`\n```php\n    #[Inject]\n    public Config $config;\n\n    #[RequestMapping(methods: \"GET , POST\", path:\"/test/config\")]\n    public function config()\n    {\n        //di()-\u003eget(Config::class)-\u003eget('app.debug');\n        return $this-\u003econfig-\u003eget('app.debug');\n    }\n```\n\n## 中间件\n```php\n\u003c?php\nnamespace App\\Middleware;\nuse App\\Middleware\\Contract\\MiddlewareInterface;\nuse DcrSwoole\\Log\\LogBase;\nuse DcrSwoole\\Request\\Request;\nclass TestMiddleware implements MiddlewareInterface\n{\n    public function handle()\n    {\n        return static function ($request, $next) {\n            $data = di()-\u003eget(Request::class)-\u003eget();\n//            throw new RuntimeException('test middlere error');\n            return $next-\u003ehandle($request);\n        };\n    }\n}\n\n```\n\n\n### Console 命令应用 需要在`app/Console/Kernel.php` 配置 命令类\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Console\\Command;\n\nuse App\\Repository\\TestRepository;\nuse DcrSwoole\\Utils\\ApplicationContext;\nuse Inhere\\Console\\IO\\Input;\nuse Inhere\\Console\\IO\\Output;\nuse Toolkit\\PFlag\\FlagsParser;\n\n/**\n * @package app\\Console\\Command\n * php artisan test\n */\nclass TestCommand extends \\Inhere\\Console\\Command\n{\n    protected static string $name = 'test';\n\n    protected static string $desc = 'print system ENV information';\n\n    protected function configFlags(FlagsParser $fs): void\n    {\n        // 绑定选项\n        $fs-\u003eaddOptByRule('update, up', 'bool;update linux command docs to latest');\n        $fs-\u003eaddOptByRule('init, i', 'bool;update linux command docs to latest');\n        $fs-\u003eaddOptByRule('search, s', 'string;input keywords for search');\n\n        // 绑定参数\n        // - 这里没有设置必须 可以不传，获取到就是空string\n        $fs-\u003eaddArg('keywords', 'the keywords for search or show docs', 'string');\n    }\n\n    protected function execute(Input $input, Output $output)\n    {\n        $keywords = $this-\u003eflags-\u003egetOpt('search', 23);\n\n        ApplicationContext::getContainer()-\u003eget(TestRepository::class)-\u003efromRepos();\n        $output-\u003einfo(\"hello world ...\");\n    }\n}\n\n\n```\n\n\n### crontab定时任务 需要在 `/config/crontab.php` 配置 定时任务\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Crontab;\n\nuse App\\Crontab\\Contract\\CrontabInterface;\nuse App\\Repository\\TestRepository;\nuse DcrSwoole\\Utils\\ApplicationContext;\n\nclass TestCrontab implements CrontabInterface\n{\n    public function execute(): void\n    {\n        ApplicationContext::getContainer()-\u003eget(TestRepository::class)-\u003etest1();\n    }\n}\n\n```\n\n### event 事件\n```php\n\u003c?php\n\nnamespace App\\Event;\nuse Symfony\\Contracts\\EventDispatcher\\Event;\n\nclass TestEvent extends Event\n{\n    public const NAME = 'order.placed';\n    //推荐使用对象 比如模型对象或dto object\n    protected $params;\n    public function __construct($params)\n    {\n        $this-\u003eparams = $params;\n    }\n    public function getParams()\n    {\n        return $this-\u003eparams;\n    }\n}\n\n\n```\n### listener 监听者\n```php\n\u003c?php\nnamespace App\\Listener;\nuse App\\Event\\TestEvent;\nuse App\\Listener\\Contract\\BaseListenerInterface;\nclass TestEventListener implements BaseListenerInterface\n{\n    /**\n     * @param TestEvent $event\n     */\n    public function process(object $event)\n    {\n        echo '打印参数'.PHP_EOL;\n        var_dump($event-\u003egetParams());\n    }\n}\n\n\n```\n\n### 触发事件 \n```php\n    #[RequestMapping(methods: \"GET , POST\", path:\"/test/event\")]\n    public function event($request, $response): array\n    {\n        $params = [\n            'test' =\u003e 23,\n        ];\n        event(new TestEvent($params),TestEvent::NAME);\n        // 初始化事件分发器\n        return [];\n    }\n```\n\n或者\n```php\n    #[RequestMapping(methods: \"GET , POST\", path:\"/test/event\")]\n    public function event($request, $response): array\n    {\n        $params = [\n            'test' =\u003e 23,\n        ];\n        $dispatcher = EventInstance::instance();\n        $dispatcher-\u003edispatch(new TestEvent($params), TestEvent::NAME);\n    }\n```\n\n## 切面 aop\n```php\n\u003c?php\nnamespace App\\Aspect;\n\nuse App\\Service\\UserService;\nuse Hyperf\\Di\\Aop\\ProceedingJoinPoint;\nuse Hyperf\\Di\\Exception\\Exception;\n\n/**\n * 需要再config/config 文件配置 切面类  \n */\nclass DebugAspect\n{\n    //要切入的方法 \n    public array $classes = [\n        UserService::class . '::first',\n    ];\n\n    /**\n     * 测试切面\n     * @param ProceedingJoinPoint $proceedingJoinPoint\n     * @return mixed\n     */\n    public function process(ProceedingJoinPoint $proceedingJoinPoint)\n    {\n        var_dump(11);\n        try {\n            return $proceedingJoinPoint-\u003eprocess();\n        } catch (Exception $e) {\n        }\n    }\n}\n\n```\n\n## 文档\n[更多文档](https://github.com/guanhui07/dcr-swoole/tree/master/doc)\n\n[我的博客文章-介绍](https://guanhui07.github.io/blog/#/posts/823)\n\n### 更多例子查看 代码demo\n\n### ab 本机macbook pro 压测 Requests per second:    58832.04 [#/sec] (mean)\n```\n→ ab -k  -n 100000  -c 30 'http://127.0.0.1:9501/test/request'\nThis is ApacheBench, Version 2.3 \u003c$Revision: 1879490 $\u003e\nCopyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/\nLicensed to The Apache Software Foundation, http://www.apache.org/\n\nBenchmarking 127.0.0.1 (be patient)\nCompleted 10000 requests\nCompleted 20000 requests\nCompleted 30000 requests\nCompleted 40000 requests\nCompleted 50000 requests\nCompleted 60000 requests\nCompleted 70000 requests\nCompleted 80000 requests\nCompleted 90000 requests\nCompleted 100000 requests\nFinished 100000 requests\n\n\nServer Software:        swoole-http-server\nServer Hostname:        127.0.0.1\nServer Port:            9501\n\nDocument Path:          /test/request\nDocument Length:        4 bytes\n\nConcurrency Level:      30\nTime taken for tests:   1.700 seconds\nComplete requests:      100000\nFailed requests:        0\nKeep-Alive requests:    100000\nTotal transferred:      15600000 bytes\nHTML transferred:       400000 bytes\nRequests per second:    58832.04 [#/sec] (mean)\nTime per request:       0.510 [ms] (mean)\nTime per request:       0.017 [ms] (mean, across all concurrent requests)\nTransfer rate:          8962.69 [Kbytes/sec] received\n\nConnection Times (ms)\n              min  mean[+/-sd] median   max\nConnect:        0    0   0.0      0       1\nProcessing:     0    1   0.3      0      15\nWaiting:        0    0   0.3      0      15\nTotal:          0    1   0.3      0      15\nERROR: The median and mean for the processing time are more than twice the standard\n       deviation apart. These results are NOT reliable.\nERROR: The median and mean for the total time are more than twice the standard\n       deviation apart. These results are NOT reliable.\n\nPercentage of the requests served within a certain time (ms)\n  50%      0\n  66%      1\n  75%      1\n  80%      1\n  90%      1\n  95%      1\n  98%      1\n  99%      2\n 100%     15 (longest request)\n```\n\n## wrk 压测  Requests/sec:  79540.12\n```\n→ wrk -t20 -c300 -d 30s --latency   http://127.0.0.1:9501/test/request\nRunning 30s test @ http://127.0.0.1:9501/test/request\n  20 threads and 300 connections\n  Thread Stats   Avg      Stdev     Max   +/- Stdev\n    Latency     7.56ms   14.05ms 154.35ms   88.96%\n    Req/Sec     4.71k     1.25k   18.48k    82.37%\n  Latency Distribution\n     50%    1.43ms\n     75%    6.30ms\n     90%   23.63ms\n     99%   68.04ms\n  2394042 requests in 30.10s, 356.17MB read\n  Socket errors: connect 67, read 86, write 0, timeout 0\nRequests/sec:  79540.12\nTransfer/sec:     11.83MB\n```\n\n### composer依赖组件\n\n```\n     \"symfony/event-dispatcher\": \"^6.2\",  事件监听 观察者模式\n    \"doctrine/migrations\": \"^3.5\",  migrate\n    \"elasticsearch/elasticsearch\": \"7.16\",  es\n    \"firebase/php-jwt\": \"^6.3\",   jwt token \n    \"gregwar/captcha\": \"^1.1\",  captcha \n    \"guanhui07/database\": \"^1.0\",   laravel orm 改\n    \"guanhui07/dcr-swoole-crontab\": \"^1.0\",  crontab\n    \"guanhui07/guzzle\": \"^1.0\",   guzzle client \n    \"guanhui07/redis\": \"^1.0\",   redis pool\n    \"inhere/console\": \"^4.1\",    console command \n    \"inhere/php-validate\": \"^2.8\",   validate 验证器\n    \"intervention/image\": \"^2.7\",   image操作\n    \"middlewares/utils\": \"^3.0\",    middleware中间件\n    \"monolog/monolog\": \"^2.8\",     monolog  \n    \"mwangithegreat/faker\": \"^1.9\",   faker造数据\n    \"nesbot/carbon\": \"^2.6\",     carbon time\n    \"nikic/fast-route\": \"^1.3\",   nikic的 resful route\n    \"opis/closure\": \"^3.6\",      闭包序列化\n    \"php-amqplib/php-amqplib\": \"dev-master\",   rabbitmq\n    \"php-di/php-di\": \"^7.0\",   依赖注入 di container \n    \"qiniu/php-sdk\": \"^7.7\",  七牛cdn\n    \"spatie/image\": \"^2.2\",   \n    \"symfony/finder\": \"^5.0\",   symfony finder\n    \"vlucas/phpdotenv\": \"^5.4\"  dotenv读取 \n     \"hyperf/aop-integration\": \" ^1.1\",    aop切面支持 \n```\n\n## 关联\n\n参考 hyperf laravel webman 项目\n\nhttps://github.com/guanhui07/dcr  fpm以及workerman实现websocket\n\nhttps://github1s.com/walkor/webman-framework\n\nhttps://github1s.com/hyperf/hyperf\n\nhttps://github1s.com/laravel/laravel\n\nhttps://github.com/SerendipitySwow/Serendipity-job\n\nhttps://github.com/sunsgneayo/annotation 路由注解参考\n\n\n### todo:\n类似`hyperf`实现 Command Crontab AutoController Cacheable 等注解\n\n\n## 我的其他包：\nhttps://github.com/guanhui07/dcr  借鉴Laravel实现的 PHP Framework ，FPM模式、websocket使用的workerman、支持容器、PHP8特性attributes实现了路由注解、中间件注解、Laravel Orm等特性\n\nhttps://github.com/guanhui07/redis Swoole模式下 Redis连接池\n\nhttps://github.com/guanhui07/facade  facade、门面 fpm模式下可使用\n\nhttps://github.com/guanhui07/dcr-swoole-crontab 基于swoole实现的crontab秒级定时任务\n\nhttps://github.com/guanhui07/timer php定时器，参考了workerman源码 实现一个单进程(守护进程)的定时器。\n\nhttps://github.com/guanhui07/database  基于 illuminate/database 做的连接池用于适配Swoole的协程环境\n\nhttps://github.com/guanhui07/dcr-swoole  高性能PHP Framework ，Cli模式，基于Swoole实现，常驻内存，协程框架，支持容器、切面、PHP8特性attributes实现了路由注解、中间件注解、支持Laravel Orm等特性\n\nhttps://github.com/guanhui07/geohash  geohash 用于附近的人\n\nhttps://github.com/guanhui07/open-ai  open-ai  chatgpt调用\n\nhttps://github.com/guanhui07/geohash  附近的人\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguanhui07%2Fdcr-swoole","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguanhui07%2Fdcr-swoole","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguanhui07%2Fdcr-swoole/lists"}