feat: add ScalewayRuntime resolving the Front Controller with a writable dir fallbackUnverified
When var/ is not writable, APP_CACHE_DIR and APP_LOG_DIR move to the temp dir while APP_BUILD_DIR keeps the container warmed at build time.
c5937dd parent: 17f12fd added
src/ScalewayRuntime.php +66 -0 | new file mode 100644 | ||
| @@ -0,0 +1,66 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace JDecool\ScalewayRuntime; | |
| 6 | + | |
| 7 | +use JDecool\ScalewayRuntime\Runner\HttpKernelRunner; | |
| 8 | +use JDecool\ScalewayRuntime\Runner\InvocationRunner; | |
| 9 | +use Symfony\Component\HttpKernel\HttpKernelInterface; | |
| 10 | +use Symfony\Component\Runtime\SymfonyRuntime; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Runs Symfony Applications on Scaleway Serverless Functions. | |
| 14 | + * | |
| 15 | + * On top of the options managed by SymfonyRuntime, it accepts: | |
| 16 | + * - "front_controller": the Front Controller path, relative to "project_dir"; defaults to "public/index.php". | |
| 17 | + * | |
| 18 | + * When the project's var/ directory is not writable (the deployed code may be read-only), | |
| 19 | + * the cache and log dirs move to the system temp dir through APP_CACHE_DIR and APP_LOG_DIR, | |
| 20 | + * while APP_BUILD_DIR keeps the container warmed at build time. | |
| 21 | + */ | |
| 22 | +class ScalewayRuntime extends SymfonyRuntime | |
| 23 | +{ | |
| 24 | + /** | |
| 25 | + * @param array<mixed> $options see SymfonyRuntime, plus "front_controller" | |
| 26 | + */ | |
| 27 | + public function __construct(array $options = []) | |
| 28 | + { | |
| 29 | + parent::__construct($options); // @phpstan-ignore argument.type (options come from untyped config) | |
| 30 | + | |
| 31 | + $projectDir = $this->options['project_dir'] ?? null; | |
| 32 | + if (\is_string($projectDir) && !is_writable(is_dir($projectDir.'/var') ? $projectDir.'/var' : $projectDir)) { | |
| 33 | + // Kernels using MicroKernelTrait read these from $_SERVER only; values set by the app win. | |
| 34 | + $_SERVER['APP_BUILD_DIR'] ??= $projectDir.'/var/cache'; | |
| 35 | + $_SERVER['APP_CACHE_DIR'] ??= sys_get_temp_dir().'/cache'; | |
| 36 | + $_SERVER['APP_LOG_DIR'] ??= sys_get_temp_dir().'/log'; | |
| 37 | + } | |
| 38 | + } | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Builds the Application from the Front Controller and picks the runner matching its type. | |
| 42 | + */ | |
| 43 | + public function createInvocationRunner(): InvocationRunner | |
| 44 | + { | |
| 45 | + $projectDir = $this->options['project_dir'] ?? null; | |
| 46 | + $frontController = $this->options['front_controller'] ?? 'public/index.php'; | |
| 47 | + if (!\is_string($projectDir) || !\is_string($frontController)) { | |
| 48 | + throw new \LogicException('The "project_dir" and "front_controller" runtime options must be strings.'); | |
| 49 | + } | |
| 50 | + | |
| 51 | + $file = $projectDir.'/'.$frontController; | |
| 52 | + // A static closure keeps $this and our locals out of the Front Controller's scope. | |
| 53 | + $app = (static fn (): mixed => require $file)(); | |
| 54 | + if (!\is_callable($app)) { | |
| 55 | + throw new \TypeError(\sprintf('Invalid return value: callable expected, "%s" returned from "%s".', get_debug_type($app), $file)); | |
| 56 | + } | |
| 57 | + | |
| 58 | + [$app, $args] = $this->getResolver($app)->resolve(); | |
| 59 | + $application = $app(...$args); | |
| 60 | + | |
| 61 | + return match (true) { | |
| 62 | + $application instanceof HttpKernelInterface => new HttpKernelRunner($application), | |
| 63 | + default => throw new \LogicException(\sprintf('"%s" cannot run Applications of type "%s".', static::class, get_debug_type($application))), | |
| 64 | + }; | |
| 65 | + } | |
| 66 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +namespace JDecool\ScalewayRuntime; | ||
| 6 | + | ||
| 7 | +use JDecool\ScalewayRuntime\Runner\HttpKernelRunner; | ||
| 8 | +use JDecool\ScalewayRuntime\Runner\InvocationRunner; | ||
| 9 | +use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
| 10 | +use Symfony\Component\Runtime\SymfonyRuntime; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * Runs Symfony Applications on Scaleway Serverless Functions. | ||
| 14 | + * | ||
| 15 | + * On top of the options managed by SymfonyRuntime, it accepts: | ||
| 16 | + * - "front_controller": the Front Controller path, relative to "project_dir"; defaults to "public/index.php". | ||
| 17 | + * | ||
| 18 | + * When the project's var/ directory is not writable (the deployed code may be read-only), | ||
| 19 | + * the cache and log dirs move to the system temp dir through APP_CACHE_DIR and APP_LOG_DIR, | ||
| 20 | + * while APP_BUILD_DIR keeps the container warmed at build time. | ||
| 21 | + */ | ||
| 22 | +class ScalewayRuntime extends SymfonyRuntime | ||
| 23 | +{ | ||
| 24 | + /** | ||
| 25 | + * @param array<mixed> $options see SymfonyRuntime, plus "front_controller" | ||
| 26 | + */ | ||
| 27 | + public function __construct(array $options = []) | ||
| 28 | + { | ||
| 29 | + parent::__construct($options); // @phpstan-ignore argument.type (options come from untyped config) | ||
| 30 | + | ||
| 31 | + $projectDir = $this->options['project_dir'] ?? null; | ||
| 32 | + if (\is_string($projectDir) && !is_writable(is_dir($projectDir.'/var') ? $projectDir.'/var' : $projectDir)) { | ||
| 33 | + // Kernels using MicroKernelTrait read these from $_SERVER only; values set by the app win. | ||
| 34 | + $_SERVER['APP_BUILD_DIR'] ??= $projectDir.'/var/cache'; | ||
| 35 | + $_SERVER['APP_CACHE_DIR'] ??= sys_get_temp_dir().'/cache'; | ||
| 36 | + $_SERVER['APP_LOG_DIR'] ??= sys_get_temp_dir().'/log'; | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Builds the Application from the Front Controller and picks the runner matching its type. | ||
| 42 | + */ | ||
| 43 | + public function createInvocationRunner(): InvocationRunner | ||
| 44 | + { | ||
| 45 | + $projectDir = $this->options['project_dir'] ?? null; | ||
| 46 | + $frontController = $this->options['front_controller'] ?? 'public/index.php'; | ||
| 47 | + if (!\is_string($projectDir) || !\is_string($frontController)) { | ||
| 48 | + throw new \LogicException('The "project_dir" and "front_controller" runtime options must be strings.'); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + $file = $projectDir.'/'.$frontController; | ||
| 52 | + // A static closure keeps $this and our locals out of the Front Controller's scope. | ||
| 53 | + $app = (static fn (): mixed => require $file)(); | ||
| 54 | + if (!\is_callable($app)) { | ||
| 55 | + throw new \TypeError(\sprintf('Invalid return value: callable expected, "%s" returned from "%s".', get_debug_type($app), $file)); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + [$app, $args] = $this->getResolver($app)->resolve(); | ||
| 59 | + $application = $app(...$args); | ||
| 60 | + | ||
| 61 | + return match (true) { | ||
| 62 | + $application instanceof HttpKernelInterface => new HttpKernelRunner($application), | ||
| 63 | + default => throw new \LogicException(\sprintf('"%s" cannot run Applications of type "%s".', static::class, get_debug_type($application))), | ||
| 64 | + }; | ||
| 65 | + } | ||
| 66 | +} | ||
added
tests/Fixtures/front-controllers/not-callable.php +5 -0 | new file mode 100644 | ||
| @@ -0,0 +1,5 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +return 'not a closure'; | |
| new file mode 100644 | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +return 'not a closure'; | ||
added
tests/Fixtures/front-controllers/recording.php +7 -0 | new file mode 100644 | ||
| @@ -0,0 +1,7 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +use JDecool\ScalewayRuntime\Tests\Fixtures\RecordingKernel; | |
| 6 | + | |
| 7 | +return static fn (): RecordingKernel => RecordingKernel::build(); | |
| new file mode 100644 | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +use JDecool\ScalewayRuntime\Tests\Fixtures\RecordingKernel; | ||
| 6 | + | ||
| 7 | +return static fn (): RecordingKernel => RecordingKernel::build(); | ||
added
tests/Fixtures/front-controllers/unsupported.php +5 -0 | new file mode 100644 | ||
| @@ -0,0 +1,5 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +return static fn (): stdClass => new stdClass(); | |
| new file mode 100644 | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +return static fn (): stdClass => new stdClass(); | ||
added
tests/ScalewayRuntimeTest.php +79 -0 | new file mode 100644 | ||
| @@ -0,0 +1,79 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace JDecool\ScalewayRuntime\Tests; | |
| 6 | + | |
| 7 | +use JDecool\ScalewayRuntime\ScalewayRuntime; | |
| 8 | +use PHPUnit\Framework\TestCase; | |
| 9 | + | |
| 10 | +final class ScalewayRuntimeTest extends TestCase | |
| 11 | +{ | |
| 12 | + /** @var array<mixed> */ | |
| 13 | + private array $server; | |
| 14 | + private string $projectDir; | |
| 15 | + | |
| 16 | + protected function setUp(): void | |
| 17 | + { | |
| 18 | + $this->server = $_SERVER; | |
| 19 | + unset($_SERVER['APP_BUILD_DIR'], $_SERVER['APP_CACHE_DIR'], $_SERVER['APP_LOG_DIR']); | |
| 20 | + $this->projectDir = sys_get_temp_dir().'/scaleway-runtime-'.bin2hex(random_bytes(4)); | |
| 21 | + mkdir($this->projectDir.'/var', 0o777, true); | |
| 22 | + } | |
| 23 | + | |
| 24 | + protected function tearDown(): void | |
| 25 | + { | |
| 26 | + $_SERVER = $this->server; | |
| 27 | + chmod($this->projectDir, 0o777); | |
| 28 | + if (is_dir($this->projectDir.'/var')) { | |
| 29 | + chmod($this->projectDir.'/var', 0o777); | |
| 30 | + rmdir($this->projectDir.'/var'); | |
| 31 | + } | |
| 32 | + rmdir($this->projectDir); | |
| 33 | + } | |
| 34 | + | |
| 35 | + public function testWritableProjectKeepsItsDirs(): void | |
| 36 | + { | |
| 37 | + $this->createRuntime(); | |
| 38 | + | |
| 39 | + self::assertArrayNotHasKey('APP_CACHE_DIR', $_SERVER); | |
| 40 | + self::assertArrayNotHasKey('APP_BUILD_DIR', $_SERVER); | |
| 41 | + self::assertArrayNotHasKey('APP_LOG_DIR', $_SERVER); | |
| 42 | + } | |
| 43 | + | |
| 44 | + public function testReadOnlyVarDirFallsBackToTempDirAndKeepsTheWarmedBuildDir(): void | |
| 45 | + { | |
| 46 | + chmod($this->projectDir.'/var', 0o555); | |
| 47 | + | |
| 48 | + $this->createRuntime(); | |
| 49 | + | |
| 50 | + self::assertSame($this->projectDir.'/var/cache', $_SERVER['APP_BUILD_DIR']); | |
| 51 | + self::assertSame(sys_get_temp_dir().'/cache', $_SERVER['APP_CACHE_DIR']); | |
| 52 | + self::assertSame(sys_get_temp_dir().'/log', $_SERVER['APP_LOG_DIR']); | |
| 53 | + } | |
| 54 | + | |
| 55 | + public function testReadOnlyProjectWithoutVarDirFallsBack(): void | |
| 56 | + { | |
| 57 | + rmdir($this->projectDir.'/var'); | |
| 58 | + chmod($this->projectDir, 0o555); | |
| 59 | + | |
| 60 | + $this->createRuntime(); | |
| 61 | + | |
| 62 | + self::assertSame(sys_get_temp_dir().'/cache', $_SERVER['APP_CACHE_DIR']); | |
| 63 | + } | |
| 64 | + | |
| 65 | + public function testDirsSetByTheAppWin(): void | |
| 66 | + { | |
| 67 | + chmod($this->projectDir.'/var', 0o555); | |
| 68 | + $_SERVER['APP_CACHE_DIR'] = '/custom/cache'; | |
| 69 | + | |
| 70 | + $this->createRuntime(); | |
| 71 | + | |
| 72 | + self::assertSame('/custom/cache', $_SERVER['APP_CACHE_DIR']); | |
| 73 | + } | |
| 74 | + | |
| 75 | + private function createRuntime(): void | |
| 76 | + { | |
| 77 | + new ScalewayRuntime(['project_dir' => $this->projectDir, 'error_handler' => false, 'debug' => false]); | |
| 78 | + } | |
| 79 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +namespace JDecool\ScalewayRuntime\Tests; | ||
| 6 | + | ||
| 7 | +use JDecool\ScalewayRuntime\ScalewayRuntime; | ||
| 8 | +use PHPUnit\Framework\TestCase; | ||
| 9 | + | ||
| 10 | +final class ScalewayRuntimeTest extends TestCase | ||
| 11 | +{ | ||
| 12 | + /** @var array<mixed> */ | ||
| 13 | + private array $server; | ||
| 14 | + private string $projectDir; | ||
| 15 | + | ||
| 16 | + protected function setUp(): void | ||
| 17 | + { | ||
| 18 | + $this->server = $_SERVER; | ||
| 19 | + unset($_SERVER['APP_BUILD_DIR'], $_SERVER['APP_CACHE_DIR'], $_SERVER['APP_LOG_DIR']); | ||
| 20 | + $this->projectDir = sys_get_temp_dir().'/scaleway-runtime-'.bin2hex(random_bytes(4)); | ||
| 21 | + mkdir($this->projectDir.'/var', 0o777, true); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + protected function tearDown(): void | ||
| 25 | + { | ||
| 26 | + $_SERVER = $this->server; | ||
| 27 | + chmod($this->projectDir, 0o777); | ||
| 28 | + if (is_dir($this->projectDir.'/var')) { | ||
| 29 | + chmod($this->projectDir.'/var', 0o777); | ||
| 30 | + rmdir($this->projectDir.'/var'); | ||
| 31 | + } | ||
| 32 | + rmdir($this->projectDir); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public function testWritableProjectKeepsItsDirs(): void | ||
| 36 | + { | ||
| 37 | + $this->createRuntime(); | ||
| 38 | + | ||
| 39 | + self::assertArrayNotHasKey('APP_CACHE_DIR', $_SERVER); | ||
| 40 | + self::assertArrayNotHasKey('APP_BUILD_DIR', $_SERVER); | ||
| 41 | + self::assertArrayNotHasKey('APP_LOG_DIR', $_SERVER); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public function testReadOnlyVarDirFallsBackToTempDirAndKeepsTheWarmedBuildDir(): void | ||
| 45 | + { | ||
| 46 | + chmod($this->projectDir.'/var', 0o555); | ||
| 47 | + | ||
| 48 | + $this->createRuntime(); | ||
| 49 | + | ||
| 50 | + self::assertSame($this->projectDir.'/var/cache', $_SERVER['APP_BUILD_DIR']); | ||
| 51 | + self::assertSame(sys_get_temp_dir().'/cache', $_SERVER['APP_CACHE_DIR']); | ||
| 52 | + self::assertSame(sys_get_temp_dir().'/log', $_SERVER['APP_LOG_DIR']); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + public function testReadOnlyProjectWithoutVarDirFallsBack(): void | ||
| 56 | + { | ||
| 57 | + rmdir($this->projectDir.'/var'); | ||
| 58 | + chmod($this->projectDir, 0o555); | ||
| 59 | + | ||
| 60 | + $this->createRuntime(); | ||
| 61 | + | ||
| 62 | + self::assertSame(sys_get_temp_dir().'/cache', $_SERVER['APP_CACHE_DIR']); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + public function testDirsSetByTheAppWin(): void | ||
| 66 | + { | ||
| 67 | + chmod($this->projectDir.'/var', 0o555); | ||
| 68 | + $_SERVER['APP_CACHE_DIR'] = '/custom/cache'; | ||
| 69 | + | ||
| 70 | + $this->createRuntime(); | ||
| 71 | + | ||
| 72 | + self::assertSame('/custom/cache', $_SERVER['APP_CACHE_DIR']); | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + private function createRuntime(): void | ||
| 76 | + { | ||
| 77 | + new ScalewayRuntime(['project_dir' => $this->projectDir, 'error_handler' => false, 'debug' => false]); | ||
| 78 | + } | ||
| 79 | +} | ||