laravel-resque/src/ResqueServiceProvider.php

73 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2016-07-28 21:52:05 +00:00
<?php
namespace Idanoo\Resque;
use Idanoo\Resque\Console\WorkCommand;
2016-07-28 21:52:05 +00:00
use Illuminate\Support\ServiceProvider;
/**
* ResqueServiceProvider
*
* @author Holger Reinhardt <hlgrrnhrdt@gmail.com>
*/
class ResqueServiceProvider extends ServiceProvider
{
2016-07-29 16:03:41 +00:00
/**
*
*/
public function boot()
{
2016-07-29 22:00:27 +00:00
$this->setRedisConfig();
2016-07-29 16:03:41 +00:00
}
2016-07-28 21:52:05 +00:00
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
2016-07-29 16:03:41 +00:00
$this->registerResque();
2016-07-28 21:52:05 +00:00
$this->registerWorkCommand();
}
2016-07-29 16:03:41 +00:00
protected function registerResque()
2016-07-28 21:52:05 +00:00
{
2016-07-29 22:00:27 +00:00
$this->app->singleton(Resque::class, function () {
$prefix = $this->app['config']['resque.prefix'];
2016-07-29 23:49:45 +00:00
return (new Resque())->setPrefix($prefix ?: 'resque');
2016-07-28 21:52:05 +00:00
});
}
protected function registerWorkCommand()
{
2016-07-29 12:05:07 +00:00
$this->app->singleton('command.resque.work', function () {
2016-07-29 22:00:27 +00:00
return new WorkCommand($this->app->make(Resque::class));
2016-07-28 21:52:05 +00:00
});
2016-07-29 12:02:56 +00:00
$this->commands('command.resque.work');
2016-07-28 21:52:05 +00:00
}
2016-07-29 22:00:27 +00:00
protected function setRedisConfig()
{
$default = $this->app['config']['resque.default'];
$config = $this->app['config'][sprintf('database.redis.%s', $default)];
if (!$config) {
$config = $this->app['config']['database.redis.default'];
}
2016-07-29 22:00:27 +00:00
$host = isset($config['host']) ? $config['host'] : 'localhost';
$port = isset($config['port']) ? $config['port'] : 6379;
$database = isset($config['database']) ? $config['database'] : 0;
2016-08-01 08:49:21 +00:00
$password = isset($config['password']) ? $config['password'] : '';
2016-07-29 22:00:27 +00:00
$server = implode(':', [$host, $port]);
2016-08-01 08:49:21 +00:00
if ($password) {
$server = implode('@', [implode(':', ['', $password]), $server]);
2016-08-01 08:49:21 +00:00
}
2016-07-29 22:00:27 +00:00
2016-08-01 08:49:21 +00:00
$dsn = sprintf('redis://%s/%s', $server, $database);
\Resque\Resque::setBackend($dsn);
2016-07-29 22:00:27 +00:00
}
2016-07-28 21:52:05 +00:00
}