VOOZH about

URL: https://deepwiki.com/hypervel/console/6-command-replacement-and-aliasing

⇱ Command Replacement and Aliasing | hypervel/console | DeepWiki


Loading...
Menu

Command Replacement and Aliasing

Purpose and Scope

This document describes the command replacement and aliasing system in the Hypervel Console package. The CommandReplacer class provides a mechanism to rename, alias, and remove console commands during application bootstrap. This system enables standardization of command naming conventions (such as converting gen:* commands to make:*) while maintaining backward compatibility through aliases, and allows removal of unwanted commands from the application's command registry.

For information about the base Command class and command execution lifecycle, see Command System. For details about the Application class that integrates with the replacer, see Application System.


System Overview

The command replacement system operates as a transparent transformation layer between command registration and execution. When the application discovers commands, the CommandReplacer evaluates each command against a static replacement map and applies transformations as needed.

Command Replacement Flow


Sources: src/CommandReplacer.php50-71


The CommandReplacer Class

The CommandReplacer class is a static utility that maintains a centralized mapping of command transformations.

Class Structure

ComponentTypePurpose
$commandsstatic arrayMaps original command names to replacements
replace()static methodApplies transformation to a Command instance

Replacement Map Structure

The $commands array supports three replacement patterns:


Sources: src/CommandReplacer.php11-48


Replacement Patterns

Simple String Replacements

The majority of replacements are simple string mappings that rename a command while preserving its original functionality and description. The original command name is retained as an alias for backward compatibility.

Original CommandReplacementPattern
gen:amqp-consumermake:amqp-consumergen:*make:*
gen:amqp-producermake:amqp-producergen:*make:*
gen:aspectmake:aspectgen:*make:*
gen:classmake:classgen:*make:*
gen:commandmake:commandgen:*make:*
gen:kafka-consumermake:kafka-consumergen:*make:*
gen:listenermake:listenergen:*make:*
gen:middlewaremake:middlewaregen:*make:*
gen:migrationmake:migrationgen:*make:*
gen:nats-consumermake:nats-consumergen:*make:*
gen:nsq-consumermake:nsq-consumergen:*make:*
gen:processmake:processgen:*make:*
gen:swaggermake:swaggergen:*make:*
gen:migration-from-databasemake:migration-from-databasegen:*make:*
gen:swagger-schemamake:swagger-schemagen:*make:*
gen:view-engine-cacheview:cacheSpecial case
describe:aspectsaspect:listdescribe:**:list

Sources: src/CommandReplacer.php18-46

Array-Based Replacements

Array-based replacements allow modification of both the command name and description. This pattern is used when renaming a command requires clarifying its purpose.






















Original CommandNew NameNew Description
startserveStart Hypervel servers
describe:routesroute:listList all registered routes

Sources: src/CommandReplacer.php12-45


Command Removal

Commands mapped to null are removed from the application's command registry. This mechanism allows the package to disable commands that are not applicable to the Hypervel environment or have been deprecated.

Removed Commands

CommandReason for Removal
infoNot applicable to Hypervel
server:watchNot applicable to Hypervel
gen:jobReplaced by different queue system
gen:modelCustom model generation approach
gen:seederCustom seeding approach
gen:observerCustom observer pattern
gen:requestCustom request handling
gen:resourceCustom resource approach
describe:listenersNot implemented

Sources: src/CommandReplacer.php16-47


The replace() Method

The replace() method implements the transformation logic, taking a Command instance and optionally maintaining the original name as an alias.

Method Signature

public static function replace(Command $command, bool $remainAlias = true): ?Command

Parameters

ParameterTypeDefaultDescription
$commandCommandrequiredThe command instance to potentially transform
$remainAliasbooltrueWhether to keep the original name as an alias

Return Value

Return TypeConditionMeaning
CommandNo match in mapOriginal command unchanged
CommandString/array replacementModified command with new name
nullNull replacementCommand should be removed

Transformation Logic


Sources: src/CommandReplacer.php50-71


Integration with Application

The CommandReplacer is invoked by the Application class during command registration. According to the high-level architecture diagrams, the replacement occurs in the command parsing phase before the command is added to the registry.

Integration Point


Sources: src/CommandReplacer.php50-71

Command Aliasing Behavior

When remainAlias is true (the default), the original command name is preserved as an alias. This ensures backward compatibility:

  • Users can invoke commands using either the old or new name
  • Existing scripts and documentation continue to work
  • Deprecation can be gradual without breaking changes

Example:

  • php artisan gen:aspect MyAspect → executes make:aspect
  • php artisan make:aspect MyAspect → executes make:aspect
  • Both invocations target the same command

Sources: src/CommandReplacer.php62-64


Configuration and Extension

The CommandReplacer uses a static array for its replacement map, which means the mappings are fixed at the package level. To customize command replacements:

Modifying the Replacement Map

Applications that need custom replacement behavior should extend the CommandReplacer class and override the $commands property:

// Custom replacer class structure
class CustomCommandReplacer extends CommandReplacer
{
 protected static array $commands = [
 // Add custom replacements
 'my:command' => 'new:command',
 // Remove unwanted mappings by omitting them
 ];
}

Disabling Aliasing

To rename commands without maintaining backward compatibility, pass false for the $remainAlias parameter:

CommandReplacer::replace($command, false);

This will rename the command without creating an alias, making only the new name available for invocation.

Sources: src/CommandReplacer.php50-64


Summary

The command replacement and aliasing system provides:

  1. Naming Standardization: Converts legacy gen:* commands to make:* pattern
  2. Backward Compatibility: Original names remain accessible as aliases
  3. Command Removal: Eliminates commands not applicable to Hypervel
  4. Description Updates: Clarifies command purposes during renaming
  5. Framework Integration: Operates transparently during application bootstrap

The system operates as a simple but effective transformation layer, requiring no runtime overhead once commands are registered.

Sources: src/CommandReplacer.php1-72