sammakescode/laravel-command-switches

This package is abandoned and no longer maintained. No replacement package was suggested.

A laravel helper library to allow you to switch commands on and off

v0.1.6 2020-01-31 14:10 UTC

This package is auto-updated.

Last update: 2022-02-19 20:23:43 UTC


README

The main purpose of this library is to introduce a stardard way of switching console commands on and off. It's something I regularly want to do in the event of a failing job.

Requirements

  • PHP 7.1+
  • Laravel 5.6+

Installation

This library can be installed simply with composer.

composer require sammakescode/laravel-command-switches

After installation, run your migrations.

php artisan migrate

Usage

Checking if your command is switched on

In your command use the Switchable trait.

    use SamMakesCode\CommandSwitches\Traits\Switchable;

You can then use the following functionality to determine if the command has been switched off.

    if ($this->isOff()) {
        \Log::notice($this->signature . ' is switched off');
        return;
    }
    // Your functionality

You can also use isOn().

    if ($this->isOn()) {
        // Your functionality
    }

Here's a more complete example to add some context.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use SamMakesCode\CommandSwitches\Traits\Switchable;

class SomeCommand extends Command
{
    use Switchable;

    protected $signature = 'some-command';

    protected $description = 'Command description';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        if ($this->isOn()) {
            // Your functionality
        }
    }
}

Switching you commands on and off

You can toggle your commands and off anywhere in your code (such as in a admin section) like this:

    \SamMakesCode\CommandSwitches\CommandSwitch::findOrCreate(SomeCommand::class)->setOn();