Last active
March 15, 2019 14:42
-
-
Save martinbean/16399826a790b72c685746c6287f833e to your computer and use it in GitHub Desktop.
Mapping jobs to handler classes in Lumen
This file contains 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\Handlers\Jobs; | |
use App\Jobs\LogNameJob; | |
use Illuminate\Support\Facades\Log; | |
class LogNameHandler | |
{ | |
public function __construct() | |
{ | |
// Inject any dependencies the handler needs | |
} | |
public function handle(LogNameJob $job) | |
{ | |
// Handle the job | |
Log::debug($job->name); // Will write 'Martin Bean' to log | |
} | |
} |
This file contains 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\Jobs; | |
class LogNameJob extends Job | |
{ | |
/** | |
* The name to log. | |
* | |
* @var string | |
*/ | |
public $name; | |
/** | |
* Create a new job instance. | |
* | |
* @param string $name | |
* @return void | |
*/ | |
public function __construct(string $name) | |
{ | |
$this->name = $name; | |
} | |
} |
This file contains 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\Providers; | |
use App\Handlers\Jobs\LogNameHandler; | |
use App\Jobs\LogNameJob; | |
use Illuminate\Support\Facades\Bus; | |
use Illuminate\Support\ServiceProvider; | |
class LogNameServiceProvider extends ServiceProvider | |
{ | |
public function boot() | |
{ | |
Bus::map([ | |
LogNameJob::class => LogNameHandler::class, | |
]); | |
} | |
} |
This file contains 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 | |
use App\Jobs\LogNameJob; | |
use Illuminate\Support\Facades\Bus; | |
$router->get('/', function () use ($router) { | |
// When the queue processes LogNameJob, | |
// it should invoke the LogNameHandler class. | |
Bus::dispatch(new LogNameJob('Martin Bean')); | |
return response('Dispatched.'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment