Skip to content
Bertie2011 edited this page Aug 29, 2021 · 1 revision

Command extensions have the same purpose as subcommands, namely isolating reused code. The advantages are that users don't have to type the subcommand name inside the full command and that extensions can be active side by side, while subcommands are hierarchical. The disadvantages are that there is no autoloading, autocorrecting no access to arguments.

Creating an extension

Create a subclass from CLIFramework\Extension\CommandExtension or CLIFramework\Extension\ApplicationExtension depending on what the extension applies to. Instead of the CommandExtension, the DaemonExtension can be used as base class to run the execute code on a background thread.

Just as with commands, the following methods are available to be overridden:

  • public function init(): void
  • public function options($opts): void
  • public function prepare(): void
  • public function execute(): void
  • public function finish(): void

Each of these methods is called at the same time as the same method on the command or application.

Note that creating an arguments($args) method to register arguments won't work and that the execute() method won't receive arguments either.

Registering an extension

Inside the init(): void method of a command or application, you can call $this->extension($extension) to register the extension. As arguments, you can either pass an existing instance of the exception or a class, like MyExtension::class.