You are here

class Changes in Replication 8.2

Same name and namespace in other branches
  1. 8 src/Changes/Changes.php \Drupal\replication\Changes\Changes

Hierarchy

Expanded class hierarchy of Changes

1 file declares its use of Changes
ChangesFactory.php in src/ChangesFactory.php

File

src/Changes/Changes.php, line 15

Namespace

Drupal\replication\Changes
View source
class Changes implements ChangesInterface {
  use DependencySerializationTrait;

  /**
   * The sequence index.
   *
   * @var \Drupal\multiversion\Entity\Index\SequenceIndexInterface
   */
  protected $sequenceIndex;

  /**
   * The workspace to generate the changeset from.
   *
   * @var string
   */
  protected $workspaceId;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var \Symfony\Component\Serializer\SerializerInterface
   */
  protected $serializer;

  /**
   * @var \Drupal\replication\Plugin\ReplicationFilterManagerInterface
   */
  protected $filterManager;

  /**
   * @var string
   *   The id of the filter plugin to use during replication.
   */
  protected $filter;

  /**
   * The parameters passed to the filter plugin.
   *
   * @var array
   */
  protected $parameters;

  /**
   * Whether to include entities in the changeset.
   *
   * @var boolean
   */
  protected $includeDocs = FALSE;

  /**
   * The sequence ID to start including changes from. Result includes last_seq.
   *
   * @var int
   */
  protected $since = 0;

  /**
   * Number of items to return.
   *
   * @var int|NULL
   *   The limit of items.
   */
  protected $limit = NULL;

  /**
   * @param \Drupal\multiversion\Entity\Index\SequenceIndexInterface $sequence_index
   * @param \Drupal\multiversion\Entity\WorkspaceInterface $workspace
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Symfony\Component\Serializer\SerializerInterface $serializer
   * @param \Drupal\replication\Plugin\ReplicationFilterManagerInterface $filter_manager
   */
  public function __construct(SequenceIndexInterface $sequence_index, WorkspaceInterface $workspace, EntityTypeManagerInterface $entity_type_manager, SerializerInterface $serializer, ReplicationFilterManagerInterface $filter_manager) {
    $this->sequenceIndex = $sequence_index;
    $this->workspaceId = $workspace
      ->id();
    $this->entityTypeManager = $entity_type_manager;
    $this->serializer = $serializer;
    $this->filterManager = $filter_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function filter($filter) {
    $this->filter = $filter;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function parameters(array $parameters = NULL) {
    $this->parameters = $parameters;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function includeDocs($include_docs) {
    $this->includeDocs = $include_docs;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setSince($seq) {
    $this->since = $seq;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setLimit($limit) {
    $this->limit = $limit;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getNormal() {
    $sequences = $this->sequenceIndex
      ->useWorkspace($this->workspaceId)
      ->getRange($this->since, NULL);

    // When we have the since parameter set, we should return values starting
    // just after the since sequence (that is first in the $sequences array).
    if ($this->since > 0) {
      array_shift($sequences);
    }

    // Setup filter plugin.
    $parameters = is_array($this->parameters) ? $this->parameters : [];
    $filter = NULL;
    if (is_string($this->filter) && $this->filter) {
      $filter = $this->filterManager
        ->createInstance($this->filter, $parameters);
    }
    elseif (isset($parameters['uuids'])) {
      $filter = $this->filterManager
        ->createInstance('uuid', $parameters);
    }

    // Format the result array.
    $changes = [];
    $count = 0;
    foreach ($sequences as $sequence) {
      if (!empty($sequence['local']) || !empty($sequence['is_stub'])) {
        continue;
      }

      // Get the document.
      $revision = NULL;
      if ($this->includeDocs == TRUE || $filter !== NULL) {

        /** @var \Drupal\multiversion\Entity\Storage\ContentEntityStorageInterface $storage */
        $storage = $this->entityTypeManager
          ->getStorage($sequence['entity_type_id']);
        $storage
          ->useWorkspace($this->workspaceId);
        $revision = $storage
          ->loadRevision($sequence['revision_id']);
      }

      // Filter the document.
      if ($filter !== NULL && !$filter
        ->filter($revision)) {
        continue;
      }
      if ($this->limit && $count >= $this->limit) {
        break;
      }
      $uuid = $sequence['entity_uuid'];
      if (!isset($changes[$uuid])) {
        $count++;
      }
      $changes[$uuid] = [
        'changes' => [
          [
            'rev' => $sequence['rev'],
          ],
        ],
        'id' => $uuid,
        'seq' => $sequence['seq'],
      ];
      if ($sequence['deleted']) {
        $changes[$uuid]['deleted'] = TRUE;
      }

      // Include the document.
      if ($this->includeDocs == TRUE) {
        $changes[$uuid]['doc'] = $this->serializer
          ->normalize($revision);
      }
    }

    // Now when we have rebuilt the result array we need to ensure that the
    // results array is still sorted on the sequence key, as in the index.
    $return = array_values($changes);
    usort($return, function ($a, $b) {
      return $a['seq'] - $b['seq'];
    });
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function getLongpoll() {
    $no_change = TRUE;
    do {
      $change = $this->sequenceIndex
        ->useWorkspace($this->workspaceId)
        ->getRange($this->since, NULL);
      $no_change = empty($change) ? TRUE : FALSE;
    } while ($no_change);
    return $change;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Changes::$entityTypeManager protected property
Changes::$filter protected property The id of the filter plugin to use during replication.
Changes::$filterManager protected property
Changes::$includeDocs protected property Whether to include entities in the changeset.
Changes::$limit protected property Number of items to return.
Changes::$parameters protected property The parameters passed to the filter plugin.
Changes::$sequenceIndex protected property The sequence index.
Changes::$serializer protected property
Changes::$since protected property The sequence ID to start including changes from. Result includes last_seq.
Changes::$workspaceId protected property The workspace to generate the changeset from.
Changes::filter public function Set the ID of the filter plugin to use to refine the changeset. Overrides ChangesInterface::filter
Changes::getLongpoll public function Return the changes with a 'longpoll'. Overrides ChangesInterface::getLongpoll
Changes::getNormal public function Return the changes in a 'normal' way. Overrides ChangesInterface::getNormal
Changes::includeDocs public function Set the flag for including entities in the changeset. Overrides ChangesInterface::includeDocs
Changes::parameters public function Set the parameters for the filter plugin. Overrides ChangesInterface::parameters
Changes::setLimit public function Set the limit of returned number of items. Overrides ChangesInterface::setLimit
Changes::setSince public function Sets from what sequence number to check for changes. Overrides ChangesInterface::setSince
Changes::__construct public function
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2