zero-to-prod/data-model-generator
Composer 安装命令:
composer require zero-to-prod/data-model-generator
包简介
Builds DataModels from a Schema
README 文档
README
Generates PHP classes and enums from OpenAPI 3.0 and Swagger 2.0 schemas.
Reads your API schema and a configuration file, then outputs fully-typed PHP classes with configurable visibility, readonly modifiers, constants, comments, and type mappings.
Contents
Installation
Install the package via composer:
composer require zero-to-prod/data-model-generator
Requires PHP 8.1 or higher.
Usage
Initialize Configuration
Generate a data-model.json configuration file in your project root:
vendor/bin/data-model-generator init
This copies a default configuration template that you can customize.
Generate from a Schema
Pass a path or URL to an OpenAPI 3.0 or Swagger 2.0 schema:
vendor/bin/data-model-generator generate /path/to/openapi.json
The CLI detects the schema type automatically and generates PHP files based on your data-model.json configuration.
Programmatic Usage
You can also call the engine directly:
use Zerotoprod\DataModelGenerator\Engine; use Zerotoprod\DataModelGenerator\Models\Components; use Zerotoprod\DataModelGenerator\Models\Config; Engine::generate( Components::from($componentsArray), Config::from($configArray) );
Configuration
The data-model.json file controls how classes and enums are generated.
{
"$schema": "./vendor/zero-to-prod/data-model-generator/src/Schema/data_model.json",
"model": {
"directory": "./app/DataModels",
"namespace": "App\\DataModels",
"imports": [
"use Zerotoprod\\DataModel;"
],
"comments": true,
"readonly": false,
"use_statements": [
"use DataModel;"
],
"properties": {
"comments": true,
"visibility": "public",
"readonly": true,
"types": {
"integer": "int",
"boolean": "bool",
"number": "float"
},
"nullable": true
},
"constants": {
"comments": true,
"type": true,
"visibility": "public"
}
}
}
Model Options
| Option | Type | Default | Description |
|---|---|---|---|
directory |
string |
null |
Output directory for generated files |
namespace |
string |
null |
PHP namespace for generated classes |
imports |
array |
[] |
use import statements added to every file |
comments |
bool |
false |
Include class-level docblock comments |
readonly |
bool |
false |
Apply the readonly modifier to classes |
use_statements |
array |
[] |
Traits added inside every class body |
Property Options
| Option | Type | Default | Description |
|---|---|---|---|
visibility |
public|protected|private |
public |
Visibility modifier for properties |
readonly |
bool |
false |
Apply readonly to all properties |
comments |
bool |
false |
Include property-level docblock comments |
types |
object |
{} |
Type mapping (e.g., "integer": "int" maps integer types to int) |
nullable |
bool |
false |
Append null to all property types and default to null |
Constant Options
| Option | Type | Default | Description |
|---|---|---|---|
visibility |
public|protected|private |
public |
Visibility modifier for constants |
type |
bool |
false |
Include type declarations on constants |
comments |
bool |
false |
Include constant-level docblock comments |
Example
Given a schema that defines a User model and a Role enum, and using the default configuration above, the generator produces:
app/DataModels/User.php
<?php namespace App\DataModels; use Zerotoprod\DataModel; /** Represents a user */ class User { use DataModel; /** The user's name */ public const string name = 'name'; /** The user's age */ public const string age = 'age'; /** The user's name */ public readonly string|null $name = null; /** The user's age */ public readonly int|null $age = null; }
app/DataModels/Role.php
<?php namespace App\DataModels; use Zerotoprod\DataModel; enum Role: string { use DataModel; case Admin = 'admin'; case User = 'user'; }
Documentation Publishing
You can publish this README to your local documentation directory.
This can be useful for providing documentation for AI agents.
This can be done using the included script:
# Publish to default location (./docs/zero-to-prod/data-model-generator) vendor/bin/zero-to-prod-data-model-generator # Publish to custom directory vendor/bin/zero-to-prod-data-model-generator /path/to/your/docs
Automatic Documentation Publishing
You can automatically publish documentation by adding the following to your composer.json:
{
"scripts": {
"post-install-cmd": [
"zero-to-prod-data-model-generator"
],
"post-update-cmd": [
"zero-to-prod-data-model-generator"
]
}
}
Testing
./vendor/bin/phpunit
zero-to-prod/data-model-generator 适用场景与选型建议
zero-to-prod/data-model-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.37k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「zero-to-prod」 「DataModelGenerator」 「data-model-generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zero-to-prod/data-model-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zero-to-prod/data-model-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zero-to-prod/data-model-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Array helpers.
A Generic Factory Pattern for Creating Arrays.
Build a Factory for Anything
A modern approach to extensible, typesafe DTOs with factory support.
Fluently set class properties with dynamic methods.
A DataModel for a Url
统计信息
- 总下载量: 1.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-06