定制 ensi/laravel-openapi-server-generator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ensi/laravel-openapi-server-generator

Composer 安装命令:

composer require ensi/laravel-openapi-server-generator

包简介

laravel openapi server generator

README 文档

README

Latest Version on Packagist Tests Total Downloads

Generates Laravel application code from Open Api Specification files

Installation

You can install the package via composer:

composer require ensi/laravel-openapi-server-generator --dev

Publish the config file with:

php artisan vendor:publish --provider="Ensi\LaravelOpenApiServerGenerator\LaravelOpenApiServerGeneratorServiceProvider"

and configure all the options.

Migrating from version 0.x.x

Delete config/openapi-server-generator.php, republish it using command above and recreate desired configuration.

Version Compatibility

Laravel OpenApi Server Generator Laravel PHP
^0.0.2 - ^0.8.2 ^7.x ^7.1.3
^0.8.3 - ^0.9.0 ^7.x || ^8.x ^7.1.3 || ^8.0
^1.0.0 - ^1.1.2 * ^8.0
^2.0.0 - ^3.0.3 * ^8.1
^4.0.0 ^9.x || ^10.x || ^11.x ^8.1

Basic Usage

Run php artisan openapi:generate-server. It will generate all the configured entities from you OAS3 files.
Override default_entities_to_generate configiration with php artisan openapi:generate-server -e routes,enums
Make output more versbose: php artisan openapi:generate-server -v

Overwriting templates

You can also adjust file templates according to your needs.

  1. Find the needed template inside templates directory in this repo;
  2. Copy it to to resources/openapi-server-generator/templates directory inside your application or configure package to use another directory via extra_templates_path option;
  3. Change whatever you need.

Existing entities and generators

'routes' => RoutesGenerator::class

Generates laravel route file (route.php) for each endpoint in oas3->paths
The following extension properties are used by this generator:

x-lg-handler: '\App\Http\Controllers\CustomersController@create' // Optional. Path is ignored if this field is empty. You can use :: instead of @ if you want
x-lg-route-name: 'createCustomer' // Optional. Translates to `->name('createCustomer')`
x-lg-middleware: '\App\Http\Middleware\Authenticate::class,web'  // Optional. Translates to `->middleware([\App\Http\Middleware\Authenticate::class, 'web'])`
x-lg-without-middleware: '\App\Http\Middleware\Authenticate::class,web'  // Optional. Translates to `->withoutMiddleware([\App\Http\Middleware\Authenticate::class, 'web'])`

route.php file IS overriden with each generation.
You should include it in your main route file like that:

$generatedRoutes = __DIR__ . "/OpenApiGenerated/routes.php";
if (file_exists($generatedRoutes)) { // prevents your app and artisan from breaking if there is no autogenerated route file for some reason.
    require $generatedRoutes;
}

'controllers' => ControllersGenerator::class

Generates Controller class for each non-existing class specified in x-lg-handler
Supports invocable Controllers.
If several openapi paths point to several methods in one Controller/Handler then the generated class includes all of them.
If a class already exists it is NOT overriden.
Controller class IS meant to be modified after generation.

'requests' => RequestsGenerator::class

Generates Laravel Form Requests for DELETE, PATCH, POST, PUT paths
Destination must be configured with array as namespace instead of string.
E.g.

'requests' => [
    'namespace' => ["Controllers" => "Requests"]
],

This means "Get handler (x-lg-handler) namespace and replace Controllers with Requests in it"
Form Request class IS meant to be modified after generation. You can treat it as a template generated with php artisan make:request FooRequest
If the file already exists it IS NOT overriden with each generation.

Form Request class name is ucFirst(operationId). You can override it with x-lg-request-class-name
You can skip generating form request for a give route with x-lg-skip-request-generation: true directive.

When generating a request, the Laravel Validation rules for request fields are automatically generated.
Validation rules generate based on the field type and required field.
For fields whose values should only take the values of some enum, you must specify the x-lg-enum-class option. E.g.: x-lg-enum-class: 'CustomerGenderEnum'.

'enums' => EnumsGenerator::class

Generates Enum class only for enums listed in oas3->components->schemas.
Your need to specify x-enum-varnames field in each enum schema. The values are used as enum constants' names.
Destination directory is cleared before generation to make sure all unused enum classes are deleted.
Enums generator does NOT support allOf, anyOf and oneOf at the moment.

'pest_tests' => PestTestsGenerator::class

Generates Pest test file for each x-lg-handler
You can exclude oas3 path from test generation using x-lg-skip-tests-generation: true.
If a test file already exists it is NOT overriden.
Test file class IS meant to be modified after generation.

'resources' => ResourcesGenerator::class

Generates Resource file for x-lg-handler
Resource properties are generated relative to field in response, which can be set in the config

'resources' => [
    'response_key' => 'data'
],

You can also specify response_key for resource: add x-lg-resource-response-key: data in object.
When specifying response_key, you can use the "dot" syntax to specify nesting, for example data.field
You can exclude resource generation using x-lg-skip-resource-generation: true in route.
You can rename resource Class using x-lg-resource-class-name: FooResource in resource object or properties object.
If a resource file already exists it is NOT overridden.
Resource file contains a set of fields according to the specification. You also need to specify mixin DocBlock to autocomplete resource.

'policies' => PoliciesGenerator::class

Generates Laravel Policies for routes. Destination must be configured with array as namespace instead of string. E.g:

'policies' => [
    'namespace' => ["Controllers" => "Policies"]
],
  • The path must contain a 403 response or the policy will not be generated.
  • You can exclude policy generation using x-lg-skip-policy-generation: true in route.
  • If a policy file already exists it is NOT overridden.

Contributing

Please see CONTRIBUTING for details.

Testing

  1. composer install
  2. composer test

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

License

The MIT License (MIT). Please see License File for more information.

ensi/laravel-openapi-server-generator 适用场景与选型建议

ensi/laravel-openapi-server-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 78.11k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2021 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ensi/laravel-openapi-server-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ensi/laravel-openapi-server-generator 我们能提供哪些服务?
定制开发 / 二次开发

基于 ensi/laravel-openapi-server-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 78.11k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-05