uraankhayayaal/openapi-generator-lumen
Composer 安装命令:
composer require uraankhayayaal/openapi-generator-lumen
包简介
Openapi auto generator for lumen using php reflection module
README 文档
README
Installation
- Install the package via Composer:
composer require uraankhayayaal/openapi-generator-lumen
- Register the service provider in
bootstrap/app.php:
$app->register(Uraankhayayaal\OpenapiGeneratorLumen\Providers\OpenApiGeneratorProvider::class);
- Add the openapi generator command to your
app/Console/Kernel.phpfile:
protected $commands = [ \Uraankhayayaal\OpenapiGeneratorLumen\Console\Commands\OpenApiGeneratorCommand::class, ];
Usage
Requests
We have to define request body params and query params for automatically generating OpenAPI.
- Query params — use a request extended from
Uraankhayayaal\OpenapiGeneratorLumen\Http\Requests\BaseRequestQueryDatato define Query params like the following example:
<?php namespace App\Http\Requests; use Uraankhayayaal\OpenapiGeneratorLumen\Http\Requests\BaseRequestQueryData; final class ConfirmQuery extends BaseRequestQueryData { public string $hash; public function rules(): array { return [ 'hash' => 'required|max:64', ]; } public function messages(): array { return []; } }
Use in controller like the following:
use App\Http\Requests\ConfirmQuery; ... public function confirm(ConfirmQuery $query): UserResource { ... $hash = $query->hash; ... }
- Body params — extend your POST, PUT, PATCH, or HEAD request that has a body from
Uraankhayayaal\OpenapiGeneratorLumen\Http\Requests\BaseRequestFormDatalike the following example:
<?php namespace App\Http\Requests; use OpenApi\Attributes as OA; use Uraankhayayaal\OpenapiGeneratorLumen\Http\Requests\BaseRequestFormData; final class RegisterForm extends BaseRequestFormData { public string $username; public string $email; public string $phone; public string $password; public bool $isAgreeMarketing; public bool $isAgreePolicy; #[OA\Property(property: 'file', type: 'string', format: 'binary', nullable: false)] // Use attributes or annotations for override public $file, public function rules(): array { return [ 'username' => 'required|max:255', 'email' => 'required|max:255', 'phone' => 'required|max:255', 'password' => 'required', 'isAgreeMarketing' => 'required|boolean', 'isAgreePolicy' => 'required|boolean', 'file' => 'file', ]; } public function messages(): array { return [ 'email.required' => 'Email is required', ]; } /** * @return \Illuminate\Http\UploadedFile|\Illuminate\Http\UploadedFile[]|array|null */ public function file(): mixed { return app('request')->file('file'); } }
Use in controller like the following:
use App\Http\Requests\RegisterForm; ... public function register(RegisterForm $registerForm): JsonResource { ... $username = $registerForm->username; ... }
Responses
Extend your responses from Illuminate\Http\Resources\Json\JsonResource like the following example:
<?php namespace App\Http\Responses; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; class UserResource extends JsonResource { /** @param string[] $roles */ public function __construct( public int $id, public int $status, public string $email, public string $phone, public array $roles, public int $createdAt, public int $updatedAt, ) {} /** * Transform the resource into an array. * * @return array<string, mixed> */ public function toArray(Request $request): array { return [ 'id' => $this->id, 'status' => $this->status, 'email' => $this->email, 'phone' => $this->phone, 'roles' => $this->roles, 'createdAt' => $this->createdAt, 'updatedAt' => $this->updatedAt, ]; } }
Use the response in your controller like the following:
use App\Http\Responses\UserResource; ... public function register(): UserResource { ... }
Generate
And just use:
php artisan openapi:generate
Customize, own config
Create a config file in config/openapi-generator.php with content:
<?php return [ 'title' => 'API DOCUMENTATION', 'version' => '0.0.1', 'servers' => [ 'local' => [ 'host' => 'http://localhost:8000', 'description' => 'Local API server', ], 'prod' => [ 'host' => 'https://example.domain', 'description' => 'Production API server', ], ], 'auth_middleware' => 'auth', 'export' => [ 'path' => './openapi.json', 'format' => 'json', ], ];
And add the config in bootstrap/app.php for overriding package config values:
$app->configure('openapi-generator');
uraankhayayaal/openapi-generator-lumen 适用场景与选型建议
uraankhayayaal/openapi-generator-lumen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 uraankhayayaal/openapi-generator-lumen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 uraankhayayaal/openapi-generator-lumen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 35
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-23