Commit 5c2eaf00 authored by heddn's avatar heddn Committed by heddn

Issue #3035586 by heddn: Deprecated file_* services

parent abe81ddb
......@@ -5,16 +5,32 @@
* Install, update, and uninstall functions for migrate_json_example.
*/
use Drupal\Core\File\FileSystemInterface;
/**
* Copies the example file to the sites/default/files folder.
*/
function migrate_json_example_install() {
// Create the example file directory and ensure it's writable.
$directory = file_default_scheme() . '://migrate_json_example';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
// TODO: remove after 8.6 is no longer supported in
// https://www.drupal.org/project/migrate_plus/issues/3035587
if (version_compare(\Drupal::VERSION, '8.7', '>=')) {
\Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
}
else {
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
}
// Copy the example file to example directory.
$module_path = drupal_get_path('module', 'migrate_json_example');
$file_source = $module_path . '/artifacts/products.json';
file_unmanaged_copy($file_source, $directory . '/products.json', FILE_EXISTS_REPLACE);
// TODO: remove after 8.6 is no longer supported in
// https://www.drupal.org/project/migrate_plus/issues/3035587
if (version_compare(\Drupal::VERSION, '8.7', '>=')) {
\Drupal::service('file_system')->copy($file_source, $directory . '/products.json', FileSystemInterface::EXISTS_REPLACE);
}
else {
file_unmanaged_copy($file_source, $directory . '/products.json', FILE_EXISTS_REPLACE);
}
}
......@@ -78,7 +78,15 @@ class FileBlob extends ProcessPluginBase implements ContainerFactoryPluginInterf
return $destination;
}
$dir = $this->getDirectory($destination);
if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
// TODO: remove after 8.6 is no longer supported in
// https://www.drupal.org/project/migrate_plus/issues/3035587
if (version_compare(\Drupal::VERSION, '8.7', '>=')) {
$success = $this->fileSystem->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
if (!$success) {
throw new MigrateSkipProcessException("Could not create directory '$dir'");
}
}
elseif (file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
throw new MigrateSkipProcessException("Could not create directory '$dir'");
}
if ($this->putFile($destination, $blob, $replace)) {
......@@ -95,14 +103,26 @@ class FileBlob extends ProcessPluginBase implements ContainerFactoryPluginInterf
* @param string $blob
* The base64 encoded file contents.
* @param int $replace
* (optional) FILE_EXISTS_REPLACE (default) or FILE_EXISTS_ERROR, depending
* on the configuration.
* (optional) either FileSystemInterface::EXISTS_REPLACE; (default) or
* FileSystemInterface::EXISTS_ERROR, depending on the configuration.
*
* @return bool|string
* File path on success, FALSE on failure.
*/
protected function putFile($destination, $blob, $replace = FILE_EXISTS_REPLACE) {
if ($path = file_destination($destination, $replace)) {
protected function putFile($destination, $blob, $replace = FileSystemInterface::EXISTS_REPLACE) {
// TODO: remove after 8.6 is no longer supported in
// https://www.drupal.org/project/migrate_plus/issues/3035587
if (!isset($replace)) {
$replace = FILE_EXISTS_REPLACE;
}
if (version_compare(\Drupal::VERSION, '8.7', '>=')) {
$path = $this->fileSystem->getDestinationFilename($destination, $replace);
}
else {
$path = file_destination($destination, $replace);
}
if ($path) {
if (file_put_contents($path, $blob)) {
return $path;
}
......@@ -119,15 +139,28 @@ class FileBlob extends ProcessPluginBase implements ContainerFactoryPluginInterf
* Determines how to handle file conflicts.
*
* @return int
* Either FILE_EXISTS_REPLACE (default) or FILE_EXISTS_ERROR, depending on
* the configuration.
* Either FileSystemInterface::EXISTS_REPLACE; (default) or
* FileSystemInterface::EXISTS_ERROR, depending on the configuration.
*/
protected function getOverwriteMode() {
if (!empty($this->configuration['reuse'])) {
return FILE_EXISTS_ERROR;
// TODO: remove after 8.6 is no longer supported in
// https://www.drupal.org/project/migrate_plus/issues/3035587
if (isset($this->configuration['reuse']) && !empty($this->configuration['reuse'])) {
if (version_compare(\Drupal::VERSION, '8.7', '>=')) {
return FileSystemInterface::EXISTS_ERROR;
}
else {
return FILE_EXISTS_ERROR;
}
}
return FILE_EXISTS_REPLACE;
if (version_compare(\Drupal::VERSION, '8.7', '>=')) {
return FileSystemInterface::EXISTS_REPLACE;
}
else {
return FILE_EXISTS_REPLACE;
}
}
/**
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment