Skip to content

Commit

Permalink
Fallbacks for artifact urls
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed Oct 18, 2023
1 parent 1a2fa4e commit 2f652c4
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 345 deletions.
5 changes: 1 addition & 4 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"**/*.php": [
"./vendor/bin/ecs check --ansi --fix",
"./vendor/bin/phpstan analyse"
],
"**/*.php": ["composer run fix-cs", "composer run phpstan analyse"],
"*": "prettier --ignore-unknown --write"
}
658 changes: 346 additions & 312 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use craft\config\BaseConfig;
use craft\helpers\App;
use craft\helpers\StringHelper;

/**
* @method array s3ClientOptions(array $options)
Expand Down Expand Up @@ -59,9 +58,4 @@ public function getRegion(): ?string
{
return $this->region ?? App::env('AWS_REGION');
}

public function getCdnBaseUrl(): string
{
return StringHelper::ensureRight($this->cdnBaseUrl, '/');
}
}
29 changes: 17 additions & 12 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Craft;
use craft\cloud\fs\BuildArtifactsFs;
use craft\cloud\fs\CpResourcesFs;
use craft\helpers\App;
use craft\helpers\ConfigHelper;

Expand All @@ -19,18 +18,9 @@ public static function isCraftCloud(): bool
return (bool)App::env('AWS_LAMBDA_RUNTIME_API') || App::env('LAMBDA_TASK_ROOT');
}

public static function artifactUrl(string $path = ''): ?string
public static function artifactUrl(string $path = ''): string
{
return Module::getInstance()->getConfig()->enableCdn
? (new BuildArtifactsFs())->createUrl($path)
: null;
}

public static function cpResourceUrl(string $path = ''): ?string
{
return Module::getInstance()->getConfig()->enableCdn
? (new CpResourcesFs())->createUrl($path)
: null;
return (new BuildArtifactsFs())->createUrl($path);
}

public static function setMemoryLimit(int|string $limit, int|string $offset = 0): int|float
Expand All @@ -41,4 +31,19 @@ public static function setMemoryLimit(int|string $limit, int|string $offset = 0)

return $memoryLimit;
}

public static function getComponents(): array
{
if (!Helper::isCraftCloud()) {
return [];
}

$components = [
'response' => [
'class' => \craft\cloud\web\Response::class,
],
];

return $components;
}
}
4 changes: 0 additions & 4 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ public function bootstrap($app): void
'fs' => Craft::createObject(CpResourcesFs::class),
]);

$app->getConfig()->getGeneral()->resourceBaseUrl(
Helper::cpResourceUrl(),
);

$app->getImages()->supportedImageFormats = ImageTransformer::SUPPORTED_IMAGE_FORMATS;

/**
Expand Down
1 change: 0 additions & 1 deletion src/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public function getFunctions(): array
{
return [
new TwigFunction('artifactUrl', [Helper::class, 'artifactUrl']),
new TwigFunction('cpResourceUrl', [Helper::class, 'cpResourceUrl']),
];
}
}
3 changes: 3 additions & 0 deletions src/fs/AssetsFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class AssetsFs extends Fs
{
public ?string $localFsPath = '@webroot/craft-cloud/{handle}';
public ?string $localFsUrl = '@web/craft-cloud/{handle}';

/**
* @inheritDoc
*/
Expand Down
8 changes: 8 additions & 0 deletions src/fs/BuildArtifactsFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

namespace craft\cloud\fs;

use craft\cloud\Module;
use League\Uri\Components\HierarchicalPath;

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

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

return HierarchicalPath::createRelativeFromSegments([
parent::getPrefix(),
'artifacts',
Expand Down
8 changes: 4 additions & 4 deletions src/fs/Fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
* @property-read string $prefix
* @property-read ?string $settingsHtml
*/
class Fs extends FlysystemFs
abstract class Fs extends FlysystemFs
{
protected static bool $showUrlSetting = false;
protected ?string $expires = null;
protected ?Local $localFs = null;
protected S3Client $client;
public ?string $subpath = null;
public ?string $localFsPath = '@webroot/craft-cloud/{handle}';
public ?string $localFsUrl = '@web/craft-cloud/{handle}';
public ?string $localFsPath = null;
public ?string $localFsUrl = null;
public ?string $url = '__URL__';

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ public function getRootUrl(): ?string
public function createUrl(string $path = ''): string
{
$baseUrl = Module::getInstance()->getConfig()->enableCdn
? Module::getInstance()->getConfig()->getCdnBaseUrl()
? Module::getInstance()->getConfig()->cdnBaseUrl
: $this->getLocalFs()->getRootUrl();

if (!$baseUrl) {
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Bref\FpmRuntime\FpmHandler;
use Bref\Runtime\LambdaRuntime;
use craft\cloud\runtime\event\EventHandler;
use RuntimeException;
use Throwable;

/**
Expand All @@ -28,15 +29,15 @@ public static function run(): void
$appRoot = getenv('LAMBDA_TASK_ROOT');
$handlerFile = $appRoot . '/' . getenv('_HANDLER');
if (!is_file($handlerFile)) {
$lambdaRuntime->failInitialization("Handler `$handlerFile` doesn't exist");
$lambdaRuntime->failInitialization("Handler `$handlerFile` doesn't exist", 'Runtime.NoSuchHandler');
}

// use the Bref fpm handler to start php-fpm
$phpFpm = new FpmHandler($handlerFile);
try {
$phpFpm->start();
} catch (Throwable $e) {
$lambdaRuntime->failInitialization('Error while starting PHP-FPM', $e);
$lambdaRuntime->failInitialization(new RuntimeException('Error while starting PHP-FPM: ' . $e->getMessage(), 0, $e));
}

// create our own event handler and pass the fpm handler to it
Expand Down

0 comments on commit 2f652c4

Please sign in to comment.