You are here

public function SourcePluginBase::count in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/migrate/src/Plugin/migrate/source/SourcePluginBase.php \Drupal\migrate\Plugin\migrate\source\SourcePluginBase::count()

Get the source count.

Return a count of available source records, from the cache if appropriate. Returns -1 if the source is not countable.

Parameters

bool $refresh: Whether or not to refresh the count.

Return value

int The count.

3 methods override SourcePluginBase::count()
EmbeddedDataSource::count in core/modules/migrate/src/Plugin/migrate/source/EmbeddedDataSource.php
Get the source count.
EmptySource::count in core/modules/migrate/src/Plugin/migrate/source/EmptySource.php
Get the source count.
SqlBase::count in core/modules/migrate/src/Plugin/migrate/source/SqlBase.php
Get the source count.

File

core/modules/migrate/src/Plugin/migrate/source/SourcePluginBase.php, line 365
Contains \Drupal\migrate\Plugin\migrate\source\SourcePluginBase.

Class

SourcePluginBase
The base class for all source plugins.

Namespace

Drupal\migrate\Plugin\migrate\source

Code

public function count($refresh = FALSE) {
  if ($this->skipCount) {
    return -1;
  }
  if (!isset($this->cacheKey)) {
    $this->cacheKey = hash('sha256', $this
      ->getPluginId());
  }

  // If a refresh is requested, or we're not caching counts, ask the derived
  // class to get the count from the source.
  if ($refresh || !$this->cacheCounts) {
    $count = $this
      ->getIterator()
      ->count();
    $this
      ->getCache()
      ->set($this->cacheKey, $count);
  }
  else {

    // Caching is in play, first try to retrieve a cached count.
    $cache_object = $this
      ->getCache()
      ->get($this->cacheKey, 'cache');
    if (is_object($cache_object)) {

      // Success.
      $count = $cache_object->data;
    }
    else {

      // No cached count, ask the derived class to count 'em up, and cache
      // the result.
      $count = $this
        ->getIterator()
        ->count();
      $this
        ->getCache()
        ->set($this->cacheKey, $count);
    }
  }
  return $count;
}