Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 31
MigrateTemplateStorage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 31
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 findTemplatesByTag
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 11
 getTemplateByName
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 getAllTemplates
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 14
<?php
/**
 * @file
 * Contains \Drupal\migrate\MigrateTemplateStorage.
 */
namespace Drupal\migrate;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
 * Storage to access migration template configuration in enabled extensions.
 */
class MigrateTemplateStorage implements MigrateTemplateStorageInterface {
  /**
   * 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;
  }
  /**
   * {@inheritdoc}
   */
  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;
  }
  /**
   * {@inheritdoc}
   */
  public function getTemplateByName($name) {
    $templates = $this->getAllTemplates();
    return isset($templates[$name]) ? $templates[$name] : NULL;
  }
  /**
   * {@inheritdoc}
   */
  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] !== '.' && preg_match('/\.yml$/', $file)) {
            $templates[basename($file, '.yml')] = Yaml::decode(file_get_contents("$full_directory/$file"));
          }
        }
      }
    }
    return $templates;
  }
}