Skip to content

Commit bb0072e

Browse files
wip
1 parent b1f194e commit bb0072e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/Util/EnvSetup.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Supabase\Util;
6+
7+
use Dotenv\Dotenv;
8+
9+
class EnvSetup
10+
{
11+
public static function env(string $path): mixed
12+
{
13+
// If the env vars are set and not empty use them
14+
$apiKey = getenv('SB_API_KEY');
15+
$url = getenv('SB_URL');
16+
17+
// else check try to load the .env file in the $path
18+
if (empty($apiKey) || empty($url)) {
19+
$loaded = Dotenv::createArrayBacked($path)->safeLoad();
20+
if (array_key_exists('SB_API_KEY', $loaded)) {
21+
$apiKey = $loaded['SB_API_KEY'];
22+
}
23+
24+
if (array_key_exists('SB_URL', $loaded)) {
25+
$url = $loaded['SB_URL'];
26+
}
27+
}
28+
29+
if (empty($apiKey)) {
30+
throw new \Exception('Could not load API_KEY');
31+
}
32+
33+
if (empty($url)) {
34+
throw new \Exception('Could not load URL');
35+
}
36+
37+
return [
38+
'SB_API_KEY' => $apiKey,
39+
'SB_URL' => $url,
40+
];
41+
}
42+
}

0 commit comments

Comments
 (0)