Commands

A integral part of Laravel is being able to use the command line to interact with an app. This is no different for modules - you may decide that your module would benefit from having commands. These shouldn’t be essential parts of your app unless you provide an entry point through the web, since users of the portal may not have direct access to the console.

An example may be a command to clear up old entries in a database. Your module doesn’t rely on it, but it allows you to provide a simple way to use the functionality, and it can later be used via the web/api.

Commands should be created using the default Laravel command structure.

Registering Commands

Having written a command, you will need to register the command so the portal knows it exists. If you don’t do this, your command will not be accessible outside your module.

The easiest way to register it is to put the class name into the $commands array in the service provider

protected $commands = [
    \My\Namespace\CommandOne::class,
    \My\Namespace\CommandTwo::class,
];

You may also use the commands() method, which takes an array of command class names.

Scheduling Commands

You may decide your command to clear up old database entries should be run every so often automatically. To do this, we would normally enter an entry into the console Kernel in the portal/playground, but this would require additional setup for each module. Therefore, the SDK provides a way to run scheduled commands using cron.

To run a command automatically, you can add the command to the $scheduledCommands array in the service provider. Each command must also be registered as above, and each command must have an associated cron string.

protected $scheduledCommands = [
    \My\Namespace\CommandOne::class => '* * * * *',
    ...
];

You may also use th CommandStore interface directly in your service provider. To do this, resolve \BristolSU\Support\ModuleInstance\Contracts\Scheduler\CommandStore from the container, and use the schedule method, passing it your module alias, command class name and the cron string.

public function boot()
{
    $commandStore = $this->app->make(\BristolSU\Support\ModuleInstance\Contracts\Scheduler\CommandStore::class);
    $commandStore->schedule('my-module-alias', \My\Namespace\CommandOne::class, '* * * * *');
}

The SDK will make sure these commands are now run at the specified times according to the cron string. It is recommended not to run the command too often where possible, as it can make your module expensive to run for the user.