8000 add cloud class · laravel/framework@a39f4db · GitHub
[go: up one dir, main page]

Skip to content

Commit a39f4db

Browse files
committed
add cloud class
1 parent eb0be33 commit a39f4db

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

src/Illuminate/Foundation/Cloud.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation;
4+
5+
use Illuminate\Database\Migrations\Migrator;
6+
use Illuminate\Foundation\Bootstrap\HandleExceptions;
7+
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
8+
use Monolog\Formatter\JsonFormatter;
9+
use Monolog\Handler\SocketHandler;
10+
11+
class Cloud
12+
{
13+
/**
14+
* Handle a bootstrapper that is bootstrapping.
15+
*/
16+
public static function bootstrapperBootstrapping(Application $app, string $bootstrapper): void
17+
{
18+
//
19+
}
20+
21+
/**
22+
* Handle a bootstrapper that has bootstrapped.
23+
*/
24+
public static function bootstrapperBootstrapped(Application $app, string $bootstrapper): void
25+
{
26+
(match ($bootstrapper) {
27+
LoadConfiguration::class => function () use ($app) {
28+
static::configureDisks($app);
29+
static::configureUnpooledPostgresConnection($app);
30+
static::ensureMigrationsUseUnpooledConnection($app);
31+
},
32+
HandleExceptions::class => function () use ($app) {
33+
static::configureCloudLogging($app);
34+
},
35+
default => fn () => true,
36+
})();
37+
}
38+
39+
/**
40+
* Configure the Laravel Cloud disks if applicable.
41+
*/
42+
public static function configureDisks(Application $app): void
43+
{
44+
if (! isset($_SERVER['LARAVEL_CLOUD_DISK_CONFIG'])) {
45+
return;
46+
}
47+
48+
$disks = json_decode($_SERVER['LARAVEL_CLOUD_DISK_CONFIG'], true);
49+
50+
foreach ($disks as $disk) {
51+
$app['config']->set('filesystems.disks.'.$disk['disk'], [
52+
'driver' => 's3',
53+
'key' => $disk['access_key_id'],
54+
'secret' => $disk['access_key_secret'],
55+
'bucket' => $disk['bucket'],
56+
'url' => $disk['url'],
57+
'endpoint' => $disk['endpoint'],
58+
'region' => 'auto',
59+
'use_path_style_endpoint' => false,
60+
'throw' => false,
61+
'report' => false,
62+
]);
63+
64+
if ($disk['is_default'] ?? false) {
65+
$app['config']->set('filesystems.default', $disk['disk']);
66+
}
67+
}
68+
}
69+
70+
/**
71+
* Configure the unpooled Laravel Postgres connection if applicable.
72+
*/
73+
public static function configureUnpooledPostgresConnection(Application $app): void
74+
{
75+
$host = $app['config']->get('database.connections.pgsql.host', '');
76+
77+
if (str_contains($host, 'pg.laravel.cloud') &&
78+
str_contains($host, '-pooler')) {
79+
$app['config']->set(
80+
'database.connections.pgsql-unpooled',
81+
array_merge($app['config']->get('database.connections.pgsql'), [
82+
'host' => str_replace('-pooler', '', $host),
83+
])
84+
);
85+
}
86+
}
87+
88+
/**
89+
* Ensure that migrations use the unpooled Postgres connection if applicable.
90+
*/
91+
public static function ensureMigrationsUseUnpooledConnection(Application $app): void
92+
{
93+
if (! is_array($app['config']->get('database.connections.pgsql-unpooled'))) {
94+
return;
95+
}
96+
97+
Migrator::resolveConnectionsUsing(function ($resolver, $connection) use ($app) {
98+
$connection = $connection ?? $app['config']->get('database.default');
99+
100+
return $resolver->connection(
101+
$connection === 'pgsql' ? 'pgsql-unpooled' : $connection
102+
);
103+
});
104+
}
105+
106+
/**
107+
* Configure the Laravel Cloud log channels.
108+
*/
109+
public static function configureCloudLogging(Application $app): void
110+
{
111+
$app['config']->set('logging.channels.stderr.formatter_with', [
112+
'includeStacktraces' => true,
113+
]);
114+
115+
$app['config']->set('logging.channels.laravel-cloud-socket', [
116+
'driver' => 'monolog',
117+
'handler' => SocketHandler::class,
118+
'formatter' => JsonFormatter::class,
119+
'formatter_with' => [
120+
'includeStacktraces' => true,
121+
],
122+
'with' => [
123+
'connectionString' => $_ENV['LARAVEL_CLOUD_LOG_SOCKET'] ??
124+
$_SERVER['LARAVEL_CLOUD_LOG_SOCKET'] ??
125+
'unix:///tmp/cloud-init.sock',
126+
'persistent' => true,
127+
],
128+
]);
129+
}
130+
}

0 commit comments

Comments
 (0)
0