CLI Command

Apex also makes it easy to develop new CLI commands that can be run from the terminal. Within terminal run the followingcreate cli command:

apex create cli disaster view-project

Open the newly generated file at /src/Disaster/Opus/Cli/ViewProject.php, and replace it with the following contents:

<?php
declare(strict_types = 1);

namespace App\Disaster\Opus\Cli;

use Apex\App\Cli\{Cli, CliHelpScreen};
use App\Disaster\Models\Project;
use Apex\App\Interfaces\Opus\CliCommandInterface;

/**
 * CLI Command -- ./apex disaster view_project
 */
class ViewProject implements CliCommandInterface
{

    /**
     * Process
     */
    public function process(Cli $cli, array $args):void
    {

        // Get desired flags with values
        $opt = $cli->getArgs(['title']);
        $title = $opt['title'] ?? '';
        $project_id = $args[0] ?? 0;

        // Check for project
        $proj = null;
        if ($title != '' && !$proj = Project::whereFirst('title = %s', $title)) {
            $cli->error("No project exists with the title, $title");
            return;
        } elseif ($proj === null && $project_id > 0 && !$proj = Project::whereId($project_id)) {
            $cli->error("No project exists with the id#, $project_id");
            return;
        }

        // Display response
        $cli->send("Successfully found the project, displaying results in a crude fashion.\n\n");
        print_r($proj->toArray());
    }

    /**
     * Help
     */
    public function help(Cli $cli):CliHelpScreen
    {

        $help = new CliHelpScreen(
            title: 'View Project',
            usage: 'disaster view_project [<PROJECT_ID>] [--title <TITLE>]',
            description: 'View details on a project'
        );

        // Add parameters
        $help->addParam('project_id', 'The id# of the project.');

        // Add optional flags
        $help->addFlag('--title', 'The title of the project.');

        // Add example
        $help->addExample('./apex disaster view_project 51');
        $help->addExample('./apex disaster view_project --title "Some Title"');

        // Return
        return $help;
    }

}

Every CLI command class contains two functions, process() which is executed when the command is processed and help() to provide a help screen. For full details, please visit the CLI Command page of the developer documentation.

The above CLI command accepts either, one additional argument of a project id# or a --project flag of a project id#, which is done to give you an example of both. The command can be executed with:

apex disaster view-project 1

Naturally, change the 1 to the id# of a project within your database if necessary. You may also get the same results using the --project flag with the command:

apex disaster view-project --project 1

Again, this was simply done to provide an example of both, arguments and flags within CLI commands. You may also view the help screen developed within the help() function by running the command:

apex help disaster view-project

For full details on CLI commands, please consult the CLI Commands page of the developer documentation.