Skip to content

Instantly share code, notes, and snippets.

@sonnykt
Last active September 5, 2024 04:54
Show Gist options
  • Save sonnykt/f1d7dc624cc75889c494c705fd7313dd to your computer and use it in GitHub Desktop.
Save sonnykt/f1d7dc624cc75889c494c705fd7313dd to your computer and use it in GitHub Desktop.
Drupal 10 - Migration - Simulate migrate_plus HTTP fetcher plugin
$entity_type_manager = \Drupal::entityTypeManager();
$storage = $entity_type_manager->getStorage('migration');
$migration_plugin_manager = \Drupal::service('plugin.manager.migration');

$migration_id = '...';
$migration = $storage->load($migration_id);
$migration_plugin = $migration_plugin_manager->createInstance($migration_id, $migration->toArray());

$source_plugin = $migration_plugin->getSourcePlugin();
$source_plugin_id = $migration_plugin->getSourcePlugin()->getPluginId();

$data_parser = $source_plugin->getDataParserPlugin();
$data_fetcher = $data_parser->getDataFetcherPlugin();

// OR:
$data_parser = \Drupal::service('plugin.manager.migrate_plus.data_parser')->createInstance($migration->toArray()['source']['data_parser_plugin'], $migration->toArray()['source']);
$data_fetcher = \Drupal::service('plugin.manager.migrate_plus.data_fetcher')->createInstance($migration->toArray()['source']['data_fetcher_plugin'], $migration->toArray()['source']);

$http_client = \Drupal::httpClient();

$response = $data_fetcher->getResponse($source_plugin->currentUrl());
$response = $http_client->get($source_plugin->currentUrl());
$response = $response->getBody()->__toString();
// OR:
$response = $data_fetcher->getResponseContent($url);
$source_data = json_decode($response, TRUE);

$response->getStatusCode();
$response->getHeaders();

$length = $data_fetcher->getResponse($source_plugin->currentUrl())->getHeaders()['Content-Length'];

$length = $http_client->get($source_plugin->currentUrl())->getHeaders()['Content-Length'];
$length = $http_client->get($source_plugin->currentUrl(), ['headers' => []])->getHeaders()['Content-Length'];

Debug with ReflectionClass

$r = new ReflectionClass($data_parser);
$m = $r->getMethod('getSourceData');
$m->setAccessible(TRUE);
$source_data = $m->invoke($data_parser, $data_parser->currentUrl());
$iterator = new \ArrayIterator($source_data);

$r = new ReflectionClass($source_plugin);
$m = $r->getMethod('doCount');
$m->setAccessible(TRUE);
$m->invoke($source_plugin);

$r = new ReflectionClass($data_parser);
$m = $r->getMethod('openSourceUrl');
$m->setAccessible(TRUE);
$x = $m->invoke($data_parser, $data_parser->currentUrl());

$r = new ReflectionClass($source_plugin);
$m = $r->getMethod('getIterator');
$m->setAccessible(TRUE);
$x = $m->invoke($source_plugin);

$r = new ReflectionClass($data_parser);
$m = $r->getProperty('iterator');
$m->setAccessible(TRUE);
$iterator = $m->getValue($data_parser);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment