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

Filesytem updates and bug fixes #217

Merged
merged 2 commits into from
Aug 24, 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
148 changes: 70 additions & 78 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.22.0@fe2c67ec89f358940f90db05efd2d663388b45a6">
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
<file src="src/SonsOfPHP/Bard/Tests/JsonFileTest.php">
<UnusedClass>
<code><![CDATA[JsonFileTest]]></code>
Expand Down Expand Up @@ -642,9 +642,6 @@
<MissingTemplateParam>
<code><![CDATA[IteratorAggregate]]></code>
</MissingTemplateParam>
<PossiblyUnusedMethod>
<code><![CDATA[jsonSerialize]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/SonsOfPHP/Component/Cqrs/AbstractQueryMessage.php">
<UnusedClass>
Expand Down Expand Up @@ -1147,12 +1144,6 @@
<code><![CDATA[MockToggle]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Adapter/AdapterInterface.php">
<UndefinedDocblockClass>
<code><![CDATA[FileNotFouneException]]></code>
<code><![CDATA[string|resouce]]></code>
</UndefinedDocblockClass>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php">
<UnusedClass>
<code><![CDATA[ChainAdapter]]></code>
Expand All @@ -1167,13 +1158,12 @@
</UnusedForeachValue>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php">
<UndefinedVariable>
<code><![CDATA[$filename]]></code>
<code><![CDATA[$filename]]></code>
</UndefinedVariable>
<UnusedClass>
<code><![CDATA[NativeAdapter]]></code>
</UnusedClass>
<UnusedParam>
<code><![CDATA[$context]]></code>
</UnusedParam>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php">
<UnusedClass>
Expand All @@ -1195,37 +1185,11 @@
<code><![CDATA[Context]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/ContextInterface.php">
<MissingTemplateParam>
<code><![CDATA[ArrayAccess]]></code>
<code><![CDATA[IteratorAggregate]]></code>
</MissingTemplateParam>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Exception/UnableToCopyFileException.php">
<UnusedClass>
<code><![CDATA[UnableToCopyFileException]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Exception/UnableToDeleteDirectoryException.php">
<UnusedClass>
<code><![CDATA[UnableToDeleteDirectoryException]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Exception/UnableToDeleteFileException.php">
<UnusedClass>
<code><![CDATA[UnableToDeleteFileException]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Exception/UnableToMoveFileException.php">
<UnusedClass>
<code><![CDATA[UnableToMoveFileException]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Exception/UnableToWriteFileException.php">
<UnusedClass>
<code><![CDATA[UnableToWriteFileException]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Filesystem.php">
<InvalidReturnStatement>
<code><![CDATA[$this->adapter->get($path, $context)]]></code>
Expand All @@ -1243,13 +1207,6 @@
<code><![CDATA[Filesystem]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/FilesystemInterface.php">
<PossiblyUnusedMethod>
<code><![CDATA[exists]]></code>
<code><![CDATA[move]]></code>
<code><![CDATA[read]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/SonsOfPHP/Component/Filesystem/Tests/Adapter/ChainAdapterTest.php">
<UnusedClass>
<code><![CDATA[ChainAdapterTest]]></code>
Expand Down Expand Up @@ -1576,6 +1533,11 @@
<code><![CDATA[GroupIdEnricher]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Logger/Enricher/HostnameEnricher.php">
<UnusedClass>
<code><![CDATA[HostnameEnricher]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Logger/Enricher/InodeEnricher.php">
<UnusedClass>
<code><![CDATA[InodeEnricher]]></code>
Expand Down Expand Up @@ -1627,23 +1589,7 @@
<code><![CDATA[MockFilter]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Logger/Formatter/SimpleFormatter.php">
<UnusedClass>
<code><![CDATA[SimpleFormatter]]></code>
</UnusedClass>
<UnusedVariable>
<code><![CDATA[$message]]></code>
</UnusedVariable>
</file>
<file src="src/SonsOfPHP/Component/Logger/Handler/AbstractHandler.php">
<UndefinedMethod>
<code><![CDATA[doHandle]]></code>
</UndefinedMethod>
</file>
<file src="src/SonsOfPHP/Component/Logger/Handler/FileHandler.php">
<PossiblyUnusedParam>
<code><![CDATA[$record]]></code>
</PossiblyUnusedParam>
<UnusedClass>
<code><![CDATA[FileHandler]]></code>
</UnusedClass>
Expand All @@ -1654,9 +1600,6 @@
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Logger/Handler/StreamHandler.php">
<PossiblyUnusedParam>
<code><![CDATA[$record]]></code>
</PossiblyUnusedParam>
<UnusedClass>
<code><![CDATA[StreamHandler]]></code>
</UnusedClass>
Expand All @@ -1671,14 +1614,6 @@
<code><![CDATA[Logger]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Logger/Record.php">
<UndefinedPropertyAssignment>
<code><![CDATA[$that->datatime]]></code>
</UndefinedPropertyAssignment>
<UndefinedVariable>
<code><![CDATA[$datatime]]></code>
</UndefinedVariable>
</file>
<file src="src/SonsOfPHP/Component/Logger/Tests/ContextTest.php">
<UnusedClass>
<code><![CDATA[ContextTest]]></code>
Expand All @@ -1689,6 +1624,11 @@
<code><![CDATA[GroupIdEnricherTest]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Logger/Tests/Enricher/HostnameEnricherTest.php">
<UnusedClass>
<code><![CDATA[HostnameEnricherTest]]></code>
</UnusedClass>
</file>
<file src="src/SonsOfPHP/Component/Logger/Tests/Enricher/InodeEnricherTest.php">
<UnusedClass>
<code><![CDATA[InodeEnricherTest]]></code>
Expand Down Expand Up @@ -1829,9 +1769,6 @@
<UnusedClass>
<code><![CDATA[NativeMailTransport]]></code>
</UnusedClass>
<UnusedFunctionCall>
<code><![CDATA[mail]]></code>
</UnusedFunctionCall>
</file>
<file src="src/SonsOfPHP/Component/Mailer/Transport/NullTransport.php">
<UnusedClass>
Expand All @@ -1843,6 +1780,11 @@
<code><![CDATA[toFloat]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/SonsOfPHP/Component/Money/Currency.php">
<PossiblyUnusedMethod>
<code><![CDATA[__callStatic]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/SonsOfPHP/Component/Money/CurrencyProvider/ChainCurrencyProvider.php">
<InvalidReturnType>
<code><![CDATA[iterable]]></code>
Expand Down Expand Up @@ -1889,7 +1831,6 @@
<PossiblyUnusedMethod>
<code><![CDATA[__callStatic]]></code>
<code><![CDATA[compare]]></code>
<code><![CDATA[jsonSerialize]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/SonsOfPHP/Component/Money/Operator/Amount/AddAmountOperator.php">
Expand Down Expand Up @@ -2233,6 +2174,56 @@
<code><![CDATA[getFeatureToggleByKey]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/SonsOfPHP/Contract/Filesystem/Adapter/AdapterInterface.php">
<InvalidThrow>
<code><![CDATA[FilesystemExceptionInterface]]></code>
<code><![CDATA[FilesystemExceptionInterface]]></code>
<code><![CDATA[FilesystemExceptionInterface]]></code>
<code><![CDATA[FilesystemExceptionInterface]]></code>
</InvalidThrow>
<UndefinedDocblockClass>
<code><![CDATA[FileNotFouneExceptionInterface]]></code>
<code><![CDATA[string|resouce]]></code>
</UndefinedDocblockClass>
</file>
<file src="src/SonsOfPHP/Contract/Filesystem/Adapter/CopyAwareInterface.php">
<InvalidThrow>
<code><![CDATA[FileNotFoundExceptionInterface]]></code>
<code><![CDATA[FilesystemExceptionInterface]]></code>
</InvalidThrow>
</file>
<file src="src/SonsOfPHP/Contract/Filesystem/Adapter/DirectoryAwareInterface.php">
<InvalidThrow>
<code><![CDATA[FilesystemExceptionInterface]]></code>
</InvalidThrow>
</file>
<file src="src/SonsOfPHP/Contract/Filesystem/Adapter/MoveAwareInterface.php">
<InvalidThrow>
<code><![CDATA[FileNotFoundExceptionInterface]]></code>
<code><![CDATA[FilesystemExceptionInterface]]></code>
</InvalidThrow>
</file>
<file src="src/SonsOfPHP/Contract/Filesystem/ContextInterface.php">
<MissingTemplateParam>
<code><![CDATA[ArrayAccess]]></code>
<code><![CDATA[IteratorAggregate]]></code>
</MissingTemplateParam>
</file>
<file src="src/SonsOfPHP/Contract/Filesystem/FilesystemInterface.php">
<InvalidThrow>
<code><![CDATA[FilesystemExceptionInterface]]></code>
<code><![CDATA[FilesystemExceptionInterface]]></code>
<code><![CDATA[FilesystemExceptionInterface]]></code>
<code><![CDATA[FilesystemExceptionInterface]]></code>
<code><![CDATA[FilesystemExceptionInterface]]></code>
<code><![CDATA[FilesystemExceptionInterface]]></code>
</InvalidThrow>
<PossiblyUnusedMethod>
<code><![CDATA[exists]]></code>
<code><![CDATA[move]]></code>
<code><![CDATA[read]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/SonsOfPHP/Contract/HttpHandler/MiddlewareStackInterface.php">
<InvalidThrow>
<code><![CDATA[HttpHandlerExceptionInterface]]></code>
Expand Down Expand Up @@ -2264,6 +2255,7 @@
<code><![CDATA[withChannel]]></code>
<code><![CDATA[withDatetime]]></code>
<code><![CDATA[withLevel]]></code>
<code><![CDATA[withMessage]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/SonsOfPHP/Contract/Mailer/AddressInterface.php">
Expand Down
68 changes: 59 additions & 9 deletions src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

namespace SonsOfPHP\Component\Filesystem\Adapter;

use SonsOfPHP\Component\Filesystem\Exception\FileNotFoundException;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use SonsOfPHP\Component\Filesystem\Exception\UnableToCopyFileException;
use SonsOfPHP\Component\Filesystem\Exception\UnableToDeleteFileException;
use SonsOfPHP\Component\Filesystem\Exception\UnableToMoveFileException;
use SonsOfPHP\Component\Filesystem\Exception\UnableToWriteFileException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
Expand All @@ -18,25 +24,50 @@
*
* @author Joshua Estes <[email protected]>
*/
final readonly class NativeAdapter implements AdapterInterface, CopyAwareInterface, DirectoryAwareInterface, MoveAwareInterface
final class NativeAdapter implements AdapterInterface, CopyAwareInterface, DirectoryAwareInterface, MoveAwareInterface
{
public function __construct(
private string $prefix,
) {}
private readonly int $defaultPermissions = 0o777,
) {
$this->prefix = rtrim($prefix, '/');
}

public function add(string $path, mixed $contents, ?ContextInterface $context = null): void
{
file_put_contents($this->prefix . $path, $contents);
if ($this->isFile($path, $context)) {
throw new FilesystemException(sprintf('File "%s" already exists', $path));
}

$this->makeDirectory(dirname($path), $context);

if (false === file_put_contents($this->prefix . '/' . ltrim($path, '/'), $contents)) {
throw new UnableToWriteFileException('Unable to write file "' . $path . '"');
}

if (false === chmod($this->prefix . '/' . ltrim($path, '/'), $this->defaultPermissions)) {
throw new FilesystemException('Unable to set permissions on file "' . $path . '"');
}
}

public function get(string $path, ?ContextInterface $context = null): mixed
{
return file_get_contents($this->prefix . $path);
if (!$this->isFile($path, $context)) {
throw new FileNotFoundException('File "' . $path . '" not found');
}

return file_get_contents($this->prefix . '/' . ltrim($path, '/'));
}

public function remove(string $path, ?ContextInterface $context = null): void
{
unlink($this->prefix . $path);
if (!$this->isFile($path, $context)) {
throw new FileNotFoundException('File "' . $path . '" not found');
}

if (false === unlink($this->prefix . '/' . ltrim($path, '/'))) {
throw new UnableToDeleteFileException('Unable to remove file "' . $path . '"');
}
}

public function has(string $path, ?ContextInterface $context = null): bool
Expand All @@ -46,21 +77,40 @@ public function has(string $path, ?ContextInterface $context = null): bool

public function isFile(string $path, ?ContextInterface $context = null): bool
{
return is_file($filename);
return is_file($this->prefix . '/' . ltrim($path, '/'));
}

public function copy(string $source, string $destination, ?ContextInterface $context = null): void
{
copy($this->prefix . $source, $this->prefix . $destination);
if (!$this->isFile($source)) {
throw new FilesystemException('Source file "' . $source . '" does not exist');
}

if ($this->isFile($destination)) {
throw new FilesystemException('Destination file "' . $destination . '" already exists');
}

if (false === copy($this->prefix . '/' . ltrim($source, '/'), $this->prefix . '/' . ltrim($destination, '/'))) {
throw new UnableToCopyFileException('Unable to file "' . $source . '" to "' . $destination . '"');
}
}

public function isDirectory(string $path, ?ContextInterface $context = null): bool
{
return is_dir($filename);
return is_dir($this->prefix . '/' . ltrim($path, '/'));
}

public function makeDirectory(string $path, ?ContextInterface $context = null): void
{
if (!$this->isDirectory($path) && false === mkdir($this->prefix . '/' . ltrim($path, '/'), $this->defaultPermissions, true)) {
throw new FilesystemException('Unable to create directory "' . $path . '"');
}
}

public function move(string $source, string $destination, ?ContextInterface $context = null): void
{
rename($this->prefix . $source, $this->prefix . $destination);
if (false === rename($this->prefix . '/' . ltrim($source, '/'), $this->prefix . '/' . ltrim($destination, '/'))) {
throw new UnableToMoveFileException('Unable to move file "' . $source . '" to "' . $destination . '"');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace SonsOfPHP\Component\Filesystem\Exception;

use SonsOfPHP\Contract\Filesystem\Exception\UnableToCopyFileExceptionInterface;

/**
* @author Joshua Estes <[email protected]>
*/
final class UnableToCopyFileException extends FilesystemException {}
final class UnableToCopyFileException extends FilesystemException implements UnableToCopyFileExceptionInterface {}
Loading
Loading