FALSE, 'guzzle_options' => [], ]; parent::__construct($configuration, $plugin_id, $plugin_definition); $this->fileSystem = $file_system; $this->httpClient = $http_client; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('file_system'), $container->get('http_client') ); } /** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { // If we're stubbing a file entity, return a uri of NULL so it will get // stubbed by the general process. if ($row->isStub()) { return NULL; } list($source, $destination) = $value; // Modify the destination filename if necessary. $replace = !empty($this->configuration['rename']) ? FILE_EXISTS_RENAME : FILE_EXISTS_REPLACE; $final_destination = file_destination($destination, $replace); // Try opening the file first, to avoid calling file_prepare_directory() // unnecessarily. We're suppressing fopen() errors because we want to try // to prepare the directory before we give up and fail. $destination_stream = @fopen($final_destination, 'w'); if (!$destination_stream) { // If fopen didn't work, make sure there's a writable directory in place. $dir = $this->fileSystem->dirname($final_destination); if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { throw new MigrateException("Could not create or write to directory '$dir'"); } // Let's try that fopen again. $destination_stream = @fopen($final_destination, 'w'); if (!$destination_stream) { throw new MigrateException("Could not write to file '$final_destination'"); } } // Stream the request body directly to the final destination stream. $this->configuration['guzzle_options']['sink'] = $destination_stream; try { // Make the request. Guzzle throws an exception for anything but 200. $this->httpClient->get($source, $this->configuration['guzzle_options']); } catch (\Exception $e) { throw new MigrateException("{$e->getMessage()} ($source)"); } return $final_destination; } }