-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from fortrabbit/craft4
Craft4 support
- Loading branch information
Showing
12 changed files
with
367 additions
and
201 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ | |
*Thumbs.db | ||
vendor/ | ||
composer.lock | ||
.phpunit.result.cache | ||
tests/storage | ||
build/phpstan |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
Changelog | ||
========= | ||
## 3.0.0 - 2022-05-25 | ||
* Craft 4 and PHP 8 support | ||
|
||
## 2.1.0 - 2021-03-03 | ||
|
||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
includes: | ||
- vendor/craftcms/phpstan/phpstan.neon | ||
|
||
parameters: | ||
level: 5 | ||
paths: | ||
- src | ||
- bin |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use craft\rector\SetList as CraftSetList; | ||
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector; | ||
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector; | ||
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector; | ||
use Rector\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector; | ||
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector; | ||
use Rector\Core\Configuration\Option; | ||
use Rector\Core\ValueObject\PhpVersion; | ||
use Rector\Set\ValueObject\LevelSetList; | ||
use Rector\Set\ValueObject\SetList; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
// Optional directories to skip | ||
$parameters = $containerConfigurator->parameters(); | ||
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_80); | ||
$parameters->set(Option::SKIP, [ | ||
EncapsedStringsToSprintfRector::class, | ||
RemoveFinalFromConstRector::class, | ||
SimplifyIfReturnBoolRector::class, | ||
UnnecessaryTernaryExpressionRector::class, | ||
ExplicitBoolCompareRector::class | ||
]); | ||
|
||
|
||
// Craft Version | ||
$containerConfigurator->import(CraftSetList::CRAFT_CMS_40); | ||
// PHP Version | ||
$containerConfigurator->import(LevelSetList::UP_TO_PHP_80); | ||
// Other | ||
$containerConfigurator->import(SetList::CODE_QUALITY); | ||
$containerConfigurator->import(SetList::CODING_STYLE); | ||
$containerConfigurator->import(SetList::DEAD_CODE); | ||
$containerConfigurator->import(SetList::TYPE_DECLARATION); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace fortrabbit\ObjectStorage; | ||
|
||
use Aws\Handler\GuzzleV6\GuzzleHandler; | ||
use Craft; | ||
use craft\flysystem\base\FlysystemFs; | ||
use craft\helpers\App; | ||
use craft\helpers\DateTimeHelper; | ||
use DateTime; | ||
use League\Flysystem\AwsS3V3\AwsS3V3Adapter; | ||
use League\Flysystem\AwsS3V3\PortableVisibilityConverter; | ||
use League\Flysystem\FilesystemAdapter; | ||
use League\Flysystem\Visibility; | ||
use League\MimeTypeDetection\FinfoMimeTypeDetector; | ||
|
||
/** | ||
* Class ObjectStorageFs | ||
* | ||
* @property mixed $settingsHtml | ||
* @property string $rootUrl | ||
*/ | ||
class Fs extends FlysystemFs | ||
{ | ||
|
||
public static function displayName(): string | ||
{ | ||
return 'fortrabbit Object Storage'; | ||
} | ||
|
||
/** | ||
* @var string Subfolder to use | ||
*/ | ||
public string $subfolder = ''; | ||
|
||
/** | ||
* @var string AWS key ID | ||
*/ | ||
public string $keyId = ''; | ||
|
||
/** | ||
* @var string AWS key secret | ||
*/ | ||
public string $secret = ''; | ||
|
||
/** | ||
* @var string Bucket to use | ||
*/ | ||
public string $bucket = ''; | ||
|
||
/** | ||
* @var string Region to use | ||
*/ | ||
public string $region = ''; | ||
|
||
/** | ||
* @var string Cache expiration period as a string, like '2 hours' | ||
*/ | ||
public string $expires = ''; | ||
|
||
/** | ||
* @var string fortrabbit endpoint hostname | ||
*/ | ||
public string $endpoint = ''; | ||
/** | ||
* @var bool Set ACL for Uploads | ||
*/ | ||
public bool $makeUploadsPublic = true; | ||
|
||
/** | ||
* @var bool Whether the specified sub folder should be added to the root URL | ||
*/ | ||
public bool $addSubfolderToRootUrl = true; | ||
|
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function defineRules(): array | ||
{ | ||
return array_merge(parent::defineRules(), [ | ||
[['bucket', 'keyId', 'secret', 'endpoint'], 'required'], | ||
]); | ||
} | ||
|
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getSettingsHtml(): ?string | ||
{ | ||
return Craft::$app->getView()->renderTemplate('fortrabbit-object-storage/fsSettings', [ | ||
'fs' => $this, | ||
]); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getRootUrl(): ?string | ||
{ | ||
$rootUrl = parent::getRootUrl(); | ||
|
||
if ($this->url === '$OBJECT_STORAGE_HOST' || $this->url === '') { | ||
$rootUrl = 'https://' . Craft::parseEnv('$OBJECT_STORAGE_HOST') . '/'; | ||
} | ||
|
||
if ($rootUrl && $this->subfolder) { | ||
$rootUrl .= rtrim(App::parseEnv($this->subfolder), '/') . '/'; | ||
} | ||
|
||
return $rootUrl; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @return AwsS3V3Adapter | ||
*/ | ||
protected function createAdapter(): FilesystemAdapter | ||
{ | ||
$client = static::client($this->getConfigArray()); | ||
|
||
return new AwsS3V3Adapter( | ||
$client, | ||
App::parseEnv($this->bucket), | ||
$this->getParsedSubfolder(), | ||
new PortableVisibilityConverter(), | ||
new FinfoMimeTypeDetector() | ||
); | ||
} | ||
|
||
protected static function client(array $config = []): S3Client | ||
{ | ||
return new S3Client($config); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function addFileMetadataToConfig(array $config): array | ||
{ | ||
if (DateTimeHelper::isValidIntervalString($this->expires)) { | ||
$expires = new DateTime(); | ||
$now = new DateTime(); | ||
$expires->modify('+' . $this->expires); | ||
$diff = (int)$expires->format('U') - (int)$now->format('U'); | ||
$config['CacheControl'] = 'max-age=' . $diff; | ||
} | ||
|
||
return parent::addFileMetadataToConfig($config); | ||
} | ||
|
||
|
||
private function getParsedSubfolder(): string | ||
{ | ||
if ($this->subfolder && ($subfolder = rtrim(App::parseEnv($this->subfolder), '/')) !== '') { | ||
return $subfolder . '/'; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
|
||
/** | ||
* Get the config array for AWS Clients. | ||
*/ | ||
protected function getConfigArray(): array | ||
{ | ||
$endpoint = App::parseEnv($this->endpoint); | ||
|
||
if (!str_contains($endpoint, 'https')) { | ||
$endpoint = 'https://' . $endpoint; | ||
} | ||
|
||
return [ | ||
'version' => 'latest', | ||
'region' => App::parseEnv($this->region), | ||
'endpoint' => $endpoint, | ||
'http_handler' => new GuzzleHandler(Craft::createGuzzleClient()), | ||
'credentials' => [ | ||
'key' => App::parseEnv($this->keyId), | ||
'secret' => App::parseEnv($this->secret) | ||
] | ||
]; | ||
} | ||
|
||
|
||
/** | ||
* Returns the visibility setting for the Fs. | ||
* | ||
* @return string | ||
*/ | ||
protected function visibility(): string | ||
{ | ||
return $this->makeUploadsPublic ? Visibility::PUBLIC : Visibility::PRIVATE; | ||
} | ||
|
||
protected function invalidateCdnPath(string $path): bool | ||
{ | ||
return true; | ||
} | ||
} |
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
Oops, something went wrong.