VOOZH about

URL: https://deepwiki.com/hypervel/devtool/11.1-console-command-generation

⇱ Console Command Generation | hypervel/devtool | DeepWiki


Loading...
Menu

Console Command Generation

Purpose and Scope

This document details the make:command generator, which creates custom console commands for the Hypervel framework. Console commands are CLI tools that extend the application's command-line interface, allowing developers to build custom administrative tasks, maintenance scripts, and automation tools.

For information about other utility generators, see:

  • Validation rule generation: 11.2
  • Exception generation: 11.3
  • Test generation: 11.4

Command Overview

The make:command generator creates console command classes that extend Hypervel\Console\Command. These commands integrate with the framework's console kernel and can be invoked via the command-line interface.

Command Signature: make:command <name>

Primary Class: Hypervel\Devtool\Generator\ConsoleCommand

Output Location: App\Console\Commands\ (configurable)

Sources: src/Generator/ConsoleCommand.php1-52


Architecture and Class Structure

ConsoleCommand Class Hierarchy


The ConsoleCommand class follows the standard generator pattern, extending Hyperf's GeneratorCommand base class. It overrides key methods to customize behavior for console command generation.

Key Method Overrides:

MethodPurposeLine Reference
configure()Sets command descriptionsrc/Generator/ConsoleCommand.php18-23
getStub()Returns path to console.stub templatesrc/Generator/ConsoleCommand.php36-39
getDefaultNamespace()Returns default namespace for commandssrc/Generator/ConsoleCommand.php41-44
replaceClass()Processes stub placeholders, including signaturesrc/Generator/ConsoleCommand.php28-34
getOptions()Adds --command optionsrc/Generator/ConsoleCommand.php46-51

Sources: src/Generator/ConsoleCommand.php11-52


Generation Flow

Command Execution Pipeline


The generation process involves:

  1. Namespace resolution via getDefaultNamespace()
  2. Stub selection via getStub()
  3. Placeholder replacement via replaceClass() and inherited methods
  4. File writing to the determined namespace path

Sources: src/Generator/ConsoleCommand.php28-44


Usage and Options

Basic Usage


Available Options

OptionTypeDescriptionLine Reference
--commandOptionalExplicitly set the terminal command signaturesrc/Generator/ConsoleCommand.php49
--forceFlagOverwrite existing file (inherited)From GeneratorCommand

Signature Auto-Generation

When the --command option is omitted, the signature is automatically generated using the following algorithm:


Implementation: The signature generation occurs in replaceClass() using Hyperf's Str helper:


Examples:

  • SendEmailsapp:send-emails
  • GenerateReportapp:generate-report
  • ProcessQueueapp:process-queue

Sources: src/Generator/ConsoleCommand.php31


Generated Command Structure

Stub Template Anatomy


Generated File Structure

The output file contains three key components:

1. Command Signature (src/Generator/stubs/console.stub14)


The $signature property defines how the command is invoked from the terminal. It can include:

  • Command name
  • Arguments
  • Options
  • Input descriptions

2. Command Description (src/Generator/stubs/console.stub19)


The $description appears in the command list (php bin/hypervel.php list).

3. Handle Method (src/Generator/stubs/console.stub24-27)


The handle() method contains the command's execution logic. It is invoked when the command runs.

Complete Example Output

For make:command SendEmails --command=email:send, the generated file at App/Console/Commands/SendEmails.php:


Sources: src/Generator/stubs/console.stub1-28


Signature Configuration Patterns

Advanced Signature Syntax

While the generator creates a basic signature, the $signature property supports advanced syntax after generation:

Arguments:


Optional Arguments:


Arguments with Defaults:


Options:


Options with Values:


Options with Defaults:


Signature Resolution Logic


Sources: src/Generator/ConsoleCommand.php28-34


Configuration and Customization

Configuration Options

The ConsoleCommand class supports configuration through the getConfig() method inherited from GeneratorCommand:

Available Configuration Keys:

KeyPurposeDefaultLine Reference
stubCustom stub file path__DIR__ . '/stubs/console.stub'src/Generator/ConsoleCommand.php38
namespaceCustom namespace for commandsApp\Console\Commandssrc/Generator/ConsoleCommand.php43

Custom Namespace Example

To generate commands in a different namespace, configure the generator in your application's configuration:


Custom Stub Example

To use a custom stub template:


Configuration Resolution Flow


Sources: src/Generator/ConsoleCommand.php36-44


Stub Placeholder System

Available Placeholders

The console command stub uses three placeholders that are replaced during generation:

PlaceholderReplacement SourceProcessing MethodLine Reference
%NAMESPACE%getDefaultNamespace()Inherited from GeneratorCommandsrc/Generator/stubs/console.stub5
%CLASS%Class basename from inputreplaceClass() (parent)src/Generator/stubs/console.stub9
%COMMAND%--command option or auto-generatedreplaceClass() (override)src/Generator/stubs/console.stub14

Placeholder Processing Pipeline


Sources: src/Generator/ConsoleCommand.php28-34 src/Generator/stubs/console.stub5-14


Integration with Command Registration

After generation, console commands must be registered in the application's console kernel to be executable. This typically involves:

  1. Auto-discovery: Commands in App\Console\Commands may be auto-discovered by the framework
  2. Manual registration: Commands can be registered in the console kernel configuration

The generated command extends Hypervel\Console\Command, which provides:

  • Input/output handling
  • Option and argument parsing
  • Integration with the console application lifecycle

Sources: src/Generator/stubs/console.stub7-9


Related Generators

The console command generator is part of the "Additional Generators" subsystem. Related generators include:

GeneratorPurposePage Reference
make:ruleValidation rule classes11.2
make:exceptionException handler classes11.3
make:testPHPUnit test classes11.4

Sources: src/Generator/ConsoleCommand.php1-52