PHP code example of progalaxyelabs / stonescriptdb-gateway-client
1. Go to this page and download the library: Download progalaxyelabs/stonescriptdb-gateway-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
progalaxyelabs / stonescriptdb-gateway-client example snippets
toneScriptDB\GatewayClient;
// Connect to gateway
$client = new GatewayClient(
gateway_url: 'http://localhost:9000',
platform: 'myapp'
);
// Call a PostgreSQL function
$users = $client->callFunction('get_users', [
'limit' => 10
]);
foreach ($users as $user) {
echo $user['username'] . "\n";
}
use StoneScriptDB\GatewayClient;
$client = new GatewayClient('http://gateway:9000', 'myapp');
// Simple function call
$result = $client->callFunction('get_product', [
'product_id' => 123
]);
// Multi-parameter function
$orders = $client->callFunction('search_orders', [
'customer_id' => 456,
'status' => 'completed',
'limit' => 50
]);
$client = new GatewayClient(
gateway_url: 'http://gateway:9000',
platform: 'saas-platform',
tenant_id: 'acme-corp'
);
// All calls use tenant_id automatically
$data = $client->callFunction('get_tenant_data', []);
// Switch tenant dynamically
$client->setTenantId('globex-inc');
$data = $client->callFunction('get_tenant_data', []);
$client = new GatewayClient('http://gateway:9000', 'myapp');
// Register schema on deployment
$response = $client->register('/path/to/schema.tar.gz');
echo "Migrations applied: " . $response['migrations_applied'] . "\n";
echo "Functions deployed: " . $response['functions_deployed'] . "\n";
// Update running production schema
$response = $client->migrate('/path/to/schema.tar.gz');
echo "Databases updated: " . count($response['databases_updated']) . "\n";
if ($client->healthCheck()) {
echo "Gateway is healthy\n";
} else {
echo "Gateway unavailable: " . $client->getLastError() . "\n";
}
use StoneScriptDB\GatewayClient;
use StoneScriptDB\GatewayException;
$client = new GatewayClient('http://gateway:9000', 'myapp');
try {
$result = $client->callFunction('risky_operation', [
'amount' => 5000
]);
} catch (GatewayException $e) {
error_log("Gateway error: " . $e->getMessage());
// HTTP status code available
if ($e->getCode() === 500) {
// Server error
} elseif ($e->getCode() === 404) {
// Function not found
}
}
$client = new GatewayClient('http://gateway:9000', 'myapp');
// Custom timeouts
$client->setTimeout(60) // Request timeout (seconds)
->setConnectTimeout(15) // Connection timeout
->setDebug(true); // Enable debug logging
// Call function
$result = $client->callFunction('long_running_operation', []);
use StoneScriptDB\Auth\TokenValidator;
use StoneScriptDB\Auth\InvalidTokenException;
// Validate tokens from auth service
$validator = new TokenValidator('http://auth-service:3139');
try {
$claims = $validator->validateToken($jwtToken);
echo "User ID: " . $claims->sub . "\n";
echo "Email: " . $claims->email . "\n";
echo "Tenant ID: " . $claims->tenantId . "\n";
echo "Role: " . $claims->role . "\n";
echo "Expires: " . date('Y-m-d H:i:s', $claims->exp) . "\n";
} catch (InvalidTokenException $e) {
echo "Invalid token: " . $e->getMessage() . "\n";
}
// config/database.php
'connections' => [
'gateway' => [
'driver' => 'stonescriptdb',
'url' => env('DB_GATEWAY_URL', 'http://localhost:9000'),
'platform' => env('DB_GATEWAY_PLATFORM', 'myapp'),
],
],
// app/Services/DatabaseService.php
use StoneScriptDB\GatewayClient;
class DatabaseService
{
private GatewayClient $client;
public function __construct()
{
$this->client = new GatewayClient(
config('database.connections.gateway.url'),
config('database.connections.gateway.platform')
);
}
public function getUsers(int $limit = 10): array
{
return $this->client->callFunction('get_users', compact('limit'));
}
}
// app/Config/Database.php
public array $gateway = [
'gateway_url' => 'http://localhost:9000',
'platform' => 'myapp',
];
// app/Models/UserModel.php
use StoneScriptDB\GatewayClient;
class UserModel extends BaseModel
{
private GatewayClient $client;
public function __construct()
{
$config = config('Database')->gateway;
$this->client = new GatewayClient(
$config['gateway_url'],
$config['platform']
);
}
public function findUser(int $id): ?array
{
$result = $this->client->callFunction('get_user', ['id' => $id]);
return $result[0] ?? null;
}
}
// config.php
define('GATEWAY_URL', 'http://gateway:9000');
define('PLATFORM', 'myapp');
// database.php
use StoneScriptDB\GatewayClient;
function getDatabase(): GatewayClient
{
static $client = null;
if ($client === null) {
$client = new GatewayClient(GATEWAY_URL, PLATFORM);
}
return $client;
}
// index.php
$db = getDatabase();
$products = $db->callFunction('get_products', ['category' => 'electronics']);
new GatewayClient(string $gateway_url, ?string $platform = null, ?string $tenant_id = null)
try {
$client->callFunction('invalid_function', []);
} catch (GatewayException $e) {
echo $e->getMessage(); // Error description
echo $e->getCode(); // HTTP status code (if applicable)
}
new TokenValidator(string $auth_service_url)