Cli Class
Revision: 2025-11-11
Manage command-line functions with exception-based error handling and formatted output.
Usage
# List all available commands
php milkadmin/cli.php
# Execute a specific command
php milkadmin/cli.php function_name arg1 arg2
Registering Commands
In modules (extends AbstractModule):
class Posts extends AbstractModule {
public function shellTest() {
Cli::echo("Test command executed");
}
}
// Automatically registered as: posts:test
Manual registration:
Cli::set('my:command', function($param1, $param2) {
Cli::echo("Param1: $param1, Param2: $param2");
});
Exception Handling
Throw exceptions in your CLI functions - the framework catches and displays them automatically:
use App\Exceptions\CliException;
Cli::set('user:validate', function($email) {
if (empty($email)) {
throw new CliException("Email required");
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new CliException("Invalid email: $email");
}
Cli::success("Valid email!");
});
Available exceptions:
CliException- Configuration/validation errorsCliFunctionExecutionException- Runtime errors (auto-wrapped)
Methods
set(string $name, callable $function) : void
Registers a CLI function.
Cli::set('test', function($arg) {
Cli::echo("Argument: $arg");
});
run(array $argv) : bool
Executes a registered function. Handles exceptions automatically.
callFunction(string $name, ...$args) : void
Calls a registered function programmatically.
Cli::callFunction('test', 'value1', 'value2');
getAllFn() : array
Returns names of all registered functions.
isCli() : bool
Checks if running from command line.
echo(string $msg) : void
Prints a message.
success(string $msg) : void
Prints a success message in green.
error(string $msg) : void
Prints an error message in red.
drawTitle(string $title, int $padding = 4, string $color = "\033[1;36m") : void
Draws a formatted title box with automatic width.
Cli::drawTitle("My App");
// ╔═══════════╗
// ║ My App ║
// ╚═══════════╝
drawSeparator(string $title = '', int $width = 40, string $color = "\033[0;33m") : void
Draws a separator line with optional centered title.
Cli::drawSeparator("Settings");
// ━━━━ Settings ━━━━
drawTable(array $data, array $columns = null) : void
Draws a formatted table.
Cli::drawTable([
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane']
]);
System Commands
create-administrator
Emergency administrator recovery command. Creates a new admin user with random secure password.
# Auto-generate username and email
php milkadmin/cli.php create-administrator
# Custom username
php milkadmin/cli.php create-administrator admin_user
# Custom username and email
php milkadmin/cli.php create-administrator admin_user admin@company.com
build-version
Updates the application version in configuration and database.
php milkadmin/cli.php build-version
update-paths
Updates directory paths and base URL configuration when moving the installation to a new location or changing the URL.
This command updates:
public_html/milkadmin.php- Updates MILK_DIR and LOCAL_DIR pathsmilkadmin_local/config.php- Updates base_url (when URL parameter is provided)
# Update only directory paths (automatic detection)
php milkadmin/cli.php update-paths
# Update paths AND change the base URL
php milkadmin/cli.php update-paths "http://localhost/new-path/public_html/"
# Example: moving to production
php milkadmin/cli.php update-paths "https://www.mysite.com/admin/"
Run this command after moving the installation directory or deploying to a different server/domain.
Examples
Complete CLI Command
use App\Exceptions\CliException;
Cli::set('user:list', function($role = null) {
Cli::drawTitle("User Listing");
// Validation with exceptions
if ($role && !in_array($role, ['admin', 'user'])) {
throw new CliException("Invalid role. Use: admin, user");
}
// Fetch and display data
$users = [
['id' => 1, 'name' => 'John', 'role' => 'admin'],
['id' => 2, 'name' => 'Jane', 'role' => 'user']
];
Cli::drawTable($users);
Cli::success("Done!");
});
// Execute: php milkadmin/cli.php user:list admin