You are here

class MigrateSourceYaml in Migrate Source YAML 7

Migrate source class to import from a YAML file.

Hierarchy

Expanded class hierarchy of MigrateSourceYaml

File

includes/MigrateSourceYaml.inc, line 13
Contains MigrateSourceYaml.

View source
class MigrateSourceYaml extends MigrateSource {

  /**
   * Construct a new YAML source object.
   *
   * @param string $path
   *   The system path to the YAML file.
   * @param string $list
   *   The name of the list to import from. This list must be at the base of the
   *   YAML file.
   * @param array $fields
   *   Optionally provide an array of fields where each key is the field name
   *   (as found in each item of the YAML list being imported) and the value
   *   being a description of the field. Use this to customise the description
   *   of any fields or to specify additional fields to be added to in
   *   prepareRow(). This may additionally be used where the first row of data
   *   doesn't contain all fields as this plugin only extracts names from the
   *   first row of data.
   * @param array $options
   *   Optionally provide options as described in MigrateSource.
   */
  public function __construct($path, $list, array $fields = array(), array $options = array()) {
    parent::__construct($options);
    $this->file = $path;
    $this->list = $list;
    $this->yamlParser = new Parser();
    $this
      ->parse();
    $this->fields = $fields;
    foreach (reset($this->data) as $key => $field) {
      if (!isset($this->fields[$key])) {
        $this->fields[$key] = ucfirst($key);
      }
    }
  }

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

  /**
   * Return a count of all available source records.
   *
   * @return int
   *   The number of source records.
   */
  public function computeCount() {
    return count($this->data);
  }

  /**
   * Perform a rewind, setting the current record back to the first item.
   *
   * Use PHP native array functionality, setting the internal pointer of the
   * data array to the first item.
   */
  public function performRewind() {
    reset($this->data);
  }

  /**
   * Get the next record.
   *
   * Use PHP native array functionality to return the item that the internal
   * array pointer is currently at and then advance the pointer to the next
   * record.
   *
   * @return NULL|object
   *   NULL if the last item has been reached otherwise an object representing
   *   the current row.
   */
  public function getNextRow() {
    if ($item = current($this->data)) {

      // Advance the internal pointer of the array.
      next($this->data);

      // Cast the item from an array into an StdClass object.
      return (object) $item;
    }
    return NULL;
  }

  /**
   * Parse the source YAML file.
   *
   * @throws Exception
   *   On not finding the provided list in the source data.
   */
  protected function parse() {
    $yaml = file_get_contents($this->file);
    $parsed = $this->yamlParser
      ->parse($yaml);
    if (isset($parsed[$this->list])) {
      $this->data = $parsed[$this->list];
    }
    else {
      throw new Exception('The list provided to MigrateSourceYaml does not exist in the YAML file specified.');
    }
  }

  /**
   * String representation of Migration Source.
   *
   * @return string
   *   String representation of the Migration Source file.
   */
  public function __toString() {
    return (string) $this->file;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateSource::$activeMap protected property The MigrateMap class for the current migration.
MigrateSource::$activeMigration protected property The Migration class currently invoking us, during rewind() and next().
MigrateSource::$cacheCounts protected property Whether this instance should cache the source count.
MigrateSource::$cacheKey protected property Key to use for caching counts.
MigrateSource::$currentKey protected property The primary key of the current row
MigrateSource::$currentRow protected property The current row from the quey
MigrateSource::$highwaterField protected property Information on the highwater mark for the current migration, if any.
MigrateSource::$idList protected property List of source IDs to process.
MigrateSource::$mapRowAdded protected property By default, next() will directly read the map row and add it to the data row. A source plugin implementation may do this itself (in particular, the SQL source can incorporate the map table into the query) - if so, it should set this TRUE so we…
MigrateSource::$multikeySeparator protected property Used in the case of multiple key sources that need to use idlist.
MigrateSource::$numIgnored protected property Number of rows intentionally ignored (prepareRow() returned FALSE)
MigrateSource::$numProcessed protected property Number of rows we've at least looked at. 1
MigrateSource::$originalHighwater protected property The highwater mark at the beginning of the import operation.
MigrateSource::$skipCount protected property Whether this instance should not attempt to count the source.
MigrateSource::$trackChanges protected property If TRUE, we will maintain hashed source rows to determine whether incoming data has changed.
MigrateSource::count public function Return a count of available source records, from the cache if appropriate. Returns -1 if the source is not countable.
MigrateSource::current public function Implementation of Iterator::current() - called when entering a loop iteration, returning the current row
MigrateSource::dataChanged protected function Determine whether this row has changed, and therefore whether it should be processed.
MigrateSource::getCurrentKey public function
MigrateSource::getIgnored public function
MigrateSource::getProcessed public function
MigrateSource::hash protected function Generate a hash of the source row. 3
MigrateSource::key public function Implementation of Iterator::key - called when entering a loop iteration, returning the key of the current row. It must be a scalar - we will serialize to fulfill the requirement, but using getCurrentKey() is preferable.
MigrateSource::next public function Implementation of Iterator::next() - subclasses of MigrateSource should implement getNextRow() to retrieve the next valid source rocord to process.
MigrateSource::prepareRow protected function Give the calling migration a shot at manipulating, and possibly rejecting, the source row.
MigrateSource::resetStats public function Reset numIgnored back to 0.
MigrateSource::rewind public function Implementation of Iterator::rewind() - subclasses of MigrateSource should implement performRewind() to do any class-specific setup for iterating source records.
MigrateSource::valid public function Implementation of Iterator::valid() - called at the top of the loop, returning TRUE to process the loop and FALSE to terminate it
MigrateSourceYaml::computeCount public function Return a count of all available source records.
MigrateSourceYaml::fields public function Derived classes must implement fields(), returning a list of available source fields. Overrides MigrateSource::fields
MigrateSourceYaml::getNextRow public function Get the next record.
MigrateSourceYaml::parse protected function Parse the source YAML file.
MigrateSourceYaml::performRewind public function Perform a rewind, setting the current record back to the first item.
MigrateSourceYaml::__construct public function Construct a new YAML source object. Overrides MigrateSource::__construct
MigrateSourceYaml::__toString public function String representation of Migration Source.