laravel-resque/src/ResqueServiceProvider.php
idanoo 926f8a887c 2.0.0: Add namespacing (#1)
2.0.0: Namespace for idanoo/php-resque
Co-Authored-By: idanoo <daniel@m2.nz>
Co-Committed-By: idanoo <daniel@m2.nz>
2021-02-19 13:44:10 +13:00

73 lines
1.9 KiB
PHP

<?php
namespace Idanoo\Resque;
use Idanoo\Resque\Console\WorkCommand;
use Illuminate\Support\ServiceProvider;
/**
* ResqueServiceProvider
*
* @author Holger Reinhardt <hlgrrnhrdt@gmail.com>
*/
class ResqueServiceProvider extends ServiceProvider
{
/**
*
*/
public function boot()
{
$this->setRedisConfig();
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerResque();
$this->registerWorkCommand();
}
protected function registerResque()
{
$this->app->singleton(Resque::class, function () {
$prefix = $this->app['config']['resque.prefix'];
return (new Resque())->setPrefix($prefix ?: 'resque');
});
}
protected function registerWorkCommand()
{
$this->app->singleton('command.resque.work', function () {
return new WorkCommand($this->app->make(Resque::class));
});
$this->commands('command.resque.work');
}
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'];
}
$host = isset($config['host']) ? $config['host'] : 'localhost';
$port = isset($config['port']) ? $config['port'] : 6379;
$database = isset($config['database']) ? $config['database'] : 0;
$password = isset($config['password']) ? $config['password'] : '';
$server = implode(':', [$host, $port]);
if ($password) {
$server = implode('@', [implode(':', ['', $password]), $server]);
}
$dsn = sprintf('redis://%s/%s', $server, $database);
\Resque\Resque::setBackend($dsn);
}
}