You are here

class InvalidationsService in Purge 8.3

Provides a service that instantiates invalidation objects on-demand.

Hierarchy

Expanded class hierarchy of InvalidationsService

1 string reference to 'InvalidationsService'
purge.services.yml in ./purge.services.yml
purge.services.yml
1 service uses InvalidationsService
purge.invalidation.factory in ./purge.services.yml
Drupal\purge\Plugin\Purge\Invalidation\InvalidationsService

File

src/Plugin/Purge/Invalidation/InvalidationsService.php, line 14

Namespace

Drupal\purge\Plugin\Purge\Invalidation
View source
class InvalidationsService extends ServiceBase implements InvalidationsServiceInterface {

  /**
   * Instance counter.
   *
   * Incremental ID counter for handing out unique instance IDs.
   *
   * @var int
   */
  protected $instanceCounter = 0;

  /**
   * Immutable instance counter.
   *
   * As immutable instances cannot change the queue, they are counted negative
   * and the counter only decrements. Its IDs can never clash with real ones.
   *
   * @var int
   */
  protected $instanceCounterImmutables = -1;

  /**
   * The 'purge.purgers' service.
   *
   * @var \Drupal\purge\Plugin\Purge\Purger\PurgersServiceInterface
   */
  protected $purgePurgers;

  /**
   * Construct the invalidation service.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $pluginManager
   *   The plugin manager for this service.
   * @param \Drupal\purge\Plugin\Purge\Purger\PurgersServiceInterface $purge_purgers
   *   The purgers service.
   */
  public function __construct(PluginManagerInterface $pluginManager, PurgersServiceInterface $purge_purgers) {
    $this->pluginManager = $pluginManager;
    $this->purgePurgers = $purge_purgers;
  }

  /**
   * Retrieve a new instance from the plugin manager.
   *
   * @param string $plugin_id
   *   The id of the invalidation type being instantiated.
   * @param mixed|null $expression
   *   Value - usually string - that describes the kind of invalidation, NULL
   *   when the type of invalidation doesn't require $expression. Types usually
   *   validate the given expression and throw exceptions for bad input.
   * @param int $id
   *   The numeric identifier of this instance.
   *
   * @return \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface
   *   The invalidation object.
   */
  protected function createInstance($plugin_id, $expression, $id) {
    return $this->pluginManager
      ->createInstance($plugin_id, [
      'expression' => $expression,
      'id' => $id,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function get($plugin_id, $expression = NULL) {
    if (!in_array($plugin_id, $this->purgePurgers
      ->getTypes())) {
      throw new TypeUnsupportedException($plugin_id);
    }
    $id = $this->instanceCounter++;
    return $this
      ->createInstance($plugin_id, $expression, $id);
  }

  /**
   * {@inheritdoc}
   */
  public function getImmutable($plugin_id, $expression = NULL) {
    $id = $this->instanceCounterImmutables--;
    return new ImmutableInvalidation($this
      ->createInstance($plugin_id, $expression, $id));
  }

  /**
   * {@inheritdoc}
   */
  public function getFromQueueData($item_data) {
    $instance = $this
      ->createInstance($item_data[ProxyItemInterface::DATA_INDEX_TYPE], $item_data[ProxyItemInterface::DATA_INDEX_EXPRESSION], $this->instanceCounter++);

    // Replay stored purger states.
    if (isset($item_data[ProxyItemInterface::DATA_INDEX_STATES])) {
      foreach ($item_data[ProxyItemInterface::DATA_INDEX_STATES] as $id => $state) {
        $instance
          ->setStateContext($id);
        $instance
          ->setState($state);
      }
      $instance
        ->setStateContext(NULL);
    }

    // Replay stored properties.
    if (isset($item_data[ProxyItemInterface::DATA_INDEX_PROPERTIES])) {
      foreach ($item_data[ProxyItemInterface::DATA_INDEX_PROPERTIES] as $id => $properties) {
        $instance
          ->setStateContext($id);
        foreach ($properties as $key => $value) {
          $instance
            ->setProperty($key, $value);
        }
      }
      $instance
        ->setStateContext(NULL);
    }
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function getImmutableFromQueueData($item_data) {
    $instance = $this
      ->createInstance($item_data[ProxyItemInterface::DATA_INDEX_TYPE], $item_data[ProxyItemInterface::DATA_INDEX_EXPRESSION], $this->instanceCounterImmutables--);

    // Replay stored purger states.
    if (isset($item_data[ProxyItemInterface::DATA_INDEX_STATES])) {
      foreach ($item_data[ProxyItemInterface::DATA_INDEX_STATES] as $id => $state) {
        $instance
          ->setStateContext($id);
        $instance
          ->setState($state);
      }
      $instance
        ->setStateContext(NULL);
    }

    // Replay stored properties.
    if (isset($item_data[ProxyItemInterface::DATA_INDEX_PROPERTIES])) {
      foreach ($item_data[ProxyItemInterface::DATA_INDEX_PROPERTIES] as $id => $properties) {
        $instance
          ->setStateContext($id);
        foreach ($properties as $key => $value) {
          $instance
            ->setProperty($key, $value);
        }
      }
      $instance
        ->setStateContext(NULL);
    }
    return new ImmutableInvalidation($instance);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InvalidationsService::$instanceCounter protected property Instance counter.
InvalidationsService::$instanceCounterImmutables protected property Immutable instance counter.
InvalidationsService::$purgePurgers protected property The 'purge.purgers' service.
InvalidationsService::createInstance protected function Retrieve a new instance from the plugin manager.
InvalidationsService::get public function Create a new invalidation object of the given type. Overrides InvalidationsServiceInterface::get
InvalidationsService::getFromQueueData public function Replicate a invalidation object from serialized queue item data. Overrides InvalidationsServiceInterface::getFromQueueData
InvalidationsService::getImmutable public function Create a new immutable invalidation object of the given type. Overrides InvalidationsServiceInterface::getImmutable
InvalidationsService::getImmutableFromQueueData public function Replicate a immutable invalidation object from serialized queue item data. Overrides InvalidationsServiceInterface::getImmutableFromQueueData
InvalidationsService::__construct public function Construct the invalidation service.
ServiceBase::$pluginManager protected property The plugin manager for the given service.
ServiceBase::$plugins protected property The list of all available plugins and their definitions.
ServiceBase::$pluginsEnabled protected property The list of all enabled plugins and their definitions.
ServiceBase::getPlugins public function Retrieve a list of all available plugins providing the service. Overrides ServiceInterface::getPlugins 1
ServiceBase::getPluginsEnabled public function Retrieve the configured plugin_ids that the service will use. Overrides ServiceInterface::getPluginsEnabled 6
ServiceBase::isPluginEnabled public function Find out whether the given plugin_id is enabled. Overrides ServiceInterface::isPluginEnabled
ServiceBase::reload public function Reload the service and reinstantiate all enabled plugins. Overrides ServiceInterface::reload 6
ServiceProviderBase::alter public function Modifies existing service definitions. Overrides ServiceModifierInterface::alter 5
ServiceProviderBase::register public function Registers services to the container. Overrides ServiceProviderInterface::register 1