VOOZH about

URL: https://deepwiki.com/hypervel/devtool/4-code-generation-system

⇱ Code Generation System | hypervel/devtool | DeepWiki


Loading...
Menu

Code Generation System

Purpose and Scope

The Code Generation System is the core mechanism that transforms stub templates into concrete PHP class files. This document explains the overall generation pipeline, from command invocation through stub processing to file output. For details about the stub template syntax and placeholder patterns, see Stub Template System. For information about the base generator command architecture, see Generator Command Architecture.

Generation Pipeline Overview

The code generation system follows a consistent five-stage pipeline used by all 28 generator commands:

Generation Pipeline Architecture


Sources: src/Generator/ModelCommand.php31-66 src/Generator/MiddlewareCommand.php24-31 src/Generator/FactoryCommand.php25-28

Core Components

Generator Command Layer

All generator commands extend Hyperf\Devtool\Generator\GeneratorCommand, which provides the base infrastructure for the generation pipeline. Each command in the hypervel/devtool package customizes three key methods:

MethodPurposeExample
getStub()Returns path to stub templatesrc/Generator/FactoryCommand.php25-28
getDefaultNamespace()Returns target namespacesrc/Generator/FactoryCommand.php78-81
replaceClass()Performs custom placeholder replacementsrc/Generator/FactoryCommand.php40-58

Stub Template Repository

Stub templates are stored in src/Generator/stubs/ and contain placeholder markers that are replaced during generation. Each stub defines the structural skeleton for a specific component type:

Stub TypeTemplate FileTarget Output
Console Commandconsole.stubapp/Console/Commands/*.php
Middlewaremiddleware.stubapp/Http/Middleware/*.php
Middleware (PSR-15)middleware.psr15.stubapp/Http/Middleware/*.php
Validation Rulerule.stubapp/Rules/*.php
Model Factoryfactory.stubdatabase/factories/*.php

Sources: src/Generator/stubs/console.stub1-28 src/Generator/stubs/middleware.stub1-21 src/Generator/stubs/middleware.psr15.stub1-21

Placeholder Replacement System

The placeholder system uses string replacement to inject dynamic values into stub templates. Standard placeholders include:

  • %NAMESPACE% - Target namespace for the generated class
  • %CLASS% - Class name without namespace
  • %COMMAND% - Command signature for console commands
  • %MODEL% - Model class name for factories/policies
  • %MODEL_NAMESPACE% - Full namespace of associated model
  • %USES% - Base class import statement

Sources: src/Generator/FactoryCommand.php40-58 src/Generator/ConsoleCommand.php28-34 src/Generator/ModelCommand.php71-78

File Output System

Generated files are written to filesystem paths determined by combining:

  1. BASE_PATH - Application root directory
  2. Configuration path - From getConfig()['path'] or default
  3. Generated class name - Qualified class name with .php extension

The getPath() method constructs the full output path:

BASE_PATH / {configured_path} / {ClassName}.php

Sources: src/Generator/FactoryCommand.php71-76

Generation Process Flow

Detailed Generation Sequence


Sources: src/Generator/ModelCommand.php31-66 src/Generator/FactoryCommand.php40-58

Conditional Stub Selection

Commands can select different stub templates based on input options, enabling variant generation from a single command. This pattern is implemented by inspecting options in the getStub() method:

Conditional Stub Selection Pattern


The MiddlewareCommand demonstrates this pattern:


This generates either a standard middleware with handle() method or a PSR-15 compliant middleware with process() method, based on the --psr15 flag.

Sources: src/Generator/MiddlewareCommand.php24-31 src/Generator/stubs/middleware.stub1-21 src/Generator/stubs/middleware.psr15.stub1-21

Custom Placeholder Replacement

Commands override replaceClass() to inject custom placeholders specific to their generated component type. The method receives the stub content and class name, performs replacements, and returns the modified content.

Custom Replacement Examples

CommandCustom PlaceholderReplacement LogicSource
ConsoleCommand%COMMAND%Generates kebab-case command from class name or uses --command optionsrc/Generator/ConsoleCommand.php28-34
FactoryCommand%MODEL%, %MODEL_NAMESPACE%Infers model from factory name or uses --model optionsrc/Generator/FactoryCommand.php40-58
ModelCommand%USES%Injects base model class from configurationsrc/Generator/ModelCommand.php71-78

The FactoryCommand replacement logic demonstrates multi-placeholder injection:


Sources: src/Generator/ConsoleCommand.php28-34 src/Generator/FactoryCommand.php40-58 src/Generator/ModelCommand.php71-78

Orchestration Pattern

The ModelCommand implements an orchestration pattern that can invoke multiple generator commands in sequence, creating a full component stack from a single command invocation. This pattern is activated by the --all flag or individual flags like --factory, --migration, --controller, etc.

ModelCommand Orchestration Flow


The implementation uses Symfony Console's ArrayInput to programmatically invoke other commands:


When --all is specified, all flags are activated:


This results in up to 7 generated files:

  1. Model class (app/Models/User.php)
  2. Factory class (database/factories/UserFactory.php)
  3. Seeder class (database/seeders/UserSeeder.php)
  4. Migration file (database/migrations/YYYY_MM_DD_HHMMSS_create_users_table.php)
  5. Controller class (app/Http/Controllers/UserController.php)
  6. Policy class (app/Policies/UserPolicy.php)
  7. Form request classes (if --requests is also specified)

Sources: src/Generator/ModelCommand.php31-66 src/Generator/ModelCommand.php110-190 src/Generator/ModelCommand.php192-198

Configuration Override System

All generator commands support configuration overrides through the getConfig() method, which allows customization of:

  • Namespace: Target namespace for generated classes
  • Path: Filesystem path for generated files
  • Stub: Custom stub template location
  • Model namespace: Default namespace for model references

This configuration system enables project-specific customization while maintaining the default conventions. Commands check configuration before falling back to defaults:


Sources: src/Generator/RuleCommand.php23-31 src/Generator/ConsoleCommand.php36-44 src/Generator/FactoryCommand.php30-35