2017 © Pedro Peláez
 

library consoleevents

Events for Laravel Console Commands

image

bmitch/consoleevents

Events for Laravel Console Commands

  • Saturday, December 10, 2016
  • by bmitch
  • Repository
  • 0 Watchers
  • 17 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Console Events for Laravel Commands

Build Status, (*1)

What is it?

This package allows you to have events triggered by your Artisan Commands. The events available are:, (*2)

Bmitch\ConsoleEvents\Events\CommandStarting Triggered when an Artisan Command is starting., (*3)

Bmitch\ConsoleEvents\Events\CommandTerminating Triggered when an Artisan Command is terminating., (*4)

Why use it?

The main reason I created this package was for a use case where multiple commands were executed nightly and I wanted an easy way to log when they started and stopped. By hooking into these events it makes it easy., (*5)

How to Install

Add to composer

composer require bmitch/consoleevents

Modify commands to extend custom class

In any command that you wish to trigger these events simply replace the:, (*6)

use Illuminate\Console\Command;

with, (*7)

use Bmitch\ConsoleEvents\Command;

Create and Register Listeners

Create two listeners within the app/Listeners folder like this:, (*8)

<?php

namespace App\Listeners;

use Log;
use Bmitch\ConsoleEvents\Events\CommandStarting;

class CommandStartingListener
{
    /**
     * Handle the event.
     *
     * @param  CommandStarting  $commandStartingEvent
     * @return void
     */
    public function handle(CommandStarting $commandStartingEvent)
    {
        $name = $commandStartingEvent->command->getName();
        Log::info("Command {$name} starting");
    }
}
<?php

namespace App\Listeners;

use Log;
use Bmitch\ConsoleEvents\Events\CommandTerminating;

class CommandTerminatingListener
{
    /**
     * Handle the event.
     *
     * @param  CommandTerminating  $commandTerminatingEvent
     * @return void
     */
    public function handle(CommandTerminating $commandTerminatingEvent)
    {
        $command = $commandTerminatingEvent->command;
        $name = $command->getName();

        Log::info("Command {$name} stopping", [
            'commandName' => $name,
            'executionTime' => $command->getExecutionTime(),
            'exitCode' => $commandTerminatingEvent->exitCode,
        ]);
    }
}



Then register it within the `app\Providers\EventServiceProvider.php` class:
/** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'Bmitch\ConsoleEvents\Events\CommandStarting' => [ 'App\Listeners\CommandStartingListener', ], 'Bmitch\ConsoleEvents\Events\CommandTerminating' => [ 'App\Listeners\CommandTerminatingListener', ], ];

## Seeing the results ## Run your command and check `laravel.log`. You should see an entry that was triggered by the `CommandStartingListener`. Something like:
[2016-12-02 00:16:11] local.INFO: Command foo:bar starting [2016-12-02 00:16:11] local.INFO: Command foo:bar stopping {"commandName":"foo:bar","executionTime":0.005375862121582,"exitCode":0}

Additional Methods

The Bmitch\ConsoleEvents\Command class automatically tracks how long it takes to execute and provides a getExecutionTime() method to make it easy to add this data when Logging data., (*9)

Contributing

Please see CONTRIBUTING.md, (*10)

License

The MIT License (MIT). Please see License File for more information., (*11)

The Versions

10/12 2016

dev-Issue#26

dev-Issue#26

Events for Laravel Console Commands

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

by Bill Mitchell

05/12 2016

dev-master

9999999-dev

Events for Laravel Console Commands

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

by Bill Mitchell

02/12 2016

1.0.0

1.0.0.0

Events for Laravel Console Commands

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

by Bill Mitchell

29/11 2016

dev-exceptionEvent

dev-exceptionEvent

Events for Laravel Console Commands

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

by Bill Mitchell

24/11 2016

0.0.3

0.0.3.0

Events for Laravel Console Commands

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

by Bill Mitchell

23/11 2016

0.0.2

0.0.2.0

Events for Laravel Console Commands

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

by Bill Mitchell

23/11 2016

0.0.1

0.0.1.0

Events for Laravel Console Commands

  Sources   Download

by Bill Mitchell