jsiebach/commander

A Laravel Backpack-driven interface for executing artisan commands

dev-master 2018-12-13 21:57 UTC

This package is not auto-updated.

Last update: 2024-11-24 05:52:18 UTC


README

Backpack Commander is a simple CRUD interface for running Artisan commands in Backpack for Laravel. The main benefit is to quickly allow non-developers to run Artisan commands without needing command line access, and to easily configure the arguments and options for the command line using Backpack field definitions.

Commands CRUD Running a command using Backpack for Laravel Commander

Installation

composer require jsiebach/commander

  • Publish the config file

php artisan vendor:publish --provider="JSiebach\Commander\CommanderServiceProvider" --tag="config"

  • Publish the migration

php artisan vendor:publish --provider="JSiebach\Commander\CommanderServiceProvider" --tag="migrations"

  • Review the config file to update configuration for your app. Available options are:

route: Edit the route for the commander interface. Defaults to /admin/commander/command.

allow_creation_and_deletion: Determine whether new commands can be added or deleted. It is recommended to keep this option false in a production environment and add new commands directly in the database.

  • Run php artisan migrate

  • Add your commands interface link to the sidebar (Optional)

<li><a href="{{ backpack_url('commander')."/command" }}"><i class="fa fa-bullhorn"></i> <span>Commands</span></a></li>

Adding commands to Commander

You can now add new commands to your commander_commands table. The fields are:

command: The artisan command (ie. inspire - whatever you would fill in for php artisan XXXXX)

descriptive_name: A name to show users for the command. Will default to the artisan command name

The CRUD interface will also show a column with the description property of the command.

Defining commands

To define a command, add a function to the command called getCommanderFields(), which should return an array of Backpack field definitions.

  • For simple commands like inspire, you can leave off the getCommanderFields() function entirely.

  • For commands that take arguments and options, you should return the Backpack field configuration in the getCommanderFields() function on the command. For example:

<?php

namespace App\Console\Commands;

use App\User;
use Illuminate\Console\Command;
use JSiebach\Commander\CommandableInterface;

class RunNewUsersReport extends Command implements CommandableInterface
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'reports:run-new-users-report {--startdate=} {--include-admins} {--delivery-email=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run a report listing the newest users';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // You can use the arguments of the artisan command as normal:
    	$this->comment('The email address to send the report to is '.$this->option('email'));
        $this->comment('The option \'include-admins\' is '.(((boolean) $this->option('include-admins')) ? "true" : "false"));
    	$this->comment('The start date for the report is '.$this->option('start-date'));
    }

	/**
	 * @return array
	 */
	public function getCommanderFields() {
		return [
			[
				'name' => '--startdate',
				'type' => 'date_picker',
				'label' => 'Start Date'
			],
			[
				'name' => '--include-admins',
				'type' => 'checkbox',
				'label' => 'Include Admin Users'
			],
			[
				'name' => '--delivery-email',
				'type' => 'select2_from_array',
				'options' => User::all()->pluck('name', 'email'),
				'label' => 'Report Recipient'
			]
		];
	}
}

Notes

  • You cannot use relationship fields, since there is no model to be related to. Stick to simple field types that return strings, booleans, or arrays.

Running a command

In the commands CRUD interface, click the Run button to run a command. You will see a form generated by the fields defined on the command.

Fill out the fields and click Run. The output of the command will be printed in the resulting view.

Queueing a command

If you want to run a command on the queue, add the field --queue_command. If you want this to be an option, you can use a check box. If you want it to be automatic, use a hidden field with value 1.

You can specify the name of the queue with the --queue_name option. Defaults to default

Future development plans

  • Handle command errors
  • Consider ways to generate the index view without having a table (ie. define $commandable = true on commands that should be included)

Support

Please feel free to submit issues or pull requests on Github.

License

MIT

Credits

Backpack for Laravel