From 1d5e61916028be31c523b5a0a0dade6d36b8354d Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Mon, 7 Oct 2024 09:33:15 -0300 Subject: [PATCH 1/2] Avoid use of `\SplFileInfo()`. It can cause arbitrary warnings emitted in underlying stream wrappers to be detected and promoted to runtime exceptions, which is rather unexpected. Warnings are warnings and not fatal errors, after all. --- .../src/Plugin/migrate/process/FoxmlFile.php | 2 +- .../process/EnsureNonWritableTrait.php | 36 ++++++++++++++----- src/Plugin/migrate/process/NonWritable.php | 2 +- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/modules/dgi_migrate_foxml_standard_mods/src/Plugin/migrate/process/FoxmlFile.php b/modules/dgi_migrate_foxml_standard_mods/src/Plugin/migrate/process/FoxmlFile.php index 2bd412f..a3b16a6 100644 --- a/modules/dgi_migrate_foxml_standard_mods/src/Plugin/migrate/process/FoxmlFile.php +++ b/modules/dgi_migrate_foxml_standard_mods/src/Plugin/migrate/process/FoxmlFile.php @@ -94,7 +94,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable $row, $destination_property, ), - 'direct' => static::ensureNonWritable($value), + 'direct' => $this->ensureNonWritable($value), }; } diff --git a/src/Plugin/migrate/process/EnsureNonWritableTrait.php b/src/Plugin/migrate/process/EnsureNonWritableTrait.php index 28558cb..45b01ec 100644 --- a/src/Plugin/migrate/process/EnsureNonWritableTrait.php +++ b/src/Plugin/migrate/process/EnsureNonWritableTrait.php @@ -2,6 +2,7 @@ namespace Drupal\dgi_migrate\Plugin\migrate\process; +use Drupal\Core\File\FileSystemInterface; use Drupal\migrate\MigrateSkipRowException; /** @@ -27,30 +28,28 @@ trait EnsureNonWritableTrait { * @throws \Drupal\migrate\MigrateSkipRowException * If the file appears to be writable/deletable. */ - protected static function ensureNonWritable(string $path) : string { - $file = new \SplFileInfo($path); - - if (!$file->isFile()) { + protected function ensureNonWritable(string $path) : string { + if (!is_file($path)) { throw new MigrateSkipRowException(strtr('Source ({path}) does not appear to be a plain file; skipping row.', [ '{path}' => $path, ])); } - if ($file->isDir()) { + if (is_dir($path)) { throw new MigrateSkipRowException(strtr('Source ({path}) appears to be a directory; skipping row.', [ '{path}' => $path, ])); } - if (!$file->isReadable()) { + if (!is_readable($path)) { throw new MigrateSkipRowException(strtr('Source ({path}) does not appear to be readable; skipping row.', [ '{path}' => $path, ])); } - if ($file->isWritable()) { + if (is_writable($path)) { throw new MigrateSkipRowException(strtr('Source ({path}) appears to be writable(/deletable); skipping row.', [ '{path}' => $path, ])); } - if ($file->getPathInfo()?->isWritable()) { + if (is_writable($this->fileSystem->dirname($path))) { throw new MigrateSkipRowException(strtr('Directory of source ({path}) appears writable(/deletable); skipping row.', [ '{path}' => $path, ])); @@ -59,4 +58,25 @@ protected static function ensureNonWritable(string $path) : string { return $path; } + /** + * Drupal's file system service. + * + * @var \Drupal\Core\File\FileSystemInterface + */ + protected FileSystemInterface $fileSystem; + + /** + * Accessor for Drupal's file system service. + * + * @return \Drupal\Core\File\FileSystemInterface + * Drupal's file system service. + */ + protected function getFileSystem() : FileSystemInterface { + if (!isset($this->fileSystem)) { + $this->fileSystem = \Drupal::service('file_system'); + } + + return $this->fileSystem; + } + } diff --git a/src/Plugin/migrate/process/NonWritable.php b/src/Plugin/migrate/process/NonWritable.php index 5825f8d..d1fc3e9 100644 --- a/src/Plugin/migrate/process/NonWritable.php +++ b/src/Plugin/migrate/process/NonWritable.php @@ -26,7 +26,7 @@ class NonWritable extends ProcessPluginBase { * {@inheritDoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { - return static::ensureNonWritable($value); + return $this->ensureNonWritable($value); } } From e829bb91ccea8beec29c6b9cbcb1063291e34bd6 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Mon, 7 Oct 2024 11:03:54 -0300 Subject: [PATCH 2/2] Use the filesystem service properly. Forgot to use the accessor, derp. --- src/Plugin/migrate/process/EnsureNonWritableTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/migrate/process/EnsureNonWritableTrait.php b/src/Plugin/migrate/process/EnsureNonWritableTrait.php index 45b01ec..5b69409 100644 --- a/src/Plugin/migrate/process/EnsureNonWritableTrait.php +++ b/src/Plugin/migrate/process/EnsureNonWritableTrait.php @@ -49,7 +49,7 @@ protected function ensureNonWritable(string $path) : string { '{path}' => $path, ])); } - if (is_writable($this->fileSystem->dirname($path))) { + if (is_writable($this->getFileSystem()->dirname($path))) { throw new MigrateSkipRowException(strtr('Directory of source ({path}) appears writable(/deletable); skipping row.', [ '{path}' => $path, ]));