Created
April 29, 2021 11:03
-
-
Save Jeckerson/e6f9e8e5b0a2febb4494e3a58cf79de9 to your computer and use it in GitHub Desktop.
FilesProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Providers; | |
use Aws\S3\S3Client; | |
use League\Flysystem\AwsS3V3\AwsS3V3Adapter; | |
use League\Flysystem\AwsS3V3\PortableVisibilityConverter; | |
use League\Flysystem\FilesystemAdapter; | |
use League\Flysystem\Local\LocalFilesystemAdapter; | |
use League\Flysystem\Visibility; | |
use Phlexus\Files\Files; | |
use Phlexus\Providers\AbstractProvider; | |
class FilesProvider extends AbstractProvider | |
{ | |
/** | |
* @var string | |
*/ | |
protected $providerName = 'files'; | |
/** | |
* @param array $parameters | |
*/ | |
public function register(array $parameters = []): void | |
{ | |
$fileSystemAdapter = $this->detectFileSystemAdapter(); | |
$this->di->setShared($this->providerName, function () use ($fileSystemAdapter) { | |
return new Files($fileSystemAdapter); | |
}); | |
} | |
/** | |
* @return FilesystemAdapter | |
*/ | |
private function detectFileSystemAdapter(): FilesystemAdapter | |
{ | |
$spacesKey = getenv('S3_KEY'); | |
$spacesSecret = getenv('S3_SECRET'); | |
$spacesBucket = getenv('S3_BUCKET'); | |
if (!$spacesKey || !$spacesSecret || !$spacesBucket) { | |
return new LocalFilesystemAdapter( | |
implode(DIRECTORY_SEPARATOR, [$this->di->getShared('rootPath'), 'public', 'uploads']) | |
); | |
} | |
$s3Client = new S3Client([ | |
'version' => 'latest', | |
'region' => 'us-east-1', | |
'endpoint' => 'https://aws.s3...', | |
'credentials' => [ | |
'key' => getenv('S3_KEY'), | |
'secret' => getenv('S3_SECRET'), | |
], | |
]); | |
return new AwsS3V3Adapter( | |
$s3Client, | |
getenv('S3_BUCKET'), | |
'', | |
new PortableVisibilityConverter( | |
Visibility::PUBLIC | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment