BusServiceProvider.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Illuminate\Bus;
  3. use Aws\DynamoDb\DynamoDbClient;
  4. use Illuminate\Contracts\Bus\Dispatcher as DispatcherContract;
  5. use Illuminate\Contracts\Bus\QueueingDispatcher as QueueingDispatcherContract;
  6. use Illuminate\Contracts\Queue\Factory as QueueFactoryContract;
  7. use Illuminate\Contracts\Support\DeferrableProvider;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\ServiceProvider;
  10. class BusServiceProvider extends ServiceProvider implements DeferrableProvider
  11. {
  12. /**
  13. * Register the service provider.
  14. *
  15. * @return void
  16. */
  17. public function register()
  18. {
  19. $this->app->singleton(Dispatcher::class, function ($app) {
  20. return new Dispatcher($app, function ($connection = null) use ($app) {
  21. return $app[QueueFactoryContract::class]->connection($connection);
  22. });
  23. });
  24. $this->registerBatchServices();
  25. $this->app->alias(
  26. Dispatcher::class, DispatcherContract::class
  27. );
  28. $this->app->alias(
  29. Dispatcher::class, QueueingDispatcherContract::class
  30. );
  31. }
  32. /**
  33. * Register the batch handling services.
  34. *
  35. * @return void
  36. */
  37. protected function registerBatchServices()
  38. {
  39. $this->app->singleton(BatchRepository::class, function ($app) {
  40. $driver = $app->config->get('queue.batching.driver', 'database');
  41. return $driver === 'dynamodb'
  42. ? $app->make(DynamoBatchRepository::class)
  43. : $app->make(DatabaseBatchRepository::class);
  44. });
  45. $this->app->singleton(DatabaseBatchRepository::class, function ($app) {
  46. return new DatabaseBatchRepository(
  47. $app->make(BatchFactory::class),
  48. $app->make('db')->connection($app->config->get('queue.batching.database')),
  49. $app->config->get('queue.batching.table', 'job_batches')
  50. );
  51. });
  52. $this->app->singleton(DynamoBatchRepository::class, function ($app) {
  53. $config = $app->config->get('queue.batching');
  54. $dynamoConfig = [
  55. 'region' => $config['region'],
  56. 'version' => 'latest',
  57. 'endpoint' => $config['endpoint'] ?? null,
  58. ];
  59. if (! empty($config['key']) && ! empty($config['secret'])) {
  60. $dynamoConfig['credentials'] = Arr::only($config, ['key', 'secret']);
  61. if (! empty($config['token'])) {
  62. $dynamoConfig['credentials']['token'] = $config['token'];
  63. }
  64. }
  65. return new DynamoBatchRepository(
  66. $app->make(BatchFactory::class),
  67. new DynamoDbClient($dynamoConfig),
  68. $app->config->get('app.name'),
  69. $app->config->get('queue.batching.table', 'job_batches'),
  70. ttl: $app->config->get('queue.batching.ttl', null),
  71. ttlAttribute: $app->config->get('queue.batching.ttl_attribute', 'ttl'),
  72. );
  73. });
  74. }
  75. /**
  76. * Get the services provided by the provider.
  77. *
  78. * @return array
  79. */
  80. public function provides()
  81. {
  82. return [
  83. Dispatcher::class,
  84. DispatcherContract::class,
  85. QueueingDispatcherContract::class,
  86. BatchRepository::class,
  87. DatabaseBatchRepository::class,
  88. ];
  89. }
  90. }