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

Additional Bridges for Filesystem component #220

Merged
merged 4 commits into from
Aug 26, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ To get the diff between two versions, go to https://github.com/SonsOfPHP/sonsofp

## [Unreleased]

* [PR #220](https://github.com/SonsOfPHP/sonsofphp/pull/220) [Filesystem] Adds bridges for AWS and LiipImagineBundle
* [PR #214](https://github.com/SonsOfPHP/sonsofphp/pull/214) [Logger] Bug fixes and updates
* [PR #51](https://github.com/SonsOfPHP/sonsofphp/pull/51) Added new Filesystem component
* [PR #59](https://github.com/SonsOfPHP/sonsofphp/pull/59) Added new HttpMessage component
Expand Down
16 changes: 12 additions & 4 deletions bard.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,22 @@
"path": "src/SonsOfPHP/Component/FeatureToggle",
"repository": "[email protected]:SonsOfPHP/feature-toggle.git"
},
{
"path": "src/SonsOfPHP/Contract/Filesystem",
"repository": "[email protected]:SonsOfPHP/filesystem-contract.git"
},
{
"path": "src/SonsOfPHP/Component/Filesystem",
"repository": "[email protected]:SonsOfPHP/filesystem.git"
},
{
"path": "src/SonsOfPHP/Bridge/Aws/Filesystem",
"repository": "[email protected]:SonsOfPHP/filesystem-aws.git"
},
{
"path": "src/SonsOfPHP/Bridge/LiipImagine/Filesystem",
"repository": "[email protected]:SonsOfPHP/filesystem-liip-imagine.git"
},
{
"path": "src/SonsOfPHP/Component/HttpFactory",
"repository": "[email protected]:SonsOfPHP/http-factory.git"
Expand Down Expand Up @@ -141,10 +153,6 @@
"path": "src/SonsOfPHP/Contract/FeatureToggle",
"repository": "[email protected]:SonsOfPHP/feature-toggle-contract.git"
},
{
"path": "src/SonsOfPHP/Contract/Filesystem",
"repository": "[email protected]:SonsOfPHP/filesystem-contract.git"
},
{
"path": "src/SonsOfPHP/Contract/Logger",
"repository": "[email protected]:SonsOfPHP/logger-contract.git"
Expand Down
16 changes: 12 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,17 @@
"psr/http-factory": "^1.0",
"psr/cache": "^2.0 || ^3.0",
"psr/simple-cache": "^3.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"psr/log": "^2.0 || ^3.0",
"psr/link": "^1.0 || ^2.0",
"twig/twig": "^3.0",
"ext-intl": "*",
"doctrine/collections": "^2",
"doctrine/orm": "^2 || ^3",
"psr/container": "^1.0 || ^2.0",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0"
"psr/http-server-middleware": "^1.0",
"aws/aws-sdk-php": "^3.0",
"liip/imagine-bundle": "^2.0"
},
"replace": {
"sonsofphp/bard": "self.version",
Expand Down Expand Up @@ -127,7 +129,9 @@
"sonsofphp/http-handler": "self.version",
"sonsofphp/http-handler-contract": "self.version",
"sonsofphp/mailer-contract": "self.version",
"sonsofphp/mailer": "self.version"
"sonsofphp/mailer": "self.version",
"sonsofphp/filesystem-aws": "self.version",
"sonsofphp/filesystem-liip-imagine": "self.version"
},
"autoload": {
"psr-4": {
Expand All @@ -147,7 +151,10 @@
"SonsOfPHP\\Component\\EventSourcing\\": "src/SonsOfPHP/Component/EventSourcing",
"SonsOfPHP\\Bridge\\Doctrine\\EventSourcing\\": "src/SonsOfPHP/Bridge/Doctrine/EventSourcing",
"SonsOfPHP\\Component\\FeatureToggle\\": "src/SonsOfPHP/Component/FeatureToggle",
"SonsOfPHP\\Contract\\Filesystem\\": "src/SonsOfPHP/Contract/Filesystem",
"SonsOfPHP\\Component\\Filesystem\\": "src/SonsOfPHP/Component/Filesystem",
"SonsOfPHP\\Bridge\\Aws\\Filesystem\\": "src/SonsOfPHP/Bridge/Aws/Filesystem",
"SonsOfPHP\\Bridge\\LiipImagine\\Filesystem\\": "src/SonsOfPHP/Bridge/LiipImagine/Filesystem",
"SonsOfPHP\\Component\\HttpFactory\\": "src/SonsOfPHP/Component/HttpFactory",
"SonsOfPHP\\Component\\HttpHandler\\": "src/SonsOfPHP/Component/HttpHandler",
"SonsOfPHP\\Contract\\HttpHandler\\": "src/SonsOfPHP/Contract/HttpHandler",
Expand All @@ -166,7 +173,6 @@
"SonsOfPHP\\Contract\\Cqrs\\": "src/SonsOfPHP/Contract/Cqrs",
"SonsOfPHP\\Contract\\EventSourcing\\": "src/SonsOfPHP/Contract/EventSourcing",
"SonsOfPHP\\Contract\\FeatureToggle\\": "src/SonsOfPHP/Contract/FeatureToggle",
"SonsOfPHP\\Contract\\Filesystem\\": "src/SonsOfPHP/Contract/Filesystem",
"SonsOfPHP\\Contract\\Logger\\": "src/SonsOfPHP/Contract/Logger",
"SonsOfPHP\\Contract\\Money\\": "src/SonsOfPHP/Contract/Money",
"SonsOfPHP\\Contract\\Pager\\": "src/SonsOfPHP/Contract/Pager",
Expand All @@ -189,6 +195,8 @@
"src/SonsOfPHP/Bridge/Doctrine/EventSourcing/Tests",
"src/SonsOfPHP/Component/FeatureToggle/Tests",
"src/SonsOfPHP/Component/Filesystem/Tests",
"src/SonsOfPHP/Bridge/Aws/Filesystem/Tests",
"src/SonsOfPHP/Bridge/LiipImagine/Filesystem/Tests",
"src/SonsOfPHP/Component/HttpFactory/Tests",
"src/SonsOfPHP/Component/HttpHandler/Tests",
"src/SonsOfPHP/Component/HttpMessage/Tests",
Expand Down
16 changes: 16 additions & 0 deletions docs/components/filesystem/adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,19 @@ use SonsOfPHP\Component\Filesystem\Adapter\NativeAdapter;

$adapter = new WormAdapter(new NativeAdater('/tmp'));
```

## Additional Adapters

### AWS S3

```shell
composer require sonsofphp/filesystem-aws
```

```php
<?php

use SonsOfPHP\Bridge\Aws\Filesystem\Adapter\S3Adapter;

$adapter = new S3Adapter($s3Client, 'bucket-name');
```
31 changes: 31 additions & 0 deletions docs/components/filesystem/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,37 @@ $filesystem->copy('source.txt', 'destination.txt');
$filesystem->move('source.txt', 'destination.txt');
```

## Support for LiipImagineBundle

```shell
composer require sonsofphp/filesystem-liip-imagine
```

```yaml
# config/services.yaml
services:
SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface:
class: SonsOfPHP\Component\Filesystem\Adapter\NativeAdapter
arguments: ['%kernel.project_dir%/var/data/%kernel.id%']
SonsOfPHP\Contract\Filesystem\FilesystemInterface:
class: SonsOfPHP\Component\Filesystem\Filesystem
arguments: ['@SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface']
imagine.cache.resolver.sonsofphp:
class: SonsOfPHP\Bridge\LiipImagine\Filesystem\Imagine\Cache\Resolver\SonsOfPHPFilesystemResolver
arguments:
- '@SonsOfPHP\Contract\Filesystem\FilesystemInterface'
- 'https://images.example.com'
tags:
- { name: "liip_imagine.cache.resolver", resolver: sonsofphp }
```

```yaml
# config/packages/liip_imagine.yaml
liip_imagine:
data_loader: SonsOfPHP\Bridge\LiipImagine\Filesystem\Binary\Loader\SonsOfPHPFilesystemLoader
cache: sonsofphp
```

## Need Help?

Check out [Sons of PHP's Organization Discussions][discussions].
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

<testsuite name="feature-toggle">
<directory>src/SonsOfPHP/Component/FeatureToggle/Tests</directory>
<directory>src/SonsOfPHP/Bridge/Aws/Filesystem/Tests</directory>
<directory>src/SonsOfPHP/Bridge/LiipImagine/Filesystem/Tests</directory>
</testsuite>

<testsuite name="filesystem">
Expand Down
4 changes: 4 additions & 0 deletions src/SonsOfPHP/Bridge/Aws/Filesystem/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/SonsOfPHP/Bridge/Aws/Filesystem/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
183 changes: 183 additions & 0 deletions src/SonsOfPHP/Bridge/Aws/Filesystem/Adapter/S3Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Bridge\Aws\Filesystem\Adapter;

use Aws\S3\S3ClientInterface;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use SonsOfPHP\Component\Filesystem\Exception\UnableToCopyFileException;
use SonsOfPHP\Component\Filesystem\Exception\UnableToDeleteDirectoryException;
use SonsOfPHP\Component\Filesystem\Exception\UnableToDeleteFileException;
use SonsOfPHP\Component\Filesystem\Exception\UnableToWriteFileException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Contract\Filesystem\ContextInterface;
use Throwable;

/**
* Usage:
* $adapter = new S3Adapter($s3Client, 'bucket-name');
*
* @author Joshua Estes <[email protected]>
*/
final readonly class S3Adapter implements AdapterInterface, CopyAwareInterface, DirectoryAwareInterface, MoveAwareInterface
{
public function __construct(
private S3ClientInterface $client,
private string $bucket,
private string $acl = 'private',
) {}

public function add(string $path, mixed $contents, ?ContextInterface $context = null): void
{
$options = [
'Bucket' => $this->bucket,
'Prefix' => ltrim($path, '/'),
'params' => [],
];
if (isset($context['mimeType'])) {
$options['params']['ContentType'] = $context['mimeType'];
}
try {
$this->client->upload(
$this->bucket,
ltrim($path, '/'),
$contents,
$context['acl'] ?? $this->acl,
$options,
);
} catch (Throwable $exception) {
throw new UnableToWriteFileException($exception->getMessage());
}
}

public function get(string $path, ?ContextInterface $context = null): mixed
{
$options = [
'Bucket' => $this->bucket,
'Key' => ltrim($path, '/'),
'MaxKeys' => 1,
'Delimiter' => '/',
];
$command = $this->client->getCommand('GetObject', $options);

try {
return (string) $this->client->execute($command)->get('Body')->getContents();
} catch (Throwable) {
throw new FilesystemException('Could not read file');
}
}

public function remove(string $path, ?ContextInterface $context = null): void
{
$options = [
'Bucket' => $this->bucket,
'Key' => ltrim($path, '/'),
];
$command = $this->client->getCommand('DeleteObject', $options);

try {
$this->client->execute($command);
} catch (Throwable $exception) {
throw new UnableToDeleteFileException($exception->getMessage());
}
}

public function has(string $path, ?ContextInterface $context = null): bool
{
if ($this->isFile($path, $context)) {
return true;
}

return $this->isDirectory($path, $context);
}

public function isFile(string $path, ?ContextInterface $context = null): bool
{
try {
return $this->client->doesObjectExistV2($this->bucket, ltrim($path, '/'), false);
} catch (Throwable) {
throw new FilesystemException('Unable to check if file exists');
}
}

public function copy(string $source, string $destination, ?ContextInterface $context = null): void
{
try {
$this->client->copy(
$this->bucket,
ltrim($source, '/'),
$this->bucket,
ltrim($destination, '/'),
$context['acl'] ?? $this->acl,
);
} catch (Throwable) {
throw new UnableToCopyFileException();
}
}

public function isDirectory(string $path, ?ContextInterface $context = null): bool
{
$options = [
'Bucket' => $this->bucket,
'Prefix' => ltrim($path, '/'),
'MaxKeys' => 1,
'Delimiter' => '/',
];
$command = $this->client->getCommand('ListObjectsV2', $options);

try {
$result = $this->client->execute($command);
if ($result->hasKey('Contents')) {
return true;
}
return $result->hasKey('CommonPrefixes');
} catch (Throwable) {
throw new FilesystemException('Could not figure out if directory exists');
}
}

public function makeDirectory(string $path, ?ContextInterface $context = null): void
{
$this->add($path, '', $context);
}

public function removeDirectory(string $path, ?ContextInterface $context = null): void
{
try {
$this->client->deleteMatchingObjects($this->bucket, $path);
} catch (Throwable) {
throw new UnableToDeleteDirectoryException();
}
}

public function move(string $source, string $destination, ?ContextInterface $context = null): void
{
try {
$this->copy($source, $destination, $context);
$this->remove($source);
} catch (Throwable) {
throw new UnableToMoveFile();

Check failure on line 163 in src/SonsOfPHP/Bridge/Aws/Filesystem/Adapter/S3Adapter.php

View workflow job for this annotation

GitHub Actions / Psalm (8.2)

UndefinedClass

src/SonsOfPHP/Bridge/Aws/Filesystem/Adapter/S3Adapter.php:163:23: UndefinedClass: Class, interface or enum named SonsOfPHP\Bridge\Aws\Filesystem\Adapter\UnableToMoveFile does not exist (see https://psalm.dev/019)
}
}

public function mimeType(string $path, ?ContextInterface $context = null): string
{
$options = [
'Bucket' => $this->bucket,
'Key' => ltrim($path, '/'),
];
$command = $this->client->getCommand('HeadObject', $options);

try {
$result = $this->client->execute($command);
} catch (Throwable) {
throw new FilesystemException();
}

return $result->get('ContentType');

Check failure on line 181 in src/SonsOfPHP/Bridge/Aws/Filesystem/Adapter/S3Adapter.php

View workflow job for this annotation

GitHub Actions / Psalm (8.2)

NullableReturnStatement

src/SonsOfPHP/Bridge/Aws/Filesystem/Adapter/S3Adapter.php:181:16: NullableReturnStatement: The declared return type 'string' for SonsOfPHP\Bridge\Aws\Filesystem\Adapter\S3Adapter::mimeType is not nullable, but the function returns 'mixed|null' (see https://psalm.dev/139)
}
}
19 changes: 19 additions & 0 deletions src/SonsOfPHP/Bridge/Aws/Filesystem/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2022 to Present Joshua Estes

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions src/SonsOfPHP/Bridge/Aws/Filesystem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Sons of PHP - Money - Twig Bridge
=================================

## Learn More

* [Documentation][docs]
* [Contributing][contributing]
* [Report Issues][issues] and [Submit Pull Requests][pull-requests] in the
[Mother Repository][mother-repo]
* Get Help & Support using [Discussions][discussions]

[discussions]: https://github.com/orgs/SonsOfPHP/discussions
[mother-repo]: https://github.com/SonsOfPHP/sonsofphp
[contributing]: https://docs.sonsofphp.com/contributing/
[docs]: https://docs.sonsofphp.com/components/money/
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3AMoney
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3AMoney
Loading
Loading