PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Queue/QueueServiceProvider.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 305 lines | 161 code | 40 blank | 104 comment | 1 complexity | 96c09dca938d2d03b5f99094546690ad MD5 | raw file
  1. <?php namespace Illuminate\Queue;
  2. use IlluminateQueueClosure;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Queue\Console\WorkCommand;
  5. use Illuminate\Queue\Console\ListenCommand;
  6. use Illuminate\Queue\Console\RestartCommand;
  7. use Illuminate\Queue\Connectors\SqsConnector;
  8. use Illuminate\Queue\Console\SubscribeCommand;
  9. use Illuminate\Queue\Connectors\SyncConnector;
  10. use Illuminate\Queue\Connectors\IronConnector;
  11. use Illuminate\Queue\Connectors\RedisConnector;
  12. use Illuminate\Queue\Connectors\BeanstalkdConnector;
  13. use Illuminate\Queue\Failed\DatabaseFailedJobProvider;
  14. class QueueServiceProvider extends ServiceProvider {
  15. /**
  16. * Indicates if loading of the provider is deferred.
  17. *
  18. * @var bool
  19. */
  20. protected $defer = true;
  21. /**
  22. * Register the service provider.
  23. *
  24. * @return void
  25. */
  26. public function register()
  27. {
  28. $this->registerManager();
  29. $this->registerWorker();
  30. $this->registerListener();
  31. $this->registerSubscriber();
  32. $this->registerFailedJobServices();
  33. $this->registerQueueClosure();
  34. }
  35. /**
  36. * Register the queue manager.
  37. *
  38. * @return void
  39. */
  40. protected function registerManager()
  41. {
  42. $this->app->bindShared('queue', function($app)
  43. {
  44. // Once we have an instance of the queue manager, we will register the various
  45. // resolvers for the queue connectors. These connectors are responsible for
  46. // creating the classes that accept queue configs and instantiate queues.
  47. $manager = new QueueManager($app);
  48. $this->registerConnectors($manager);
  49. return $manager;
  50. });
  51. }
  52. /**
  53. * Register the queue worker.
  54. *
  55. * @return void
  56. */
  57. protected function registerWorker()
  58. {
  59. $this->registerWorkCommand();
  60. $this->registerRestartCommand();
  61. $this->app->bindShared('queue.worker', function($app)
  62. {
  63. return new Worker($app['queue'], $app['queue.failer'], $app['events']);
  64. });
  65. }
  66. /**
  67. * Register the queue worker console command.
  68. *
  69. * @return void
  70. */
  71. protected function registerWorkCommand()
  72. {
  73. $this->app->bindShared('command.queue.work', function($app)
  74. {
  75. return new WorkCommand($app['queue.worker']);
  76. });
  77. $this->commands('command.queue.work');
  78. }
  79. /**
  80. * Register the queue listener.
  81. *
  82. * @return void
  83. */
  84. protected function registerListener()
  85. {
  86. $this->registerListenCommand();
  87. $this->app->bindShared('queue.listener', function($app)
  88. {
  89. return new Listener($app['path.base']);
  90. });
  91. }
  92. /**
  93. * Register the queue listener console command.
  94. *
  95. * @return void
  96. */
  97. protected function registerListenCommand()
  98. {
  99. $this->app->bindShared('command.queue.listen', function($app)
  100. {
  101. return new ListenCommand($app['queue.listener']);
  102. });
  103. $this->commands('command.queue.listen');
  104. }
  105. /**
  106. * Register the queue restart console command.
  107. *
  108. * @return void
  109. */
  110. public function registerRestartCommand()
  111. {
  112. $this->app->bindShared('command.queue.restart', function()
  113. {
  114. return new RestartCommand;
  115. });
  116. $this->commands('command.queue.restart');
  117. }
  118. /**
  119. * Register the push queue subscribe command.
  120. *
  121. * @return void
  122. */
  123. protected function registerSubscriber()
  124. {
  125. $this->app->bindShared('command.queue.subscribe', function()
  126. {
  127. return new SubscribeCommand;
  128. });
  129. $this->commands('command.queue.subscribe');
  130. }
  131. /**
  132. * Register the connectors on the queue manager.
  133. *
  134. * @param \Illuminate\Queue\QueueManager $manager
  135. * @return void
  136. */
  137. public function registerConnectors($manager)
  138. {
  139. foreach (array('Sync', 'Beanstalkd', 'Redis', 'Sqs', 'Iron') as $connector)
  140. {
  141. $this->{"register{$connector}Connector"}($manager);
  142. }
  143. }
  144. /**
  145. * Register the Sync queue connector.
  146. *
  147. * @param \Illuminate\Queue\QueueManager $manager
  148. * @return void
  149. */
  150. protected function registerSyncConnector($manager)
  151. {
  152. $manager->addConnector('sync', function()
  153. {
  154. return new SyncConnector;
  155. });
  156. }
  157. /**
  158. * Register the Beanstalkd queue connector.
  159. *
  160. * @param \Illuminate\Queue\QueueManager $manager
  161. * @return void
  162. */
  163. protected function registerBeanstalkdConnector($manager)
  164. {
  165. $manager->addConnector('beanstalkd', function()
  166. {
  167. return new BeanstalkdConnector;
  168. });
  169. }
  170. /**
  171. * Register the Redis queue connector.
  172. *
  173. * @param \Illuminate\Queue\QueueManager $manager
  174. * @return void
  175. */
  176. protected function registerRedisConnector($manager)
  177. {
  178. $app = $this->app;
  179. $manager->addConnector('redis', function() use ($app)
  180. {
  181. return new RedisConnector($app['redis']);
  182. });
  183. }
  184. /**
  185. * Register the Amazon SQS queue connector.
  186. *
  187. * @param \Illuminate\Queue\QueueManager $manager
  188. * @return void
  189. */
  190. protected function registerSqsConnector($manager)
  191. {
  192. $manager->addConnector('sqs', function()
  193. {
  194. return new SqsConnector;
  195. });
  196. }
  197. /**
  198. * Register the IronMQ queue connector.
  199. *
  200. * @param \Illuminate\Queue\QueueManager $manager
  201. * @return void
  202. */
  203. protected function registerIronConnector($manager)
  204. {
  205. $app = $this->app;
  206. $manager->addConnector('iron', function() use ($app)
  207. {
  208. return new IronConnector($app['encrypter'], $app['request']);
  209. });
  210. $this->registerIronRequestBinder();
  211. }
  212. /**
  213. * Register the request rebinding event for the Iron queue.
  214. *
  215. * @return void
  216. */
  217. protected function registerIronRequestBinder()
  218. {
  219. $this->app->rebinding('request', function($app, $request)
  220. {
  221. if ($app['queue']->connected('iron'))
  222. {
  223. $app['queue']->connection('iron')->setRequest($request);
  224. }
  225. });
  226. }
  227. /**
  228. * Register the failed job services.
  229. *
  230. * @return void
  231. */
  232. protected function registerFailedJobServices()
  233. {
  234. $this->app->bindShared('queue.failer', function($app)
  235. {
  236. $config = $app['config']['queue.failed'];
  237. return new DatabaseFailedJobProvider($app['db'], $config['database'], $config['table']);
  238. });
  239. }
  240. /**
  241. * Register the Illuminate queued closure job.
  242. *
  243. * @return void
  244. */
  245. protected function registerQueueClosure()
  246. {
  247. $this->app->bindShared('IlluminateQueueClosure', function($app)
  248. {
  249. return new IlluminateQueueClosure($app['encrypter']);
  250. });
  251. }
  252. /**
  253. * Get the services provided by the provider.
  254. *
  255. * @return array
  256. */
  257. public function provides()
  258. {
  259. return array(
  260. 'queue', 'queue.worker', 'queue.listener', 'queue.failer',
  261. 'command.queue.work', 'command.queue.listen', 'command.queue.restart',
  262. 'command.queue.subscribe',
  263. );
  264. }
  265. }