Queue.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use InvalidArgumentException;
  13. use think\cache\driver\Redis;
  14. use think\helper\Str;
  15. use think\queue\Connector;
  16. use think\queue\connector\Database;
  17. /**
  18. * Class Queue
  19. * @package think\queue
  20. *
  21. * @method Connector driver($driver = null)
  22. * @mixin Database
  23. * @mixin Redis
  24. */
  25. class Queue extends Factory
  26. {
  27. protected $namespace = '\\think\\queue\\connector\\';
  28. /**
  29. * Get the queue connector configuration.
  30. *
  31. * @param string $name
  32. * @return array
  33. */
  34. protected function getConfig($name)
  35. {
  36. return $this->app->config->get("queue.connections.{$name}", ['driver' => 'sync']);
  37. }
  38. protected function createDriver($name)
  39. {
  40. $driver = $this->getConfig($name)['driver'];
  41. $class = false !== strpos($driver, '\\') ? $driver : $this->namespace . Str::studly($driver);
  42. /** @var Connector $driver */
  43. if (class_exists($class)) {
  44. $driver = $this->app->invokeClass($class, [$this->getConfig($driver)]);
  45. return $driver->setApp($this->app)
  46. ->setConnection($name);
  47. }
  48. throw new InvalidArgumentException("Driver [$driver] not supported.");
  49. }
  50. /**
  51. * @param null|string $name
  52. * @return Connector
  53. */
  54. public function connection($name = null)
  55. {
  56. return $this->driver($name);
  57. }
  58. /**
  59. * 默认驱动
  60. * @return string
  61. */
  62. public function getDefaultDriver()
  63. {
  64. return $this->app->config->get('queue.default');
  65. }
  66. }