Skip to content

Internals Bootstrap Cli Handler

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

bootstrap/cli/handler.php

Framework Internals · Subsystem overview · Developer guide

Source: bootstrap/cli/handler.php · Snapshot 8411f4287706.

Bootstrap scripts wire globals, constants and collaborators in a defined order. These files are executable entry points rather than a reusable class API.

Imports and dependencies

use cli\CLIHandler;
use cli\commands\ClearCommand;
use cli\commands\HelloCommand;
use cli\commands\InstallCommand;
use cli\commands\PackageManagerCommand;
use cli\commands\RoutesCommand;
use cli\commands\VersionCommand;

Execution and global state

The script below is executed by its caller. Constants, registered callbacks, global assignments, guards, included files and their order are preserved here. Unlike class methods, entry-point execution can start the CLI loop, route a request, emit output, or terminate the process.

<?php
ini_set('max_execution_time', 0);

global $routerManager;
global $loader;

use cli\CLIHandler;
use cli\commands\ClearCommand;
use cli\commands\HelloCommand;
use cli\commands\InstallCommand;
use cli\commands\PackageManagerCommand;
use cli\commands\RoutesCommand;
use cli\commands\VersionCommand;

global $cliHandler;
$cliHandler = new CLIHandler();

$_SERVER['REQUEST_URI'] = '/CLI/';

$cliHandler->addCommand(new InstallCommand());

if (DB_TYPE !== '') {
    $cliHandler->addCommand(new ClearCommand());
    $cliHandler->addCommand(new VersionCommand());
    $cliHandler->addCommand(new HelloCommand());
    $cliHandler->addCommand(new PackageManagerCommand());

    $loader->CLIHandler = $cliHandler;

    $rc = new RoutesCommand();
    $rc->addRoutes($routerManager->getRoutes());
    $cliHandler->addCommand($rc);
}
$argv = $_SERVER['argv'] ?? [];
array_shift($argv);
$firstCommand = $argv ? ['cmd' => array_shift($argv), 'args' => $argv] : null;

while (true) {
    if ($firstCommand !== null) {
        $cmd = $firstCommand['cmd'];
        $args = $firstCommand['args'];
        $firstCommand = null;
    } else {
        $input = readline("awt> ");
        if ($input === false) break;
        if (!$input) continue;

        try {
            $parts = $cliHandler->parseInput($input);
        } catch (\InvalidArgumentException $exception) {
            echo $exception->getMessage() . PHP_EOL;
            continue;
        }
        if ($parts === []) continue;
        $cmd = array_shift($parts);
        $args = $parts;
    }

    if ($cmd === 'help') {
        $cliHandler->help($args[0] ?? null);
        continue;
    }

    if ($cmd === 'clear') {
        echo "\033[2J\033[;H";
        continue;
    }

    $cliHandler->execute($cmd, $args);
}

Related types

Clone this wiki locally