grazulex/laravel-modelschema
Composer 安装命令:
composer require grazulex/laravel-modelschema
包简介
Generate and manage Laravel model schemas with automatic validation, documentation, and migration helpers for better data structure management.
关键字:
README 文档
README
A foundational Laravel package for schema-driven development. Parse YAML schemas, generate insertable fragments for models, migrations, requests, resources, factories, seeders, controllers, tests, policies, observers, services, actions, and validation rules. Built to power Laravel TurboMaker, Arc, and other schema-based packages.
[
- 📈 Migration Guide - Upgrading from previous versions
- 📊 Fragment Examples - Understanding generated fragmentsocs/FIELD_TYPES.md)** - Complete field types reference
- 🔌 Field Type Plugins - Creating custom field type plugins
- ✨ Custom Attributes Examples - Practical examples of custom attributes usage
- ✅ Custom Field Validation - Validating custom field types.io/packagist/v/grazulex/laravel-modelschema.svg?style=flat-square)](https://packagist.org/packages/grazulex/laravel-modelschema)
Overview
Laravel ModelSchema provides schema parsing, validation, and fragment generation for Laravel applications. Instead of generating complete files, it produces insertable JSON/YAML fragments that parent applications can integrate into their own generation workflows.
🎯 Core Purpose: Enable schema-driven development with clean separation between core schema logic and application-specific generation.
🚀 Key Features
- 🔍 Schema Parsing & Validation - Parse YAML schemas with core/extension separation
- 🧩 Fragment Generation - Generate insertable JSON/YAML fragments for Laravel artifacts
- 🏗️ Clean Architecture - Separate core schema responsibilities from app-specific generation
- 🔄 Multi-Generator Support - Models, Migrations, Requests, Resources, Factories, Seeders, Controllers, Tests, Policies, Observers, Services, Actions, Rules
- 📈 Schema Analysis - Advanced schema comparison, optimization, and performance analysis
- 🔌 Plugin System - Extensible field type plugins for custom functionality
- 📊 Integration API - Complete workflow for external packages (TurboMaker, Arc, etc.)
- ✨ Extensible Design - Custom field types, generators, and validation rules
� Installation
composer require grazulex/laravel-modelschema
🏗️ Architecture
Core Services
SchemaService- Main API for parsing, validation, and core/extension separationGenerationService- Coordinates all generators to produce insertable fragmentsYamlOptimizationService- Advanced YAML parsing with lazy loading, streaming, and intelligent cachingSchemaDiffService- Advanced schema comparison and difference analysisSchemaOptimizationService- Performance analysis and optimization recommendations- 13 Specialized Generators - Model, Migration, Request, Resource, Factory, Seeder, Controller, Test, Policy, Observer, Service, Action, Rule
FieldTypePluginManager- Manages extensible field type plugins for custom functionality
Schema Structure
The package uses a "core" structure to clearly separate core schema data from application extensions:
core: model: User table: users fields: name: type: string nullable: false email: type: string unique: true relations: posts: type: hasMany model: App\Models\Post options: timestamps: true soft_deletes: false # Extensions added by parent applications turbomaker: views: ['index', 'create', 'edit'] routes: ['api', 'web'] arc: permissions: ['view', 'create', 'edit', 'delete']
🚀 Quick Start
1. Basic Schema Parsing
use Grazulex\LaravelModelschema\Services\SchemaService; $schemaService = new SchemaService(); // Parse and separate core from extensions $result = $schemaService->parseAndSeparateSchema($yamlContent); // Returns: ['core' => [...], 'extensions' => [...]] // Validate only the core schema $errors = $schemaService->validateCoreSchema($yamlContent); // Extract structured data for generation $data = $schemaService->extractCoreContentForGeneration($yamlContent);
2. Complete Integration Workflow
// 1. Generate complete YAML from stub + app data $completeYaml = $schemaService->generateCompleteYamlFromStub( 'user.schema.stub', ['MODEL_NAME' => 'User', 'TABLE_NAME' => 'users'], $appExtensionData ); // 2. Validate the complete YAML (focuses on core section) $errors = $schemaService->validateFromCompleteAppYaml($completeYaml); // 3. Extract all generation data as insertable fragments $generationData = $schemaService->getGenerationDataFromCompleteYaml($completeYaml); // 4. Use fragments in your application $modelFragment = json_decode($generationData['generation_data']['model']['json'], true); $migrationFragment = json_decode($generationData['generation_data']['migration']['json'], true);
3. Fragment-Based Generation
use Grazulex\LaravelModelschema\Services\GenerationService; $generationService = new GenerationService(); // Generate all fragments for a schema $fragments = $generationService->generateAll($schema); // Result structure: // [ // 'model' => ['json' => '{"model": {...}}', 'yaml' => 'model: {...}'], // 'migration' => ['json' => '{"migration": {...}}', 'yaml' => 'migration: {...}'], // 'requests' => ['json' => '{"requests": {...}}', 'yaml' => 'requests: {...}'], // 'resources' => ['json' => '{"resources": {...}}', 'yaml' => 'resources: {...}'], // 'factory' => ['json' => '{"factory": {...}}', 'yaml' => 'factory: {...}'], // 'seeder' => ['json' => '{"seeder": {...}}', 'yaml' => 'seeder: {...}'], // 'controllers' => ['json' => '{"controllers": {...}}', 'yaml' => 'controllers: {...}'], // 'tests' => ['json' => '{"tests": {...}}', 'yaml' => 'tests: {...}'], // 'policies' => ['json' => '{"policies": {...}}', 'yaml' => 'policies: {...}'], // 'observers' => ['json' => '{"observers": {...}}', 'yaml' => 'observers: {...}'], // 'services' => ['json' => '{"services": {...}}', 'yaml' => 'services: {...}'], // 'actions' => ['json' => '{"actions": {...}}', 'yaml' => 'actions: {...}'], // 'rules' => ['json' => '{"rules": {...}}', 'yaml' => 'rules: {...}'] // ]
🏗️ Available Generators
Laravel ModelSchema provides 13 specialized generators, each producing insertable JSON/YAML fragments:
Core Laravel Components
| Generator | Description | Output Fragment |
|---|---|---|
| Model | Eloquent model with relationships, casts, and configurations | model: {class_name, table, fields, relations, casts, ...} |
| Migration | Database migration with fields, indexes, and foreign keys | migration: {table, fields, indexes, foreign_keys, ...} |
| Request | Form Request classes for validation (Store/Update) | requests: {store: {...}, update: {...}} |
| Resource | API Resource classes for data transformation | resources: {main_resource: {...}, collection_resource: {...}} |
| Factory | Model Factory for testing and seeding | factory: {class_name, definition, states, ...} |
| Seeder | Database Seeder for data population | seeder: {class_name, model, count, relationships, ...} |
Advanced Components
| Generator | Description | Output Fragment |
|---|---|---|
| Controller | API and Web Controllers with CRUD operations | controllers: {api_controller: {...}, web_controller: {...}} |
| Test | PHPUnit test classes (Feature and Unit) | tests: {feature_tests: [...], unit_tests: [...]} |
| Policy | Authorization Policy classes | policies: {class_name, methods, gates, ...} |
Business Logic Components (New in v2.0)
| Generator | Description | Output Fragment |
|---|---|---|
| Observer | Eloquent Observer with model event handlers | observers: {class_name, events, methods, ...} |
| Service | Business logic Service classes with CRUD operations | services: {class_name, methods, dependencies, ...} |
| Action | Single-responsibility Action classes | actions: {crud_actions: [...], business_actions: [...]} |
| Rule | Custom Validation Rule classes | rules: {business_rules: [...], foreign_key_rules: [...]} |
Usage Examples
// Generate specific components $observerFragment = $generationService->generateObservers($schema); $serviceFragment = $generationService->generateServices($schema); $actionFragment = $generationService->generateActions($schema); $ruleFragment = $generationService->generateRules($schema); // Generate multiple new components $fragments = $generationService->generateMultiple($schema, [ 'observers', 'services', 'actions', 'rules' ]); // Generate everything including new components $allFragments = $generationService->generateAll($schema, [ 'model' => true, 'migration' => true, 'requests' => true, 'resources' => true, 'factory' => true, 'seeder' => true, 'controllers' => true, 'tests' => true, 'policies' => true, 'observers' => true, // New 'services' => true, // New 'actions' => true, // New 'rules' => true, // New ]);
🔧 API Reference
SchemaService
| Method | Description | Returns |
|---|---|---|
parseAndSeparateSchema() |
Parse YAML and separate core/extensions | ['core' => array, 'extensions' => array] |
validateCoreSchema() |
Validate only core schema section | array (errors) |
extractCoreContentForGeneration() |
Extract structured core data | array |
generateCompleteYamlFromStub() |
Generate complete YAML from stub | string |
getGenerationDataFromCompleteYaml() |
Extract all generation fragments | array |
GenerationService
| Method | Description | Returns |
|---|---|---|
generateAll() |
Generate all fragments for schema | array |
generateSingle() |
Generate single generator fragment | array |
getAvailableGenerators() |
List available generators | array |
🔌 Trait-Based Field Type Plugin System
Laravel ModelSchema features an extensible plugin system using a trait-based architecture for custom field types. This modern approach provides powerful customization through traits and configuration objects.
Plugin Manager
use Grazulex\LaravelModelschema\Support\FieldTypePluginManager; $manager = new FieldTypePluginManager(); // Register a custom plugin $manager->registerPlugin(new CustomFieldTypePlugin()); // Auto-discover plugins in specific paths $manager->discoverPlugins([ 'App\\FieldTypes\\*Plugin', 'Custom\\Packages\\*FieldTypePlugin' ]); // Get all registered plugins $plugins = $manager->getAllPlugins();
Creating Custom Plugins with Traits
The new trait-based approach allows you to define field options through configuration arrays rather than hardcoded properties:
use Grazulex\LaravelModelschema\Support\FieldTypePlugin; class UrlFieldTypePlugin extends FieldTypePlugin { protected string $version = '1.0.0'; protected string $author = 'Your Name'; protected string $description = 'Advanced URL field with validation traits'; public function __construct() { // Define custom attributes using trait-based configuration $this->customAttributes = [ 'schemes', 'verify_ssl', 'timeout', 'domain_whitelist', 'max_redirects' ]; // Configure each attribute with validation traits $this->customAttributeConfig = [ 'schemes' => [ 'type' => 'array', 'default' => ['http', 'https'], 'enum' => ['http', 'https', 'ftp', 'ftps'], 'description' => 'Allowed URL schemes for validation' ], 'verify_ssl' => [ 'type' => 'boolean', 'default' => true, 'description' => 'Enable SSL certificate verification' ], 'timeout' => [ 'type' => 'integer', 'min' => 1, 'max' => 300, 'default' => 30, 'description' => 'Connection timeout in seconds' ], 'domain_whitelist' => [ 'type' => 'array', 'required' => false, 'validator' => function ($value): array { // Custom validation trait for domain lists if (!is_array($value)) return ['must be an array']; foreach ($value as $domain) { if (!filter_var("http://{$domain}", FILTER_VALIDATE_URL)) { return ["Invalid domain: {$domain}"]; } } return []; } ] ]; } public function getType(): string { return 'url'; } public function getAliases(): array { return ['website', 'link', 'uri']; } }
Trait-Based Custom Attributes System
The trait-based plugin system supports sophisticated custom attributes through configuration objects:
- Type validation traits:
string,int,boolean,array, etc. - Constraint traits:
min,max,required,enumvalues - Default value traits: Automatically applied if not provided
- Custom validator traits: Callback functions for complex validation logic
- Transformation traits: Custom value transformation before storage
- Integration traits: Seamlessly merged with Laravel's standard attributes
Advanced Trait Examples
// Numeric validation traits 'timeout' => [ 'type' => 'integer', 'min' => 1, 'max' => 300, 'default' => 30, 'transform' => fn($value) => (int) $value // Type transformation trait ], // Array validation traits with enum constraints 'schemes' => [ 'type' => 'array', 'enum' => ['http', 'https', 'ftp', 'ftps'], 'default' => ['http', 'https'], 'validator' => function($schemes): array { // Custom validation trait return array_filter($schemes, fn($s) => in_array($s, ['http', 'https'])); } ], // Complex custom validator traits 'domain_pattern' => [ 'type' => 'string', 'validator' => function($pattern): array { if (!preg_match('/^\/.*\/[gimxs]*$/', $pattern)) { return ['Domain pattern must be a valid regex']; } return []; } ]
📖 See Field Type Plugins Documentation for complete trait-based implementation guide.
📁 Example Schema Files
Basic User Schema
core: model: User table: users fields: name: type: string nullable: false rules: ['required', 'string', 'max:255'] email: type: string unique: true rules: ['required', 'email', 'unique:users'] email_verified_at: type: timestamp nullable: true password: type: string rules: ['required', 'string', 'min:8'] options: timestamps: true soft_deletes: false
Blog Post Schema with Relations
core: model: Post table: posts fields: title: type: string rules: ['required', 'string', 'max:255'] slug: type: string unique: true rules: ['required', 'string', 'unique:posts'] content: type: text rules: ['required'] published_at: type: timestamp nullable: true user_id: type: foreignId rules: ['required', 'exists:users,id'] relations: user: type: belongsTo model: App\Models\User comments: type: hasMany model: App\Models\Comment tags: type: belongsToMany model: App\Models\Tag pivot_table: post_tags options: timestamps: true soft_deletes: true
� Integration with Parent Applications
This package is designed to be consumed by larger Laravel packages like TurboMaker and Arc. Here's the typical integration pattern:
Parent Application Workflow
// 1. Parent app generates complete YAML $yaml = $schemaService->generateCompleteYamlFromStub('user.schema.stub', [ 'MODEL_NAME' => 'User', 'TABLE_NAME' => 'users' ], $parentAppData); // 2. Parent app validates the schema $errors = $schemaService->validateFromCompleteAppYaml($yaml); if (!empty($errors)) { throw new ValidationException($errors); } // 3. Parent app extracts generation fragments $data = $schemaService->getGenerationDataFromCompleteYaml($yaml); // 4. Parent app integrates fragments into its own files $parentAppGenerator->generateModelFile($data['generation_data']['model']['json']); $parentAppGenerator->generateMigrationFile($data['generation_data']['migration']['json']); // ... etc for requests, resources, factory, seeder
Fragment Structure
Each generator produces insertable fragments with this structure:
{
"model": {
"class_name": "User",
"table": "users",
"fields": [...],
"relations": [...],
"casts": {...},
"options": {...}
}
}
The parent application receives these fragments and inserts them into its own generation templates.
🧪 Testing
# Run all tests composer test # Run with coverage composer test-coverage # Run specific test file ./vendor/bin/pest tests/Unit/SchemaServiceTest.php
🔧 Requirements
- PHP: ^8.3
- Laravel: ^12.19 (optional, used in service provider)
- Symfony YAML: ^7.3 (for YAML parsing)
📚 Documentation
Core Documentation
- 🏗️ Architecture Guide - Understanding the package structure and design
- � Migration Guide - Upgrading from previous versions
- 📊 Fragment Examples - Understanding generated fragments
Field Types & Extensions
- � Field Types Guide - Complete field types reference
- 🔌 Field Type Plugins - Creating custom field type plugins
- ✅ Custom Field Validation - Validating custom field types
Advanced Features
- 📝 Logging System - Comprehensive logging and debugging
- ⚡ Enhanced Features - Advanced capabilities overview
- � YAML Optimization - High-performance YAML parsing with intelligent caching and streaming
- �🔍 Schema Optimization - Schema analysis and optimization tools
- 🔒 Security Features - Comprehensive security validation and protection
Integration Examples
- 🔗 Integration Example - Complete integration workflow
- ✨ New Generators Example - Observer, Service, Action, Rule generators demo
- 🛠️ Schema Service API - API usage examples
- 📋 API Extensions - Extended API implementations
- 🚀 YAML Optimization Examples - Performance optimization usage and examples
- ⚡ Schema Optimization Usage - Advanced schema analysis examples
- 🔒 Security Usage Examples - Security validation and protection examples
🤝 Contributing
We welcome contributions! Please see our Contributing Guide for details.
🔒 Security
Please review our Security Policy for reporting vulnerabilities.
📄 License
Laravel ModelSchema is open-sourced software licensed under the MIT license.
Made with ❤️ by Jean-Marc Strauven (https://github.com/Grazulex)
grazulex/laravel-modelschema 适用场景与选型建议
grazulex/laravel-modelschema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 286 次下载、GitHub Stars 达 8, 最近一次更新时间为 2025 年 08 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「documentation」 「cli」 「schema」 「validation」 「migration」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 grazulex/laravel-modelschema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 grazulex/laravel-modelschema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 grazulex/laravel-modelschema 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Laravel package that generates RESTful API documentation in Markdown based on PHPDoc.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 286
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 23
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-03
