jonbaldie/eleven

基于workerman的高性能微框架。

v0.2.1 2022-03-11 19:33 UTC

This package is auto-updated.

Last update: 2024-09-12 00:39:36 UTC


README

CircleCI

Eleven 是一个高性能的 PHP 微框架,用于快速开发 API。它受到 passwalls/mark 的启发,但采用了略有不同的方法。

安装

composer require jonbaldie/eleven

使用

<?php

use Eleven\App;

require 'vendor/autoload.php';

$api = new App('http://0.0.0.0:3000');

$api->count = 4; // process count

$api->any('/', function ($request) {
    return 'Hello world';
});

$api->get('/hello/{name}', function ($request, $name) {
    return "Hello $name";
});

$api->post('/user/create', function ($request) {
    return json_encode(['code' => 200 ,'message' => 'OKAY'], JSON_PRETTY_PRINT);
});

$api->start();

运行命令 php start.php start -d

现在访问 http://127.0.0.1:3000/hello/world 将显示 "Hello world"。

可用命令

# To restart the worker threads:
php start.php restart -d

# To stop the worker threads:
php start.php stop

# To get a pretty status table:
php start.php status

# To see a list of active connections:
php start.php connections

Docker

我在 thecodingmachine 的优秀 PHP 镜像 中使用了这个包。

docker run -it -p 3000:3000 -e PHP_EXTENSION_REDIS=1 -v (pwd):/usr/src/app thecodingmachine/php:8.1-v4-cli bash

root@a6a4143b05cb:/usr/src/app# php start.php start -d
Workerman[start.php] start in DAEMON mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.26          PHP version:8.1.1
------------------------------------------ WORKERS ------------------------------------------
proto   user            worker          listen                 processes    status
tcp     root            none            http://0.0.0.0:3000    4             [OK]
---------------------------------------------------------------------------------------------
Input "php start.php stop" to stop. Start success.

注意事项

将应用程序存储在内存中是获得巨大速度提升和难以置信的资源效率的好方法。

但如果你没有很好地管理应用程序的状态,那么你将开始看到一些奇怪的问题,其中一个会话中的数据泄露到另一个会话中。

这就是为什么我试图避免使用共享状态和在 PHP 中使用静态变量。这些在尝试快速构建项目时非常方便,但它们将在以后某个时候咬你,通常在你最没有准备的时候!

最大的痛点之一是用户会话数据。尝试使用 Cookie 或 Redis 等远程存储,以确保所有工作线程都在同一首赞美诗中高歌,并尽可能地使应用程序无状态。

当你遵循应用程序中纯、确定性状态的概念时,你会写出最好的代码,即使你没有完全订阅函数式编程的范例。

我喜欢函数式编程,并认为它是一种保持代码可测试和可维护的极好方式 - 我甚至根据这些原则创建了一个 单独的 PHP 框架项目

路线图

  • 基本功能
  • Composer 包发布
  • 测试套件设置
  • 静态分析设置
  • CircleCI 配置
  • v1.0.0 发布