Skip to content

Command Autoloading

Bertie2011 edited this page Aug 29, 2021 · 8 revisions

CLIFramework lets you register your commands in the init(): void method. However, if you have many commands, and some commands are from elsewhere (e.g. external packages), you might want to take a look at the command autoloading feature.

To enable autoloading, call $this->enableCommandAutoload(); in the init(): void method, before parent::init(). Place all top-level commands in a Command directory in the same folder as the Application class. Place sub-commands in a directory with the same name and in the same folder as the parent command. Also make sure that command namespaces match the folder structure.

The command name will be derived from the class name.

For example, the following file structure:

app/Console.php
app/Command/XCommand.php
app/Command/YCommand.php
app/Command/YCommand/XCommand.php
app/Command/YCommand/TestDemoCommand.php
app/Command/ZCommand.php
app/Command/ZCommand/AnotherCoolCommand.php

Causes the following commands to be registered:

program x
program y
program y x
program y test-demo
program z
program z another-cool