Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
feat(utils): Add constants for remote and local connections
Browse files Browse the repository at this point in the history
  • Loading branch information
darkterminal committed Apr 27, 2024
1 parent a9ae167 commit d17e740
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/Utils/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
define('LIBSQL_DESCRIBE', 'describe');
define('LIBSQL_STORE_SQL', 'store_sql');
define('LIBSQL_GET_AUTO_COMMIT', 'get_autocommit');

define('REMOTE', 'Remote');
define('LOCAL', 'Local');
49 changes: 35 additions & 14 deletions src/Utils/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ function expandConfig(array $config, bool $preferHttp): ExpandedConfig
}

// Extract configuration parameters
$flags = $config['flags'] ?? LIBSQLPHP_OPEN_READWRITE | LIBSQLPHP_OPEN_CREATE;
$encryptionKey = $config['encryptionKey'] ?? "";
$tls = $config['tls'] ?? false;
$authToken = $config['authToken'] ?? null;
$syncUrl = $config['syncUrl'] ?? null;
Expand All @@ -58,13 +60,30 @@ function expandConfig(array $config, bool $preferHttp): ExpandedConfig
// Check if the URL is for in-memory database
if ($config['url'] === ':memory:') {
return ExpandedConfig::create(
ExpandedScheme::file,
false,
null,
':memory:',
$authToken,
$syncUrl,
$syncInterval
scheme: ExpandedScheme::file,
flags: $flags,
encryptionKey: $encryptionKey,
tls: false,
authority: null,
path: ':memory:',
authToken: $authToken,
syncUrl: $syncUrl,
syncInterval: $syncInterval
);
}

$pattern = '/^file:.*\.db$/';
if (preg_match($pattern, $config['url'])) {
return ExpandedConfig::create(
scheme: ExpandedScheme::file,
flags: $flags,
encryptionKey: $encryptionKey,
tls: false,
authority: null,
path: $config['url'],
authToken: $authToken,
syncUrl: $syncUrl,
syncInterval: $syncInterval
);
}

Expand Down Expand Up @@ -127,13 +146,15 @@ function expandConfig(array $config, bool $preferHttp): ExpandedConfig

// Create and return the ExpandedConfig object
return new ExpandedConfig(
$scheme,
$tls ?? true,
$uri->authority,
$uri->path,
$authToken,
$syncUrl,
$syncInterval
scheme: $scheme,
flags: $flags,
encryptionKey: $encryptionKey,
tls: $tls ?? true,
authority: $uri->authority,
path: $uri->path,
authToken: $authToken,
syncUrl: $syncUrl,
syncInterval: $syncInterval
);
}

Expand Down

0 comments on commit d17e740

Please sign in to comment.