peroxide/api-skeleton

v1.4.2 2024-04-18 01:36 UTC

This package is not auto-updated.

Last update: 2024-09-19 04:13:30 UTC


README

API骨架:基于Slim和OpenSwoole构建,我们的API骨架专为微服务项目设计,优先考虑性能和简单性。

如何安装

使用composer安装骨架

# install using openswoole binaries
$  docker run \
        -u 1000:1000 \
        -w /var/www \
        -v .:/var/www --rm \
         openswoole/swoole:php8.3-dev \
         composer create-project peroxide/api-skeleton <your_project_dir>

或者

         
$ composer create-project \
      --ignore-platform-reqs \
      peroxide/api-skeleton <your_project_dir>

请注意:如果没有安装OpenSwoole PHP扩展,不使用--ignore-platform-reqs标志运行Composer可能会导致错误。

设置

创建你的本地.env文件

$ cp .env.example .env

要启动本地服务器,你需要将DockerCompose在你的系统用户下运行。不需要复杂的命令,只需确保Docker和Compose可以正常运行。

docker compose运行

$ docker compose up -d

测试

健康检查路由

$ curl https://:8085/check-health

创建你的代码

路由器

可以在config/routes.php中声明路由,你可以像示例一样自由组合你的路由文件

// config/routes.php file
return (function (App $app) {
    // Activate Global CORS
    $app = (new GlobalCors($app))->getDecorated();

    $app->get('/health-check', HealthCheckController::class);

    // Your routes definitions here
    // You can config route in your preferred directory
    $app = require __DIR__ . '/../app/Products/routes.php';
    $app = require __DIR__ . '/../app/Users/routes.php';
    $app = require __DIR__ . '/../app/Sales/routes.php';

    // Or you can config group routes here:
    $app->group('/cart', function (Slim\Routing\RouteCollectorProxy $group) {
        $group->get('/products', YourDependencyReference::class);
        // ...
    })->add(new AMiddlewareAction());

    /**
     * Catch-all route to serve a 404 Not Found page if none of the routes match
     * NOTE: make sure this route is defined last
     */
    $app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function ($request, $response) {
        throw new HttpNotFoundException($request);
    });

    return $app;
})($app);

有关更多详细信息,请阅读Slim 4 路由解析页面

https://slim.php.ac.cn/docs/v4/objects/routing.html

注意:所有路由都由路由器配置中指定的方法管理,这些方法利用具有'__invoke'方法的可调用类、简单方法或可调用函数。

我们的容器

我们开发了简单的容器组件,命名为peroxide/container。使用它,将依赖注入到你的类中变得轻而易举:控制器、用例、命令、DTO和领域,以声明的方式。要了解更多信息,请查看文档

https://github.com/Peroxide-PHP/peroxide-container

容器解析遵循PSR-11标准,提供了根据你自己的偏好调整容器机制的灵活性,你甚至可以在需要时完全替换它。

迁移

我们提供了一个名为Phinx的迁移组件,这是一个独立的库,用于实现迁移,而不依赖于任何特定的框架。

以下是它的用法

# outside the container
# read about phinx
./exec composer phinx

# creating a migration
./exec composer phinx create YourMigrationActionInCamelCase

# a file like database/migrations/20240418001416_your_migration_action_in_camel_case.php
# will be created

# After you migration file config run
./exec composer migrate

# And your migration will be executed

# To undo the last migration just run
./exec composer rollback

迁移类的示例

final class CreateUsersTableTest extends AbstractMigration
{
    public function change(): void
    {
        $table = $this->table('users');
        $table
            ->addColumn('email', 'string', ['limit' => 130])
            ->addColumn('created_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
            ->addColumn('updated_at', 'datetime', ['null' => true])
            ->create();
    }
}

有关Phinx的更多信息,请参阅:https://book.cakephp.com.cn/phinx/0/en/migrations.html

播种者

会话正在建设中...

质量工具

代码嗅探器

./exec composer sniff

CS修复器

./exec composer fix

使用PHPUnit进行测试

./exec composer test

PHPStan

./exec composer stan

混乱探测器

./exec composer md

性能基准测试

即将推出...

注意!

此骨架服务器在OpenSwoole运行时上运行,通过协程执行。

要注意套接字通信,使用PDO与Postgres可以阻塞事件循环,而使用PDO与MySQL是兼容的(swoole为你处理通信)。

对于Postgres,请考虑使用Postgres客户端。在实施之前,务必仔细检查任何其他通信方法,以预防未来的任何意外问题。

有关阻塞事件循环的更多信息,请参阅文档:https://openswoole.com/

建议或PR

请随意将项目分支并提交一个pull request到develop分支。我们欢迎你提出的任何建议或合作。