diff --git a/src/Illuminate/Foundation/Cloud.php b/src/Illuminate/Foundation/Cloud.php index d954bf34b00f..2bd1dffafc31 100644 --- a/src/Illuminate/Foundation/Cloud.php +++ b/src/Illuminate/Foundation/Cloud.php @@ -25,6 +25,7 @@ public static function bootstrapperBootstrapped(Application $app, string $bootst { (match ($bootstrapper) { LoadConfiguration::class => function () use ($app) { + static::configureDisks($app); static::configureUnpooledPostgresConnection($app); static::ensureMigrationsUseUnpooledConnection($app); }, @@ -36,7 +37,38 @@ public static function bootstrapperBootstrapped(Application $app, string $bootst } /** - * Adjust the database configuration for pooled Laravel Postgres. + * Configure the Laravel Cloud disks if applicable. + */ + public static function configureDisks(Application $app): void + { + if (! isset($_SERVER['LARAVEL_CLOUD_DISK_CONFIG'])) { + return; + } + + $disks = json_decode($_SERVER['LARAVEL_CLOUD_DISK_CONFIG'], true); + + foreach ($disks as $disk) { + $app['config']->set('filesystems.disks.'.$disk['disk'], [ + 'driver' => 's3', + 'key' => $disk['access_key_id'], + 'secret' => $disk['access_key_secret'], + 'bucket' => $disk['bucket'], + 'url' => $disk['url'], + 'endpoint' => $disk['endpoint'], + 'region' => 'auto', + 'use_path_style_endpoint' => false, + 'throw' => false, + 'report' => false, + ]); + + if ($disk['is_default'] ?? false) { + $app['config']->set('filesystems.default', $disk['disk']); + } + } + } + + /** + * Configure the unpooled Laravel Postgres connection if applicable. */ public static function configureUnpooledPostgresConnection(Application $app): void { diff --git a/tests/Integration/Foundation/CloudTest.php b/tests/Integration/Foundation/CloudTest.php index 99ff0e75347a..f2711fd85e2f 100644 --- a/tests/Integration/Foundation/CloudTest.php +++ b/tests/Integration/Foundation/CloudTest.php @@ -23,4 +23,37 @@ public function test_it_can_resolve_core_container_aliases() 'password' => 'test-password', ], $this->app['config']->get('database.connections.pgsql-unpooled')); } + + public function test_it_can_configure_disks() + { + $_SERVER['LARAVEL_CLOUD_DISK_CONFIG'] = json_encode( + [ + [ + 'disk' => 'test-disk', + 'access_key_id' => 'test-access-key-id', + 'access_key_secret' => 'test-access-key-secret', + 'bucket' => 'test-bucket', + 'url' => 'test-url', + 'endpoint' => 'test-endpoint', + 'is_default' => false, + ], + [ + 'disk' => 'test-disk-2', + 'access_key_id' => 'test-access-key-id-2', + 'access_key_secret' => 'test-access-key-secret-2', + 'bucket' => 'test-bucket-2', + 'url' => 'test-url-2', + 'endpoint' => 'test-endpoint-2', + 'is_default' => true, + ], + ] + ); + + Cloud::configureDisks($this->app); + + $this->assertEquals('test-disk-2', $this->app['config']->get('filesystems.default')); + $this->assertEquals('test-access-key-id', $this->app['config']->get('filesystems.disks.test-disk.key')); + + unset($_SERVER['LARAVEL_CLOUD_DISK_CONFIG']); + } }