Skip to content

Commit

Permalink
Merge pull request #146 from discoverygarden/fix/avoid-spl-file-info
Browse files Browse the repository at this point in the history
Avoid use of `\SplFileInfo()`.
  • Loading branch information
jordandukart authored Oct 7, 2024
2 parents 567ed28 + e829bb9 commit a75b679
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
$row,
$destination_property,
),
'direct' => static::ensureNonWritable($value),
'direct' => $this->ensureNonWritable($value),
};
}

Expand Down
36 changes: 28 additions & 8 deletions src/Plugin/migrate/process/EnsureNonWritableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\dgi_migrate\Plugin\migrate\process;

use Drupal\Core\File\FileSystemInterface;
use Drupal\migrate\MigrateSkipRowException;

/**
Expand All @@ -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->getFileSystem()->dirname($path))) {
throw new MigrateSkipRowException(strtr('Directory of source ({path}) appears writable(/deletable); skipping row.', [
'{path}' => $path,
]));
Expand All @@ -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;
}

}
2 changes: 1 addition & 1 deletion src/Plugin/migrate/process/NonWritable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit a75b679

Please sign in to comment.