You are here

class AcquiaPurgeProcessorsService in Acquia Purge 7

Service that loads and bundles queue processor backends.

Hierarchy

Expanded class hierarchy of AcquiaPurgeProcessorsService

File

lib/processor/AcquiaPurgeProcessorsService.php, line 11
Contains AcquiaPurgeProcessorsService.

View source
class AcquiaPurgeProcessorsService {

  /**
   * All processors, ::isEnabled() is called to see if they should load.
   *
   * @var string[]
   */
  protected $backends = array(
    '_acquia_purge_processor_ajax',
    '_acquia_purge_processor_cron',
    '_acquia_purge_processor_runtime',
  );

  /**
   * The loaded backends.
   *
   * @var AcquiaPurgeProcessorInterface
   */
  protected $processors = array();

  /**
   * Event-processor mapping.
   *
   * @var AcquiaPurgeProcessorInterface[]
   */
  protected $events = array();

  /**
   * The Acquia Purge service object.
   *
   * @var AcquiaPurgeService
   */
  protected $service;

  /**
   * Construct a new AcquiaPurgeProcessorsService instance.
   *
   * @param AcquiaPurgeService $service
   *   The Acquia Purge service object.
   */
  public function __construct(AcquiaPurgeService $service) {
    $this->service = $service;
    _acquia_purge_load('_acquia_purge_processor_interface');
    _acquia_purge_load('_acquia_purge_processor_base');

    // Initialize the processors that advertize themselves as enabled.
    foreach ($this->backends as $service) {
      $class = _acquia_purge_load($service);
      if ($class::isEnabled()) {
        $this->processors[$class] = new $class($this->service);
      }
    }

    // Query the enabled processors for their subscribed events.
    foreach ($this->processors as $processor) {
      foreach ($processor
        ->getSubscribedEvents() as $event) {
        if (!isset($this->events[$event])) {
          $this->events[$event] = array();
        }
        $this->events[$event][] = $processor;
      }
    }
  }

  /**
   * Emit a particular event.
   *
   * @param string $event
   *   Name of the event, often derived from hook implementations found directly
   *   in acquia_purge.module, but could also be 'onItemsQueued'.
   * @param mixed $a1
   *   First optional parameter to pass by reference.
   * @param mixed $a2
   *   Second optional parameter to pass by reference.
   */
  public function emit($event, &$a1 = NULL, &$a2 = NULL) {

    // Don't emit when there are no subscribers.
    if (!isset($this->events[$event])) {
      return;
    }

    // Emit to subscribed processors and pass on any given parameters.
    if (isset($this->events[$event])) {
      foreach ($this->events[$event] as $processor) {
        if (is_null($a1)) {
          $processor
            ->{$event}();
        }
        elseif (is_null($a2)) {
          $processor
            ->{$event}($a1);
        }
        else {
          $processor
            ->{$event}($a1, $a2);
        }
      }
    }
  }

  /**
   * Retrieve a loaded processor object.
   *
   * @param string $class
   *   The name of the processor class you need.
   *
   * @return AcquiaPurgeProcessorInterface|false
   *   Returns the processor, or FALSE when it doesn't exist.
   */
  public function get($class) {
    if (!isset($this->processors[$class])) {
      return FALSE;
    }
    return $this->processors[$class];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiaPurgeProcessorsService::$backends protected property All processors, ::isEnabled() is called to see if they should load.
AcquiaPurgeProcessorsService::$events protected property Event-processor mapping.
AcquiaPurgeProcessorsService::$processors protected property The loaded backends.
AcquiaPurgeProcessorsService::$service protected property The Acquia Purge service object.
AcquiaPurgeProcessorsService::emit public function Emit a particular event.
AcquiaPurgeProcessorsService::get public function Retrieve a loaded processor object.
AcquiaPurgeProcessorsService::__construct public function Construct a new AcquiaPurgeProcessorsService instance.