VOOZH about

URL: https://deepwiki.com/hypervel/devtool/11-additional-generators

⇱ Additional Generators | hypervel/devtool | DeepWiki


Loading...
Menu

Additional Generators

This page documents the utility generators for creating console commands, validation rules, exception handlers, and test classes. These generators complement the HTTP layer (page 7), database (page 6), and model system (page 5) generators by providing tools for building supporting application infrastructure.

Overview

The devtool package provides four additional generator commands for common development tasks:

GeneratorCommandPurposeDefault Namespace
Consolemake:commandCreate console commandsApp\Console\Commands
Rulemake:ruleCreate validation rulesApp\Rules
Exceptionmake:exceptionCreate exception classesApp\Exceptions
Testmake:testCreate test classesTests\Feature or Tests\Unit

Each generator extends GeneratorCommand from Hyperf and follows the standard stub-based generation pattern documented in page 4.1.

Command Class Architecture


Sources: src/Generator/ConsoleCommand.php1-52 src/Generator/RuleCommand.php1-32 src/Generator/ExceptionCommand.php1-50 src/Generator/TestCommand.php1-72

Console Command Generation

The ConsoleCommand class generates custom console commands that extend Hypervel\Console\Command. These commands integrate with the framework's console application and can be invoked via the command-line interface.

Command Structure


Generated Code Structure


The command signature is determined by the --command option or auto-generated from the class name. The generation logic is implemented in src/Generator/ConsoleCommand.php28-34:

  • If --command is provided, use that value directly
  • Otherwise, convert class name to kebab-case and prefix with app: (e.g., SendEmails becomes app:send-emails)

Generated File Structure

The stub template src/Generator/stubs/console.stub1-28 defines the generated command structure:

ComponentDescriptionLocation
$signatureCommand name used for invocationLine 14
$descriptionHelp text shown in command listLine 19
handle()Execution entry pointLines 24-27

The generated class extends Hypervel\Console\Command and includes stub properties for configuration. Developers implement business logic in the handle() method.

Configuration Options

OptionTypeDescriptionDefault
--commandOptionalTerminal command signatureapp:{kebab-case-name}
--pathOptionalOutput directory pathPer namespace
--namespaceOptionalClass namespace overrideApp\Console\Commands

The namespace and stub path can be customized via configuration src/Generator/ConsoleCommand.php41-43

Sources: src/Generator/ConsoleCommand.php1-52 src/Generator/stubs/console.stub1-28

Validation Rule Generation

The RuleCommand class generates custom validation rules that implement the ValidationRule interface. These rules integrate with the framework's validation system for custom field validation logic.

Command Usage


Validation Rule Data Flow


Generated Interface Implementation

The stub template src/Generator/stubs/rule.stub1-22 generates a class implementing Hypervel\Validation\Contracts\ValidationRule:

validate(string $attribute, mixed $value, Closure $fail): void
ParameterTypePurpose
$attributestringField name being validated
$valuemixedField value to validate
$failClosureCallback to invoke on validation failure

The $fail closure accepts two parameters:

  • Message string
  • Optional translation key (nullable string)

The return type is PotentiallyTranslatedString for internationalization support.

Default Configuration

The RuleCommand class src/Generator/RuleCommand.php1-32 provides minimal configuration:

SettingValue
Command namemake:rule
Stub templatestubs/rule.stub
Default namespaceApp\Rules
Parent optionsAll standard GeneratorCommand options

Sources: src/Generator/RuleCommand.php1-32 src/Generator/stubs/rule.stub1-22

Exception Generation

The ExceptionCommand class generates custom exception classes with optional render() and report() methods for HTTP response rendering and error reporting.

Stub Selection Matrix

The command selects from four stub templates based on option flags:

--render--reportStub Used
NoNoexception.stub
YesNoexception-render.stub
NoYesexception-report.stub
YesYesexception-render-report.stub

This logic is implemented in src/Generator/ExceptionCommand.php24-35

Exception Generation Decision Tree


Method Signatures

Basic Exception src/Generator/stubs/exception.stub1-13

  • Extends PHP's Exception class
  • No additional methods

With report() src/Generator/stubs/exception-report.stub13-17

public function report(): void

Used for logging or external error tracking service integration.

With render() src/Generator/stubs/exception-render.stub14-19

public function render(Request $request): Response

Returns a custom HTTP response when the exception is thrown. Both parameters use Hypervel types:

  • Hypervel\Http\Request
  • Hypervel\Http\Response

With both methods src/Generator/stubs/exception-render-report.stub12-28 Combines both report() and render() methods in a single class.

Command Options



















OptionDescription
--renderAdd empty render(Request): Response method
--reportAdd empty report(): void method

Sources: src/Generator/ExceptionCommand.php1-50 src/Generator/stubs/exception.stub1-13 src/Generator/stubs/exception-render.stub1-21 src/Generator/stubs/exception-report.stub1-19 src/Generator/stubs/exception-render-report.stub1-29

Test Generation

The TestCommand class generates PHPUnit test classes with placement in either feature or unit test directories based on the --unit flag.

Test Type Selection


Test Generation Flow


Stub and Namespace Selection

The command selects stub and namespace based on the --unit option src/Generator/TestCommand.php24-40:

Test TypeStubNamespaceOutput Path
Feature (default)test.stubTests\Featuretests/Feature/
Unittest.unit.stubTests\Unittests/Unit/

Path Calculation

The getPath() method src/Generator/TestCommand.php57-71 implements custom path resolution:

  1. Determine namespace from --namespace option or default
  2. Strip namespace prefix from fully qualified class name
  3. Convert backslashes to forward slashes
  4. Determine base path from --path option or default (tests/Feature or tests/Unit)
  5. Concatenate base path with filename

This differs from standard generator behavior because test files have special directory conventions.

Configuration Options

OptionTypeDescriptionDefault
--unit (-u)FlagCreate unit test instead of feature testFeature test
--path (-p)OptionalCustom output directorytests/Feature or tests/Unit
--namespaceOptionalCustom namespaceTests\Feature or Tests\Unit

The --path option is redefined src/Generator/TestCommand.php42-51 to be optional rather than inheriting the parent definition, allowing the command to use sensible defaults based on test type.

Sources: src/Generator/TestCommand.php1-72

Common Generation Patterns

All four generators share common implementation patterns inherited from GeneratorCommand:

Shared Configuration Methods


Each command overrides:

  • getStub(): Returns stub file path with configuration override support
  • getDefaultNamespace(): Returns default namespace with configuration override support
  • getOptions() (optional): Extends parent options with command-specific flags

The configuration override pattern allows users to customize stub templates and namespaces without modifying package code, as seen in src/Generator/ConsoleCommand.php36-43 src/Generator/RuleCommand.php23-30 src/Generator/ExceptionCommand.php24-40 and src/Generator/TestCommand.php24-40

Sources: src/Generator/ConsoleCommand.php36-51 src/Generator/RuleCommand.php23-31 src/Generator/ExceptionCommand.php24-49 src/Generator/TestCommand.php24-71