You are here

class MigrateTemplateStorage in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/migrate/src/MigrateTemplateStorage.php \Drupal\migrate\MigrateTemplateStorage

Storage to access migration template configuration in enabled extensions.

Hierarchy

Expanded class hierarchy of MigrateTemplateStorage

1 file declares its use of MigrateTemplateStorage
TermNode.php in core/modules/taxonomy/src/Plugin/migrate/builder/d6/TermNode.php
Contains \Drupal\taxonomy\Plugin\migrate\builder\d6\TermNode.
1 string reference to 'MigrateTemplateStorage'
migrate.services.yml in core/modules/migrate/migrate.services.yml
core/modules/migrate/migrate.services.yml
1 service uses MigrateTemplateStorage
migrate.template_storage in core/modules/migrate/migrate.services.yml
Drupal\migrate\MigrateTemplateStorage

File

core/modules/migrate/src/MigrateTemplateStorage.php, line 16
Contains \Drupal\migrate\MigrateTemplateStorage.

Namespace

Drupal\migrate
View source
class MigrateTemplateStorage {

  /**
   * Extension sub-directory containing default configuration for migrations.
   */
  const MIGRATION_TEMPLATE_DIRECTORY = 'migration_templates';

  /**
   * Template subdirectory.
   *
   * @var string
   */
  protected $directory;

  /**
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public function __construct(ModuleHandlerInterface $module_handler, $directory = self::MIGRATION_TEMPLATE_DIRECTORY) {
    $this->moduleHandler = $module_handler;
    $this->directory = $directory;
  }

  /**
   * Find all migration templates with the specified tag.
   *
   * @param $tag
   *   The tag to match.
   *
   * @return array
   *   Any templates (parsed YAML config) that matched, keyed by the ID.
   */
  public function findTemplatesByTag($tag) {
    $templates = $this
      ->getAllTemplates();
    $matched_templates = [];
    foreach ($templates as $template_name => $template) {
      if (!empty($template['migration_tags'])) {
        if (in_array($tag, $template['migration_tags'])) {
          $matched_templates[$template_name] = $template;
        }
      }
    }
    return $matched_templates;
  }

  /**
   * Retrieve a template given a specific name.
   *
   * @param string $name
   *   A migration template name.
   *
   * @return NULL|array
   *   A parsed migration template, or NULL if it doesn't exist.
   */
  public function getTemplateByName($name) {
    $templates = $this
      ->getAllTemplates();
    return isset($templates[$name]) ? $templates[$name] : NULL;
  }

  /**
   * Retrieves all migration templates belonging to enabled extensions.
   *
   * @return array
   *   Array of parsed templates, keyed by the fully-qualified id.
   */
  public function getAllTemplates() {
    $templates = [];
    foreach ($this->moduleHandler
      ->getModuleDirectories() as $directory) {
      $full_directory = $directory . '/' . $this->directory;
      if (file_exists($full_directory)) {
        $files = scandir($full_directory);
        foreach ($files as $file) {
          if ($file[0] !== '.' && fnmatch('*.yml', $file)) {
            $templates[basename($file, '.yml')] = Yaml::decode(file_get_contents("{$full_directory}/{$file}"));
          }
        }
      }
    }
    return $templates;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateTemplateStorage::$directory protected property Template subdirectory.
MigrateTemplateStorage::$moduleHandler protected property
MigrateTemplateStorage::findTemplatesByTag public function Find all migration templates with the specified tag.
MigrateTemplateStorage::getAllTemplates public function Retrieves all migration templates belonging to enabled extensions.
MigrateTemplateStorage::getTemplateByName public function Retrieve a template given a specific name.
MigrateTemplateStorage::MIGRATION_TEMPLATE_DIRECTORY constant Extension sub-directory containing default configuration for migrations.
MigrateTemplateStorage::__construct public function