VOOZH about

URL: https://deepwiki.com/hypervel/components/4-console-system

⇱ Console System | hypervel/components | DeepWiki


Loading...
Last indexed: 7 March 2026 (96fbab)
Menu

Console System

The Console System provides command-line interface functionality in Hypervel, including command discovery, execution, task scheduling, and development tools. It integrates Symfony Console with Hypervel's dependency injection container, supporting multiple command registration sources and providing code generation utilities for rapid application development.

This page provides an overview of the console architecture. For detailed information:

  • Command discovery and registration: See page 4.1 (Console Kernel and Command Discovery)
  • Task scheduling: See page 4.2 (Task Scheduling)
  • Code generation tools: See page 4.3 (Code Generation and Development Tools)

Architecture Overview

The console system is built around three major components:

  1. Console Kernel (Hypervel\Foundation\Console\Kernel) - Orchestrates command discovery, registration, and execution
  2. Task Scheduler (Hypervel\Console\Scheduling\Schedule) - Manages scheduled command execution
  3. Development Tools (Hypervel\Devtool package) - Provides code generation commands

Diagram: Console System Components


Sources: src/foundation/src/Console/Kernel.php1-438 src/devtool/src/ConfigProvider.php1-82


Console Kernel

The Kernel class (Hypervel\Foundation\Console\Kernel) serves as the central command manager, responsible for bootstrapping, command discovery, and execution coordination.

Key Properties

PropertyTypePurpose
$artisanApplicationContractThe Artisan console application instance
$commandsarrayExplicitly registered command class names
$closureCommandsarrayClosure-based command instances
$commandPathsarrayDirectories for automatic command discovery
$commandRoutePathsarrayFiles containing command route definitions
$loadedPathsarrayPaths that have been loaded
$commandsLoadedboolWhether commands have been loaded
$bootstrappersarrayConsole application bootstrapper classes

Sources: src/foundation/src/Console/Kernel.php36-76

Initialization and Bootstrapping

The kernel initializes during construction and triggers application bootstrapping when the Artisan instance is first accessed.

Diagram: Kernel Initialization Flow


Sources: src/foundation/src/Console/Kernel.php77-120

Bootstrappers

The console kernel uses a minimal set of bootstrappers compared to the HTTP kernel:

  1. RegisterFacades - Registers facade aliases
  2. RegisterProviders - Registers service providers
  3. BootProviders - Boots registered service providers

These bootstrappers ensure the application is properly initialized before command execution.

Sources: src/foundation/src/Console/Kernel.php71-75


Command Discovery and Collection

The kernel collects commands from six distinct sources, merging them into a unified command registry.

Command Collection Process

Diagram: Command Collection Workflow


Sources: src/foundation/src/Console/Kernel.php149-192

Command Sources Detail

1. Loaded Paths

Commands are discovered from directories added via load() or addCommandPaths():


The kernel uses ReflectionManager::getAllClasses() to scan these directories for command classes.

Sources: src/foundation/src/Console/Kernel.php332-347 src/foundation/src/Console/Kernel.php152-155

2. Explicit Commands

Command class names directly registered in the $commands array:


Sources: src/foundation/src/Console/Kernel.php41 src/foundation/src/Console/Kernel.php190

3. Config Commands

Commands defined in Hyperf's configuration system:


Sources: src/foundation/src/Console/Kernel.php158-160

4. Annotated Commands

Commands marked with Hyperf's @Command annotation:


Sources: src/foundation/src/Console/Kernel.php163-169

5. Closure Commands

Inline command definitions registered via command():


Closure commands are wrapped in ClosureCommand instances and stored in the container with a unique identifier.

Sources: src/foundation/src/Console/Kernel.php183-187 src/foundation/src/Console/Kernel.php311-327

6. Route Files

Command definitions loaded from files specified in commandRoutePaths:


Sources: src/foundation/src/Console/Kernel.php56 src/foundation/src/Console/Kernel.php139-143

Command Loading and Registration

After collection, commands are sorted to ensure Hyperf framework commands are registered first (allowing application commands to override them), then registered with the Artisan application.

Diagram: Command Registration Process


The pendingCommand() method (from HasPendingCommand trait) allows commands to define pending configurations like input options and output formatting.

Sources: src/foundation/src/Console/Kernel.php194-226


Artisan Console Application

The ConsoleApplication class wraps Symfony's Console Application, providing Laravel-style command execution with dependency injection support.

Key Features

FeatureDescription
Container IntegrationCommands resolved through DI container
Command LoaderContainer-based lazy loading
Output BufferingCaptures command output for testing
Dependency InjectionConstructor and method injection for commands

Command Execution

Commands can be executed in three ways:

  1. CLI Invocation: php artisan command:name
  2. Programmatic Call: $kernel->call('command:name', $params)
  3. Scheduled Execution: Via the task scheduler

Diagram: Command Execution Paths


Sources: src/foundation/src/Console/Kernel.php95-98 src/foundation/src/Console/Kernel.php233-236 src/foundation/src/Console/Kernel.php402-417


Scheduling Overview

The kernel integrates with the task scheduling system through the Schedule class, which is bound to the container during application boot.

Schedule Initialization

The kernel registers a callback during construction that binds the Schedule instance to the container after the application boots:

Diagram: Schedule Binding


Sources: src/foundation/src/Console/Kernel.php87-89 src/foundation/src/Console/Kernel.php274-281

Schedule Definition

Applications define scheduled tasks by overriding the schedule() method:


The scheduler supports timezone configuration via scheduleTimezone() and cache-based mutexes via scheduleCache().

For detailed scheduling features, see page 4.2 (Task Scheduling).

Sources: src/foundation/src/Console/Kernel.php257-299


Kernel Interface

The Kernel contract defines the public API for console command management.

Core Methods

Table: Kernel Interface Methods

MethodParametersReturnPurpose
bootstrap()-voidBootstrap application for commands
handle()InputInterface, OutputInterface?mixedRun console application
schedule()SchedulevoidDefine scheduled tasks
commands()-voidRegister application commands
command()string, ClosureClosureCommandRegister closure command
load()array|stringvoidAdd command discovery paths
addCommands()arraystaticAdd explicit command classes
addCommandPaths()arraystaticAdd command discovery paths
addCommandRoutePaths()arraystaticAdd command route file paths
getLoadedPaths()-arrayGet loaded paths
registerCommand()stringvoidRegister single command
call()string, array, OutputInterface?mixedExecute command by name
all()-arrayGet all registered commands
output()-stringGet last command output
getArtisan()-ApplicationContractGet Artisan instance
setArtisan()ApplicationContractvoidSet Artisan instance

Sources: src/foundation/src/Console/Contracts/Kernel.php1-97


Command Priority and Overriding

Commands are loaded with a specific priority to allow application commands to override framework commands:

  1. Hyperf framework commands - Registered first
  2. Application commands - Registered second, can override framework commands

This ordering ensures that if an application defines a command with the same name as a framework command, the application version takes precedence.

Sources: src/foundation/src/Console/Kernel.php197-213


Usage Patterns

Extending the Kernel

Create a custom kernel by extending Hypervel\Foundation\Console\Kernel:


Registering Closure Commands

In route files or service providers:


Programmatic Command Execution


Sources: src/foundation/src/Console/Kernel.php233-252 src/foundation/src/Console/Kernel.php311-327


Testing Support

The kernel provides methods for testing command execution:


The call() method accepts an optional OutputInterface for capturing output, and the output() method retrieves the output from the last executed command.

Sources: src/foundation/src/Console/Kernel.php233-252