3 namespace Drupal\migrate_plus\Plugin\migrate_plus\data_parser;
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate_plus\DataParserPluginBase;
10 * Obtain SOAP data for migration.
14 * title = @Translation("SOAP")
17 class Soap extends DataParserPluginBase implements ContainerFactoryPluginInterface {
20 * Iterator over the SOAP data.
27 * Method to call on the SOAP service.
34 * Parameters to pass to the SOAP service function.
38 protected $parameters;
41 * Form of the function response - 'xml', 'object', or 'array'.
45 protected $responseType;
50 public function __construct(array $configuration, $plugin_id, $plugin_definition) {
51 parent::__construct($configuration, $plugin_id, $plugin_definition);
52 $this->function = $configuration['function'];
53 $this->parameters = $configuration['parameters'];
54 $this->responseType = $configuration['response_type'];
61 * If there's an error in a SOAP call.
62 * @throws \Drupal\migrate\MigrateException
63 * If we can't resolve the SOAP function or its response property.
65 protected function openSourceUrl($url) {
66 // Will throw SoapFault if there's
67 $client = new \SoapClient($url);
68 // Determine the response property name.
69 $function_found = FALSE;
70 foreach ($client->__getFunctions() as $function_signature) {
71 // E.g., "GetWeatherResponse GetWeather(GetWeather $parameters)".
72 $response_type = strtok($function_signature, ' ');
73 $function_name = strtok('(');
74 if (strcasecmp($function_name, $this->function) === 0) {
75 $function_found = TRUE;
76 foreach ($client->__getTypes() as $type_info) {
77 // E.g., "struct GetWeatherResponse {\n string GetWeatherResult;\n}".
78 if (preg_match('|struct (.*?) {\s*[a-z]+ (.*?);|is', $type_info, $matches)) {
79 if ($matches[1] == $response_type) {
80 $response_property = $matches[2];
87 if (!$function_found) {
88 throw new MigrateException("SOAP function {$this->function} not found.");
90 elseif (!isset($response_property)) {
91 throw new MigrateException("Response property not found for SOAP function {$this->function}.");
93 $response = $client->{$this->function}($this->parameters);
94 $response_value = $response->$response_property;
95 switch ($this->responseType) {
97 $xml = simplexml_load_string($response_value);
98 $this->iterator = new \ArrayIterator($xml->xpath($this->itemSelector));
101 $this->iterator = new \ArrayIterator($response_value->{$this->itemSelector});
104 $this->iterator = new \ArrayIterator($response_value[$this->itemSelector]);
113 protected function fetchNextRow() {
114 $current = $this->iterator->current();
116 foreach ($this->fieldSelectors() as $field_name => $selector) {
117 $this->currentItem[$field_name] = $current->$selector;
119 $this->iterator->next();