Console Commands
Learn how to create your own custom console commands.
Create commands
To create your own command, you can use closures, callables, or create a class in the Commands namespace and then add it to system/console.php, for example:
use Inphinit\Experimental\Cli\Command;
$console->action('hello', function (Command $command, array $params, array $residual) {
echo 'Hello world!';
});
Then in the terminal, in the application's root folder, you can run the command:
./run hello
If you are running on Windows, or if the run file is defined as executable, the ./ prefix will not be necessary; simply use the command like this:
run hello
Commands with controllers
Commands support controllers in a similar way to routes, with the difference that the classes responsible for the commands must be added to the system/Commands/ folder. Example of use:
system/Commands/Foo/Bar/SampleCommand.php
<?php
namespace Commands\Foo\Bar;
use Inphinit\Viewing\View;
use Inphinit\Experimental\Cli\Command;
class TestCommand
{
public function test(Command $command, array $params, array $residual)
{
$name = $command->getName();
$input = $params['text'];
echo "Input: '{$input}' from '{$name}' command.";
}
}
system/console.php
<?php
use Inphinit\Experimental\Cli\Command;
$console->action('test', 'TestCommand::test')
->setOption('text', 't', Command::ARG_REQUIRED, null, 'Define a text');
Then in the terminal:
./run test -t "ping!"
Options
The commands also accept options in the format --long and optionally -short, for example:
use Inphinit\Experimental\Cli\Command;
$console->action('hello', function (Command $command, array $params, array $residual) {
var_dump($params);
})->setOption('name', 'n');
You can run the command:
./run hello --name "Ada"
Or using short:
./run hello -n "Ada"
The options accept optional arguments (default):
use Inphinit\Experimental\Cli\Command;
$command = $console->action(...);
$command->setOption('name', 'n', Command::ARG_OPTIONAL);
The options accept required arguments:
use Inphinit\Experimental\Cli\Command;
$command = $console->action(...);
$command->setOption('name', 'n', Command::ARG_REQUIRED);
The supported options can be used without a defined value in this way:
use Inphinit\Experimental\Cli\Command;
$command = $console->action(...);
$command->setOption('reindex', null, Command::ARG_NO_VALUE);
Which allows you to perform:
./run hello --reindex
If you try to enter a value, you will receive an error message:
./run hello --reindex "foobar"
Standard commands
| Command | Description |
|---|---|
run app:down |
Enable maintenance mode |
run app:up |
Disable maintenance mode |
run env:boot |
Optimizes the .env file environment variables by adding them to the boot cache |
run env:source |
Disable the cache of .env variables, making all requests require the application to parse the file |
run pkg:up |
Updates PSR-4 classes to use inphinit-autoload. Generally using this command manually is not necessary when in projects installed via Composer, as this command is usually executed automatically |
run serve |
Starts the built-in web server |