Repository navigation
Internals Cli CLIHandler
Framework Internals · Subsystem overview · Developer guide
Source: awt_src/classes/cli/CLIHandler.php · Snapshot 8411f4287706.
The CLI registers command objects and executes one command, then returns to a readline loop. Help and clear have special handling in the loop.
use cli\interfaces\CLICommand;class CLIHandlerDeclarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.
| Member | Declaration | Purpose or source contract |
|---|---|---|
| $commands | private array $commands = []; |
Stores commands in format: "command" => CLICommand @var CLICommand[] |
| Method | Signature |
|---|---|
| parseInput | public function parseInput(string $input): array |
| addCommand | public function addCommand(CLICommand $command): void |
| getCommands | public function getCommands(): array |
| execute | public function execute(string $command, array $args = []): void |
| help | public function help(?string $command = null): void |
| getGlobalHelp | private function getGlobalHelp(): string |
public function parseInput(string $input): array| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$input |
string $input |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
Parse interactive input without expanding variables or executing shell syntax.
Return expressions (including local callbacks): $parts.
Explicit exceptions: \InvalidArgumentException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Implementation, validation guards and interaction order
/** Parse interactive input without expanding variables or executing shell syntax. */
public function parseInput(string $input): array
{
$parts = [];
$value = '';
$quote = null;
$started = false;
$length = strlen($input);
for ($i = 0; $i < $length; ++$i) {
$char = $input[$i];
if ($char === '\\' && $quote !== "'") {
if ($i + 1 === $length) {
throw new \InvalidArgumentException('Incomplete escape at end of command.');
}
$next = $input[$i + 1];
if ($quote === '"' && $next !== '"' && $next !== '\\') {
$value .= $char;
} else {
$value .= $input[++$i];
}
$started = true;
} elseif ($quote !== null) {
if ($char === $quote) {
$quote = null;
} else {
$value .= $char;
}
} elseif ($char === '"' || $char === "'") {
$quote = $char;
$started = true;
} elseif (ctype_space($char)) {
if ($started) {
$parts[] = $value;
$value = '';
$started = false;
}
} else {
$value .= $char;
$started = true;
}
}
if ($quote !== null) {
throw new \InvalidArgumentException('Unclosed quote in command.');
}
if ($started) {
$parts[] = $value;
}
return $parts;
}public function addCommand(CLICommand $command): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$command |
CLICommand $command |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
State writes: $this->commands[$command->getCommand()].
Implementation, validation guards and interaction order
public function addCommand(CLICommand $command): void
{
$this->commands[$command->getCommand()] = $command;
}public function getCommands(): arrayReturns list of registered command names
@return string[]
Return expressions (including local callbacks): array_keys($this->commands).
Implementation, validation guards and interaction order
/**
* Returns list of registered command names
*
* @return string[]
*/
public function getCommands(): array
{
return array_keys($this->commands);
}public function execute(string $command, array $args = []): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$command |
string $command |
* @param array $args |
$args |
array $args = [] |
Ordered arguments passed to the command or method. |
Execute a registered command
@param string $command @param array $args @return void
Delegates to getGlobalHelp().
Implementation, validation guards and interaction order
/**
* Execute a registered command
*
* @param string $command
* @param array $args
* @return void
*/
public function execute(string $command, array $args = []): void
{
if (!isset($this->commands[$command])) {
echo "Unknown command: {$command}\n";
echo $this->getGlobalHelp();
return;
}
$cmd = $this->commands[$command];
$cmd->execute($command, $args);
$output = $cmd->result();
if ($output !== '') {
echo $output . PHP_EOL;
}
}public function help(?string $command = null): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$command |
?string $command = null |
* @return void |
Print help for a specific command or all commands
@param string|null $command @return void
Delegates to getGlobalHelp().
Implementation, validation guards and interaction order
/**
* Print help for a specific command or all commands
*
* @param string|null $command
* @return void
*/
public function help(?string $command = null): void
{
if ($command === null) {
echo $this->getGlobalHelp();
return;
}
if (!isset($this->commands[$command])) {
echo "No such command: {$command}\n";
return;
}
$args = "";
foreach ($this->commands[$command]->getArguments() as $arg => $value) {
$args .= "\n{$arg}: {$value}";
}
echo $this->commands[$command]->getHelp() . PHP_EOL . "Arguments: {$args} ". PHP_EOL;
}private function getGlobalHelp(): stringReturn expressions (including local callbacks): implode(PHP_EOL, $lines) . PHP_EOL.
Implementation, validation guards and interaction order
private function getGlobalHelp(): string
{
$lines = ['Available commands:'];
foreach ($this->commands as $name => $cmd) {
$lines[] = sprintf(" %-15s %s", $name, strtok($cmd->getHelp(), "\n"));
}
$lines[] = "\nRun: php awt <command> [args...]";
$lines[] = "Use 'help <command>' for full command help.";
return implode(PHP_EOL, $lines) . PHP_EOL;
}AWT Framework Wiki · Home · Installation Guide · Developer Guide · Framework Internals
Philosophy · Architecture · Known discrepancies · Verification
Installation Guide
Developer Guide
- Learning sequence
- First package
- Package structure
- Lifecycle
- Runtimes
- Events and objects
- Routing and controllers
- Views and resources
- Database and models
- Storage and caching
- Configuration and CLI
- Responses and errors
- Extension points
Framework Internals
- Architecture
- Bootstrap
- Packages
- Runtime
- Events and context
- Objects
- HTTP routing and controllers
- Rendering
- Blade integration
- Database
- Models
- Storage and caching
- Settings
- CLI
- Identity and sessions
- Responses
- Errors
- Complete source inventory
Documentation notes