MigrateTemplateStorage.php in Migrate Manifest 3.x
File
src/MigrateTemplateStorage.php
View source
<?php
namespace Drupal\migrate_manifest;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Extension\ModuleHandlerInterface;
class MigrateTemplateStorage implements MigrateTemplateStorageInterface {
const MIGRATION_TEMPLATE_DIRECTORY = 'migrations';
protected $directory;
protected $moduleHandler;
public function __construct(ModuleHandlerInterface $module_handler, $directory = self::MIGRATION_TEMPLATE_DIRECTORY) {
$this->moduleHandler = $module_handler;
$this->directory = $directory;
}
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;
}
public function getTemplateByName($name) {
$templates = $this
->getAllTemplates();
return isset($templates[$name]) ? $templates[$name] : NULL;
}
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;
}
}