Manager.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Illuminate\Queue\Capsule;
  3. use Illuminate\Queue\QueueManager;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Queue\QueueServiceProvider;
  6. use Illuminate\Support\Traits\CapsuleManagerTrait;
  7. /**
  8. * @mixin \Illuminate\Queue\QueueManager
  9. * @mixin \Illuminate\Contracts\Queue\Queue
  10. */
  11. class Manager
  12. {
  13. use CapsuleManagerTrait;
  14. /**
  15. * The queue manager instance.
  16. *
  17. * @var \Illuminate\Queue\QueueManager
  18. */
  19. protected $manager;
  20. /**
  21. * Create a new queue capsule manager.
  22. *
  23. * @param \Illuminate\Container\Container $container
  24. * @return void
  25. */
  26. public function __construct(Container $container = null)
  27. {
  28. $this->setupContainer($container ?: new Container);
  29. // Once we have the container setup, we will setup the default configuration
  30. // options in the container "config" bindings. This just makes this queue
  31. // manager behave correctly since all the correct binding are in place.
  32. $this->setupDefaultConfiguration();
  33. $this->setupManager();
  34. $this->registerConnectors();
  35. }
  36. /**
  37. * Setup the default queue configuration options.
  38. *
  39. * @return void
  40. */
  41. protected function setupDefaultConfiguration()
  42. {
  43. $this->container['config']['queue.default'] = 'default';
  44. }
  45. /**
  46. * Build the queue manager instance.
  47. *
  48. * @return void
  49. */
  50. protected function setupManager()
  51. {
  52. $this->manager = new QueueManager($this->container);
  53. }
  54. /**
  55. * Register the default connectors that the component ships with.
  56. *
  57. * @return void
  58. */
  59. protected function registerConnectors()
  60. {
  61. $provider = new QueueServiceProvider($this->container);
  62. $provider->registerConnectors($this->manager);
  63. }
  64. /**
  65. * Get a connection instance from the global manager.
  66. *
  67. * @param string $connection
  68. * @return \Illuminate\Contracts\Queue\Queue
  69. */
  70. public static function connection($connection = null)
  71. {
  72. return static::$instance->getConnection($connection);
  73. }
  74. /**
  75. * Push a new job onto the queue.
  76. *
  77. * @param string $job
  78. * @param mixed $data
  79. * @param string $queue
  80. * @param string $connection
  81. * @return mixed
  82. */
  83. public static function push($job, $data = '', $queue = null, $connection = null)
  84. {
  85. return static::$instance->connection($connection)->push($job, $data, $queue);
  86. }
  87. /**
  88. * Push a new an array of jobs onto the queue.
  89. *
  90. * @param array $jobs
  91. * @param mixed $data
  92. * @param string $queue
  93. * @param string $connection
  94. * @return mixed
  95. */
  96. public static function bulk($jobs, $data = '', $queue = null, $connection = null)
  97. {
  98. return static::$instance->connection($connection)->bulk($jobs, $data, $queue);
  99. }
  100. /**
  101. * Push a new job onto the queue after a delay.
  102. *
  103. * @param \DateTimeInterface|\DateInterval|int $delay
  104. * @param string $job
  105. * @param mixed $data
  106. * @param string $queue
  107. * @param string $connection
  108. * @return mixed
  109. */
  110. public static function later($delay, $job, $data = '', $queue = null, $connection = null)
  111. {
  112. return static::$instance->connection($connection)->later($delay, $job, $data, $queue);
  113. }
  114. /**
  115. * Get a registered connection instance.
  116. *
  117. * @param string $name
  118. * @return \Illuminate\Contracts\Queue\Queue
  119. */
  120. public function getConnection($name = null)
  121. {
  122. return $this->manager->connection($name);
  123. }
  124. /**
  125. * Register a connection with the manager.
  126. *
  127. * @param array $config
  128. * @param string $name
  129. * @return void
  130. */
  131. public function addConnection(array $config, $name = 'default')
  132. {
  133. $this->container['config']["queue.connections.{$name}"] = $config;
  134. }
  135. /**
  136. * Get the queue manager instance.
  137. *
  138. * @return \Illuminate\Queue\QueueManager
  139. */
  140. public function getQueueManager()
  141. {
  142. return $this->manager;
  143. }
  144. /**
  145. * Pass dynamic instance methods to the manager.
  146. *
  147. * @param string $method
  148. * @param array $parameters
  149. * @return mixed
  150. */
  151. public function __call($method, $parameters)
  152. {
  153. return $this->manager->$method(...$parameters);
  154. }
  155. /**
  156. * Dynamically pass methods to the default connection.
  157. *
  158. * @param string $method
  159. * @param array $parameters
  160. * @return mixed
  161. */
  162. public static function __callStatic($method, $parameters)
  163. {
  164. return static::connection()->$method(...$parameters);
  165. }
  166. }