maxbeckers/php-openapi-generator
最新稳定版本:0.1.2
Composer 安装命令:
composer require maxbeckers/php-openapi-generator
包简介
Parse OpenAPI specs with PHP
README 文档
README
Generate strongly typed PHP 8.2+ models, server contracts, and API clients directly from OpenAPI 3.x specs.
This package is designed for teams that want generated code to feel like hand-written application code: explicit types, predictable method signatures, and minimal runtime magic.
Why This Project
- PHP-native workflow: no npm toolchain required for day-to-day generation
- Lightweight by design: generate plain PHP classes/interfaces you can read and own
- Framework-friendly: Symfony and Laravel server glue, plus multiple client adapters
- Great for contract-first teams: keep OpenAPI as source of truth and regenerate safely
Features
- Typed model generation from
components/schemas(classes, enums, nested objects) - Server contract generation from
pathswith selectable framework target (None,Symfony,Laravel) - Client generation with pluggable HTTP adapters (
Symfony HttpClient,Guzzle,PSR-18) - Optional framework and adapter version flags for version-specific code generation when upstream APIs change
- DTO hydration/serialization helpers (
fromArray()andtoArray()) - Operation filtering by tags, operation IDs, and path patterns
- Extension plugin system for
x-*vendor extensions (for examplex-trim)
Requirements
- PHP
>=8.2 - Composer
>=2.0
Installation
composer require --dev maxbeckers/php-openapi-generator
Because this package is a Composer plugin, allow it in composer.json:
{
"config": {
"allow-plugins": {
"maxbeckers/php-openapi-generator": true
}
}
}
The CLI binary is available at vendor/bin/openapi-gen.
Quick Start
1) Create php-openapi-generator.php
<?php declare(strict_types=1); use MaxBeckers\OpenApiGenerator\Config\FrameworkTarget; use MaxBeckers\OpenApiGenerator\Config\GenerationTarget; use MaxBeckers\OpenApiGenerator\Config\GeneratorConfig; use MaxBeckers\OpenApiGenerator\Config\HttpClientAdapter; $config = new GeneratorConfig(); $config->specFile = 'openapi.yaml'; $config->outputDir = 'generated'; $config->modelNamespace = 'App\\Model'; $config->modelOutputDir = 'Model'; $config->apiNamespace = 'App\\Api'; $config->apiOutputDir = 'Api'; $config->generationTarget = GenerationTarget::Server; $config->frameworkTarget = FrameworkTarget::None; // Optional version-aware generation knobs: // $config->frameworkVersion = '8.0'; // $config->httpClient = HttpClientAdapter::Guzzle; // $config->httpClientVersion = '7.8'; $config->phpReadonly = true; $config->generateFromArray = true; $config->generateToArray = true; return $config;
2) Generate code
vendor/bin/openapi-gen
3) Choose a practical quickstart path
Server quickstart (Symfony or Laravel)
$config->generationTarget = GenerationTarget::Server; $config->frameworkTarget = FrameworkTarget::Symfony; // or FrameworkTarget::Laravel
Then:
- Generate code with
vendor/bin/openapi-gen - Implement generated
*ApiInterfacemethods - Keep generated controllers/routes as thin transport glue
Client quickstart (typed API consumers)
$config->generationTarget = GenerationTarget::Client; $config->httpClient = HttpClientAdapter::SymfonyHttpClient; // or Guzzle / Psr18
Then:
- Generate code with
vendor/bin/openapi-gen - Inject
*ApiClientinto your services - Use generated DTOs for request/response mapping
4) Optional overrides from CLI
vendor/bin/openapi-gen --target=server --framework=symfony vendor/bin/openapi-gen --target=server --framework=laravel vendor/bin/openapi-gen --target=client --http-client=psr18
What Gets Generated
Models are always generated.
Server target (GenerationTarget::Server)
FrameworkTarget::None:*ApiInterfaceonly (pure contract)FrameworkTarget::Symfony:*ApiInterface+ generated*ApiControlleractions with#[Route]FrameworkTarget::Laravel:*ApiInterface+ generated*ApiController+*ApiRouteshelper
Optional: set $config->frameworkVersion when server framework major versions require different generated glue.
Client target (GenerationTarget::Client)
*ApiClientInterface*ApiClient
Adapters:
HttpClientAdapter::SymfonyHttpClientHttpClientAdapter::GuzzleHttpClientAdapter::Psr18
Optional: set $config->httpClientVersion when adapter major versions require different generated transport code.
Recommended Workflow
- Keep your OpenAPI spec in source control.
- Regenerate code when the spec changes.
- Implement generated server interfaces or inject generated clients.
- Decide as a team whether to commit generated code or regenerate in CI.
Examples
examples/petstore-client-symfonyexamples/petstore-server-symfony
Documentation
- Docs Index
- Getting Started
- Server Quickstart
- Client Quickstart
- Configuration Reference
- Framework Targets (Server)
- Client Generation
- HTTP Client Adapters
- Validation Strategies
- Extension Plugins (
x-*)
Development
composer test
composer cs
License
MIT - see LICENSE.
Issues and feedback: github.com/maxbeckers/php-openapi-generator/issues
Built with ❤️ for PHP developers
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-05