QueueFactory.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Drupal\Core\Queue;
  3. use Drupal\Core\Site\Settings;
  4. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  5. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  6. /**
  7. * Defines the queue factory.
  8. */
  9. class QueueFactory implements ContainerAwareInterface {
  10. use ContainerAwareTrait;
  11. /**
  12. * Instantiated queues, keyed by name.
  13. *
  14. * @var array
  15. */
  16. protected $queues = [];
  17. /**
  18. * The settings object.
  19. *
  20. * @var \Drupal\Core\Site\Settings
  21. */
  22. protected $settings;
  23. /**
  24. * Constructs a queue factory.
  25. */
  26. public function __construct(Settings $settings) {
  27. $this->settings = $settings;
  28. }
  29. /**
  30. * Constructs a new queue.
  31. *
  32. * @param string $name
  33. * The name of the queue to work with.
  34. * @param bool $reliable
  35. * (optional) TRUE if the ordering of items and guaranteeing every item executes at
  36. * least once is important, FALSE if scalability is the main concern. Defaults
  37. * to FALSE.
  38. *
  39. * @return \Drupal\Core\Queue\QueueInterface
  40. * A queue implementation for the given name.
  41. */
  42. public function get($name, $reliable = FALSE) {
  43. if (!isset($this->queues[$name])) {
  44. // If it is a reliable queue, check the specific settings first.
  45. if ($reliable) {
  46. $service_name = $this->settings->get('queue_reliable_service_' . $name);
  47. }
  48. // If no reliable queue was defined, check the service and global
  49. // settings, fall back to queue.database.
  50. if (empty($service_name)) {
  51. $service_name = $this->settings->get('queue_service_' . $name, $this->settings->get('queue_default', 'queue.database'));
  52. }
  53. $this->queues[$name] = $this->container->get($service_name)->get($name);
  54. }
  55. return $this->queues[$name];
  56. }
  57. }