annotate core/modules/migrate_drupal/src/MigrationPluginManager.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 namespace Drupal\migrate_drupal;
Chris@14 4
Chris@14 5 use Drupal\Core\Cache\CacheBackendInterface;
Chris@14 6 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@14 7 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@14 8 use Drupal\Core\Language\LanguageManagerInterface;
Chris@14 9 use Drupal\migrate\Plugin\Exception\BadPluginDefinitionException;
Chris@14 10 use Drupal\migrate\Plugin\MigrateSourcePluginManager;
Chris@14 11 use Drupal\migrate\Plugin\MigrationPluginManager as BaseMigrationPluginManager;
Chris@14 12
Chris@14 13 /**
Chris@14 14 * Manages migration plugins.
Chris@14 15 *
Chris@14 16 * Analyzes migration definitions to ensure that the source plugin of any
Chris@14 17 * migration tagged with particular tags ('Drupal 6' or 'Drupal 7' by default)
Chris@14 18 * defines a source_module property in its plugin annotation. This is done in
Chris@14 19 * order to support the Migrate Drupal UI, which needs to know which modules
Chris@14 20 * "own" the data being migrated into Drupal 8, on both the source and
Chris@14 21 * destination sides.
Chris@14 22 *
Chris@14 23 * @todo Enforce the destination_module property too, in
Chris@14 24 * https://www.drupal.org/project/drupal/issues/2923810.
Chris@14 25 */
Chris@14 26 class MigrationPluginManager extends BaseMigrationPluginManager {
Chris@14 27
Chris@14 28 /**
Chris@14 29 * The Migrate source plugin manager service.
Chris@14 30 *
Chris@14 31 * @var \Drupal\migrate\Plugin\MigrateSourcePluginManager
Chris@14 32 */
Chris@14 33 protected $sourceManager;
Chris@14 34
Chris@14 35 /**
Chris@14 36 * The config factory service.
Chris@14 37 *
Chris@14 38 * @var \Drupal\Core\Config\ConfigFactoryInterface
Chris@14 39 */
Chris@14 40 protected $configFactory;
Chris@14 41
Chris@14 42 /**
Chris@14 43 * The migration tags which will trigger source_module enforcement.
Chris@14 44 *
Chris@14 45 * @var string[]
Chris@14 46 */
Chris@14 47 protected $enforcedSourceModuleTags;
Chris@14 48
Chris@14 49 /**
Chris@14 50 * MigrationPluginManager constructor.
Chris@14 51 *
Chris@14 52 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@14 53 * The module handler service.
Chris@14 54 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
Chris@14 55 * The cache backend.
Chris@14 56 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@14 57 * The language manager service.
Chris@14 58 * @param \Drupal\migrate\Plugin\MigrateSourcePluginManager $source_manager
Chris@14 59 * The Migrate source plugin manager service.
Chris@14 60 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@14 61 * The config factory service.
Chris@14 62 */
Chris@14 63 public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, MigrateSourcePluginManager $source_manager, ConfigFactoryInterface $config_factory) {
Chris@14 64 parent::__construct($module_handler, $cache_backend, $language_manager);
Chris@14 65 $this->sourceManager = $source_manager;
Chris@14 66 $this->configFactory = $config_factory;
Chris@14 67 }
Chris@14 68
Chris@14 69 /**
Chris@14 70 * Returns the migration tags that trigger source_module enforcement.
Chris@14 71 *
Chris@14 72 * @return string[]
Chris@14 73 */
Chris@14 74 protected function getEnforcedSourceModuleTags() {
Chris@14 75 if ($this->enforcedSourceModuleTags === NULL) {
Chris@14 76 $this->enforcedSourceModuleTags = $this->configFactory
Chris@14 77 ->get('migrate_drupal.settings')
Chris@14 78 ->get('enforce_source_module_tags') ?: [];
Chris@14 79 }
Chris@14 80 return $this->enforcedSourceModuleTags;
Chris@14 81 }
Chris@14 82
Chris@14 83 /**
Chris@14 84 * {@inheritdoc}
Chris@14 85 */
Chris@14 86 public function processDefinition(&$definition, $plugin_id) {
Chris@14 87 parent::processDefinition($definition, $plugin_id);
Chris@14 88
Chris@14 89 // If the migration has no tags, we don't need to enforce the source_module
Chris@14 90 // annotation property.
Chris@14 91 if (empty($definition['migration_tags'])) {
Chris@14 92 return;
Chris@14 93 }
Chris@14 94
Chris@14 95 // Check if the migration has any of the tags that trigger source_module
Chris@14 96 // enforcement.
Chris@14 97 $applied_tags = array_intersect($this->getEnforcedSourceModuleTags(), $definition['migration_tags']);
Chris@14 98 if ($applied_tags) {
Chris@14 99 // Throw an exception if the source plugin definition does not define a
Chris@14 100 // source_module.
Chris@14 101 $source_id = $definition['source']['plugin'];
Chris@14 102 $source_definition = $this->sourceManager->getDefinition($source_id);
Chris@14 103 if (empty($source_definition['source_module'])) {
Chris@14 104 throw new BadPluginDefinitionException($source_id, 'source_module');
Chris@14 105 }
Chris@14 106 }
Chris@14 107 }
Chris@14 108
Chris@14 109 }