Skip to content
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
10 changes: 0 additions & 10 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ class Config extends BaseConfig
public ?string $accessToken = null;
public ?string $redisUrl = null;
public ?string $signingKey = null;
public bool $useAssetBundleCdn = true;
public ?string $previewDomain = null;
public bool $useQueue = true;
public int $staticCacheDuration = DateTimeHelper::SECONDS_YEAR;
public int $staticCacheStaleWhileRevalidateDuration = DateTimeHelper::SECONDS_HOUR;
public ?string $storageEndpoint = null;
public bool $useAssetCdn = true;
public bool $useArtifactCdn = true;
public ?string $logLevel = null;
private bool $devMode = false;
private ?string $region = null;
Expand All @@ -47,8 +45,6 @@ public function init(): void
if (!Helper::isCraftCloud()) {
$this->gzipResponse = false;
$this->useAssetCdn = false;
$this->useArtifactCdn = false;
$this->useAssetBundleCdn = false;
$this->useQueue = false;
}
}
Expand Down Expand Up @@ -157,12 +153,6 @@ protected function defineRules(): array
'when' => fn(Config $model) => $model->useAssetCdn,
];

$rules[] = [
['environmentId', 'buildId'],
'required',
'when' => fn(Config $model) => $model->useArtifactCdn,
];

return $rules;
}
}
2 changes: 1 addition & 1 deletion src/controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function actionGetUploadUrl(): Response
'targetFilename' => Assets::prepareAssetName($originalFilename),
'filename' => $filename,
'bucket' => $fs->getBucketName(),
'key' => $fs->prefixPath($filename),
'key' => $fs->createBucketPath($filename),
'folderId' => $folder->id,
]);
}
Expand Down
14 changes: 4 additions & 10 deletions src/fs/AssetsFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use craft\cloud\Module;
use League\Uri\Components\HierarchicalPath;

class AssetsFs extends CdnFs
class AssetsFs extends Fs
{
protected ?string $expires = '1 years';
public ?string $localFsPath = '@webroot/uploads';
public ?string $localFsUrl = '/uploads';

Expand All @@ -24,15 +25,8 @@
return 'Craft Cloud';
}

public function getPrefix(): string
public function createBucketPrefix(): HierarchicalPath
{
if (!Module::getInstance()->getConfig()->useAssetCdn) {
return '';
}

return HierarchicalPath::fromRelative(
parent::getPrefix(),
'assets',
)->withoutEmptySegments()->withoutTrailingSlash();
return parent::createBucketPrefix()->append('assets');

Check failure on line 30 in src/fs/AssetsFs.php

View workflow job for this annotation

GitHub Actions / Code Quality / PHPStan / PHPStan

Method craft\cloud\fs\AssetsFs::createBucketPrefix() should return League\Uri\Components\HierarchicalPath but returns League\Uri\Contracts\SegmentedPathInterface.
}
}
17 changes: 4 additions & 13 deletions src/fs/BuildArtifactsFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,16 @@

class BuildArtifactsFs extends BuildsFs
{
public ?string $localFsPath = '@webroot';
public ?string $localFsUrl = '@web';
public bool $hasUrls = true;

public function init(): void
{
$this->useLocalFs = !Module::getInstance()->getConfig()->useArtifactCdn;
$this->localFsUrl = Module::getInstance()->getConfig()->artifactBaseUrl ?? $this->localFsUrl;
$this->baseUrl = Module::getInstance()->getConfig()->artifactBaseUrl;
parent::init();
}

public function getPrefix(): string
public function createBucketPrefix(): HierarchicalPath
{
if (!Module::getInstance()->getConfig()->useArtifactCdn) {
return '';
}

return HierarchicalPath::fromRelative(
parent::getPrefix(),
'artifacts',
)->withoutEmptySegments()->withoutTrailingSlash();
return parent::createBucketPrefix()->append('artifacts');

Check failure on line 20 in src/fs/BuildArtifactsFs.php

View workflow job for this annotation

GitHub Actions / Code Quality / PHPStan / PHPStan

Method craft\cloud\fs\BuildArtifactsFs::createBucketPrefix() should return League\Uri\Components\HierarchicalPath but returns League\Uri\Contracts\SegmentedPathInterface.
}
}
15 changes: 8 additions & 7 deletions src/fs/BuildsFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use craft\cloud\Module;
use League\Uri\Components\HierarchicalPath;

abstract class BuildsFs extends CdnFs
abstract class BuildsFs extends Fs
{
public function getPrefix(): string
public bool $hasUrls = true;
protected ?string $expires = '1 years';

public function createBucketPrefix(): HierarchicalPath
{
return HierarchicalPath::fromRelative(
parent::getPrefix(),
'builds',
Module::getInstance()->getConfig()->buildId,
)->withoutEmptySegments()->withoutTrailingSlash();
return parent::createBucketPrefix()

Check failure on line 15 in src/fs/BuildsFs.php

View workflow job for this annotation

GitHub Actions / Code Quality / PHPStan / PHPStan

Method craft\cloud\fs\BuildsFs::createBucketPrefix() should return League\Uri\Components\HierarchicalPath but returns League\Uri\Contracts\SegmentedPathInterface.
->append('builds')
->append(Module::getInstance()->getConfig()->buildId);
}
}
32 changes: 0 additions & 32 deletions src/fs/CdnFs.php

This file was deleted.

7 changes: 2 additions & 5 deletions src/fs/CpResourcesFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

class CpResourcesFs extends BuildsFs
{
public function getPrefix(): string
public function createBucketPrefix(): HierarchicalPath
{
return HierarchicalPath::fromRelative(
parent::getPrefix(),
'cpresources',
)->withoutEmptySegments()->withoutTrailingSlash();
return parent::createBucketPrefix()->append('cpresources');

Check failure on line 11 in src/fs/CpResourcesFs.php

View workflow job for this annotation

GitHub Actions / Code Quality / PHPStan / PHPStan

Method craft\cloud\fs\CpResourcesFs::createBucketPrefix() should return League\Uri\Components\HierarchicalPath but returns League\Uri\Contracts\SegmentedPathInterface.
}
}
68 changes: 39 additions & 29 deletions src/fs/Fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Craft;
use craft\behaviors\EnvAttributeParserBehavior;
use craft\cloud\Module;
use craft\cloud\StaticCache;
use craft\cloud\StaticCacheTag;
use craft\errors\FsException;
use craft\flysystem\base\FlysystemFs;
use craft\fs\Local;
Expand All @@ -30,9 +32,6 @@
use yii\base\InvalidConfigException;

/**
*
* @property-read string $bucketName
* @property-read string $prefix
* @property-read ?string $settingsHtml
*/
abstract class Fs extends FlysystemFs
Expand All @@ -44,8 +43,8 @@
public ?string $subpath = null;
public ?string $localFsPath = null;
public ?string $localFsUrl = null;
public ?string $url = '__URL__';
public bool $useLocalFs = false;
public ?string $baseUrl = null;

/**
* @inheritDoc
Expand All @@ -66,13 +65,13 @@
protected function getLocalFs(): Local
{
$path = $this->localFsPath
? HierarchicalPath::new($this->localFsPath)->append($this->prefixPath())->toString()
? $this->createPath('')->prepend($this->localFsPath)
: null;

$this->localFs = $this->localFs ?? Craft::createObject([
'class' => Local::class,
'hasUrls' => $this->hasUrls,
'path' => $path,
'path' => $path->toString(),
'url' => $this->localFsUrl,
]);

Expand All @@ -93,19 +92,16 @@

public function createUrl(string $path = ''): UriInterface
{
$baseUrl = $this->useLocalFs
? $this->getLocalFs()->getRootUrl() ?? '/'
: Module::getInstance()->getConfig()->cdnBaseUrl;
$baseUrl = $this->useLocalFs ? $this->getLocalFs()->getRootUrl() : $this->baseUrl;

// If an alias is unparsed by now, we have to fall back to a root relative URL.
// This likely means this is a console request and @web isn't set.
if (str_starts_with($baseUrl, '@')) {
$baseUrl = '/';
if ($baseUrl) {
return Modifier::from($baseUrl)
->appendSegment($this->createPath($path))
->getUri();
}

return Modifier::from($baseUrl)
->appendSegment($this->prefixPath($path))
->removeEmptySegments()
return Modifier::from(Module::getInstance()->getConfig()->cdnBaseUrl)
->appendSegment($this->createBucketPath($path))
->getUri();
}

Expand Down Expand Up @@ -145,6 +141,7 @@
return array_merge(parent::settingsAttributes(), [
'expires',
'subpath',
'baseUrl',
'localFsPath',
'localFsUrl',
]);
Expand Down Expand Up @@ -180,7 +177,7 @@
return new AwsS3V3Adapter(
client: $this->getClient(),
bucket: $this->getBucketName(),
prefix: $this->prefixPath(),
prefix: $this->createBucketPath('')->toString(),
);
}

Expand All @@ -189,7 +186,22 @@
*/
protected function invalidateCdnPath(string $path): bool
{
return true;
if (!$this->hasUrls) {
return true;
}

try {
$prefix = StaticCache::CDN_PREFIX . Module::getInstance()->getConfig()->environmentId . ':';
$tag = StaticCacheTag::create($this->createBucketPath($path))
->minify(false)
->withPrefix($prefix);

Module::getInstance()->getStaticCache()->purgeTags($tag);

return true;
} catch (\Throwable $e) {
return false;
}
}

/**
Expand Down Expand Up @@ -227,26 +239,24 @@
]);
}

protected function getPrefix(): string
protected function createBucketPrefix(): HierarchicalPath
{
if ($this->useLocalFs) {
return '';
}

return HierarchicalPath::fromRelative(Module::getInstance()->getConfig()->environmentId)
->withoutEmptySegments()
->withoutTrailingSlash();
return HierarchicalPath::fromRelative(Module::getInstance()->getConfig()->environmentId);
}

public function prefixPath(string $path = ''): string
protected function createPath(string $path): HierarchicalPath
{
return HierarchicalPath::fromRelative(

Check failure on line 249 in src/fs/Fs.php

View workflow job for this annotation

GitHub Actions / Code Quality / PHPStan / PHPStan

Method craft\cloud\fs\Fs::createPath() should return League\Uri\Components\HierarchicalPath but returns League\Uri\Contracts\SegmentedPathInterface.
$this->getPrefix(),
$this->subpath ?? '',
$path,
)->withoutEmptySegments();
}

public function createBucketPath(string $path): HierarchicalPath
{
return $this->createBucketPrefix()->append($this->createPath($path));

Check failure on line 257 in src/fs/Fs.php

View workflow job for this annotation

GitHub Actions / Code Quality / PHPStan / PHPStan

Method craft\cloud\fs\Fs::createBucketPath() should return League\Uri\Components\HierarchicalPath but returns League\Uri\Contracts\SegmentedPathInterface.
}

public function getBucketName(): ?string
{
return Module::getInstance()->getConfig()->projectId;
Expand All @@ -269,7 +279,7 @@
[
'region' => Module::getInstance()->getConfig()->getRegion(),
'version' => 'latest',
'http_handler' => new GuzzleHandler(Craft::createGuzzleClient()),

Check failure on line 282 in src/fs/Fs.php

View workflow job for this annotation

GitHub Actions / Code Quality / PHPStan / PHPStan

Instantiated class Aws\Handler\GuzzleV6\GuzzleHandler not found.
'credentials' => $this->createCredentials(),
],
Module::getInstance()->getConfig()->getS3ClientOptions(),
Expand Down Expand Up @@ -308,7 +318,7 @@

$command = $this->getClient()->getCommand($command, [
'Bucket' => $this->getBucketName(),
'Key' => $this->prefixPath($path),
'Key' => $this->createBucketPath($path)->toString(),
] + $commandConfig);

$request = $this->getClient()->createPresignedRequest(
Expand Down
9 changes: 2 additions & 7 deletions src/fs/StorageFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@

class StorageFs extends Fs
{
public bool $hasUrls = false;

public function getPrefix(): string
public function createBucketPrefix(): HierarchicalPath
{
return HierarchicalPath::fromRelative(
parent::getPrefix(),
'storage',
)->withoutEmptySegments()->withoutTrailingSlash();
return parent::createBucketPrefix()->append('storage');

Check failure on line 11 in src/fs/StorageFs.php

View workflow job for this annotation

GitHub Actions / Code Quality / PHPStan / PHPStan

Method craft\cloud\fs\StorageFs::createBucketPrefix() should return League\Uri\Components\HierarchicalPath but returns League\Uri\Contracts\SegmentedPathInterface.
}
}
7 changes: 2 additions & 5 deletions src/fs/TmpFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

class TmpFs extends StorageFs
{
public function getPrefix(): string
public function createBucketPrefix(): HierarchicalPath
{
return HierarchicalPath::fromRelative(
parent::getPrefix(),
'tmp',
)->withoutEmptySegments()->withoutTrailingSlash();
return parent::createBucketPrefix()->append('tmp');

Check failure on line 11 in src/fs/TmpFs.php

View workflow job for this annotation

GitHub Actions / Code Quality / PHPStan / PHPStan

Method craft\cloud\fs\TmpFs::createBucketPrefix() should return League\Uri\Components\HierarchicalPath but returns League\Uri\Contracts\SegmentedPathInterface.
}
}
Loading
Loading