Last active
December 21, 2020 03:52
-
-
Save ctf0/142e1433c10da2e726cd9b4fe49ac3a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Closure; | |
class CommandEventsListener | |
{ | |
protected $startCallbacks = []; | |
protected $finishCallbacks = []; | |
public function __construct($commandName) | |
{ | |
app('events')->listen('Illuminate\Console\Events\CommandStarting', function ($event) use ($commandName) { | |
if ($event->command == $commandName) { | |
$this->callStartCallbacks(); | |
} | |
}); | |
app('events')->listen('Illuminate\Console\Events\CommandFinished', function ($event) use ($commandName) { | |
if ($event->command == $commandName) { | |
$this->callFinishCallbacks(); | |
} | |
}); | |
} | |
public function onStart(Closure $callback) | |
{ | |
$this->startCallbacks[] = $callback; | |
return $this; | |
} | |
public function onFinish(Closure $callback) | |
{ | |
$this->finishCallbacks[] = $callback; | |
return $this; | |
} | |
protected function callStartCallbacks() | |
{ | |
foreach ($this->startCallbacks as $callback) { | |
$this->exec($callback); | |
} | |
} | |
protected function callFinishCallbacks() | |
{ | |
foreach ($this->finishCallbacks as $callback) { | |
$this->exec($callback); | |
} | |
} | |
protected function exec($callBack) | |
{ | |
return call_user_func($callBack); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if you want to also check for the command params u can do
so the full condition would be