Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid use of \SplFileInfo(). #146

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
Loading