QueueFactory.php 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\Core\Queue\QueueFactory.
 */

namespace Drupal\Core\Queue;

10
use Drupal\Core\Site\Settings;
11 12
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
13 14 15 16

/**
 * Defines the queue factory.
 */
17 18 19
class QueueFactory implements ContainerAwareInterface {

  use ContainerAwareTrait;
20 21 22 23 24 25 26 27 28 29 30

  /**
   * Instantiated queues, keyed by name.
   *
   * @var array
   */
  protected $queues = array();

  /**
   * The settings object.
   *
31
   * @var \Drupal\Core\Site\Settings
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
   */
  protected $settings;


  /**
   * Constructs a queue factory.
   */
  function __construct(Settings $settings) {
    $this->settings = $settings;
  }

  /**
   * Constructs a new queue.
   *
   * @param string $name
   *   The name of the queue to work with.
   * @param bool $reliable
   *   (optional) TRUE if the ordering of items and guaranteeing every item executes at
   *   least once is important, FALSE if scalability is the main concern. Defaults
   *   to FALSE.
   *
53
   * @return \Drupal\Core\Queue\QueueInterface
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
   *   A queue implementation for the given name.
   */
  public function get($name, $reliable = FALSE) {
    if (!isset($this->queues[$name])) {
      // If it is a reliable queue, check the specific settings first.
      if ($reliable) {
        $service_name = $this->settings->get('queue_reliable_service_' . $name);
      }
      // If no reliable queue was defined, check the service and global
      // settings, fall back to queue.database.
      if (empty($service_name)) {
        $service_name = $this->settings->get('queue_service_' . $name, $this->settings->get('queue_default', 'queue.database'));
      }
      $this->queues[$name] = $this->container->get($service_name)->get($name);
    }
    return $this->queues[$name];
  }
}