Skip to content

Instantly share code, notes, and snippets.

@darkterminal
Last active June 17, 2024 10:05
Show Gist options
  • Save darkterminal/b7f979c273930535557249541623dc3a to your computer and use it in GitHub Desktop.
Save darkterminal/b7f979c273930535557249541623dc3a to your computer and use it in GitHub Desktop.
Turso Client PHP - libSQL Extension for PHP (Script Installer)
<?php
$REPO = "https://raw.githubusercontent.com/tursodatabase/turso-client-php/main/release_metadata.json";
$OS = strtolower(php_uname('s'));
$ARCH = php_uname('m');
$HOME = trim(shell_exec('echo $HOME'));
$minimal_version = "8.3";
$binary_name = "liblibsql_php.so";
$destination = "$HOME/.turso-client-php";
$herd_path = "$HOME/Library/Application Support/Herd";
function slugify($text)
{
$text = preg_replace('~[^\pL\d]+~u', '_', $text);
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = preg_replace('~[^-\w]+~', '', $text);
$text = trim($text, '_');
$text = preg_replace('~-+~', '_', $text);
$text = strtolower($text);
if (empty($text)) {
return 'n_a';
}
return $text;
}
function download_extextion()
{
global $REPO, $OS, $ARCH, $HOME, $destination;
$is_dir_exists = false;
if (is_dir($destination)) {
$is_dir_exists = true;
$search_string = "libsql";
if (is_dir($destination)) {
$dir = opendir($destination);
if ($dir) {
$found = false;
while (($file = readdir($dir)) !== false) {
if (strpos($file, $search_string) !== false) {
$found = true;
break;
}
}
closedir($dir);
if ($found) {
echo "Turso Client is Ready!\n";
} else {
echo "Extension is not found!\n";
if (!$is_dir_exists) {
shell_exec("mkdir $destination");
}
}
} else {
echo "Failed to open directory $destination\n";
exit(1);
}
} else {
echo "$destination is not a valid directory\n";
exit(1);
}
} else {
shell_exec("mkdir $destination");
}
echo "Detected OS: $OS\n";
echo "Detected architecture: $ARCH\n";
echo "Home Directory: $HOME\n";
switch ($OS) {
case 'darwin':
if ($ARCH == "x86_64") {
$SUFFIX = "x86_64-apple-darwin";
} else if ($ARCH == "arm64") {
$SUFFIX = "aarch64-apple-darwin";
} else {
echo "Unsupported architecture: $ARCH for Darwin\n";
exit(1);
}
break;
case 'linux':
if ($ARCH == "x86_64") {
$SUFFIX = "x86_64-unknown-linux-gnu";
} else {
echo "Unsupported architecture: $ARCH for Linux\n";
exit(1);
}
break;
default:
echo "Unsupported OS: $OS\n";
exit(1);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $REPO);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: darkterminal"]);
$RELEASE_DATA = curl_exec($ch);
curl_close($ch);
$release = json_decode($RELEASE_DATA, true);
$assets = $release['assets'];
$download_url = null;
foreach ($assets as $asset) {
if (strpos($asset['name'], $SUFFIX) !== false) {
$download_url = $asset['browser_download_url'];
}
}
if ($download_url === null) {
echo "Download URL is not found!" . PHP_EOL;
exit(1);
}
$output_file = basename($download_url);
shell_exec("curl -L $download_url -o $output_file");
sleep(2);
shell_exec("tar -xzf $output_file");
sleep(2);
$directory = str_replace('.tar.gz', '', $output_file);
shell_exec("mv $directory/* $destination/");
shell_exec("rm $output_file");
rmdir($directory);
echo "Downloaded release asset to $output_file" . PHP_EOL;
}
$php_version_checker = fn (string $version) => version_compare(PHP_VERSION, $version, '>=');
$is_windows = fn () => PHP_OS_FAMILY === 'Windows';
if ($is_windows()) {
$message = <<<WINDOWS_ERR
Sorry, Turso Database is only support for Linux and MacOS.
You are using Windows, you try our alternative using Dev Containers
visit: https://github.com/darkterminal/turso-docker-php
Thank you!
WINDOWS_ERR;
echo $message . PHP_EOL;
exit(1);
}
if (!$php_version_checker($minimal_version)) {
echo "Oops! Your PHP version environment does not meet the requirements.\n";
echo "Need a minimal PHP $minimal_version installed on your environment.\n";
exit(1);
}
if (!function_exists('shell_exec') && !function_exists('curl_version')) {
$message = <<<ERR_FUNC_NOT_FOUND
It looks like the 'shell_exec' and 'curl_version' functions are disabled in your PHP environment. These functions are essential for this script to work properly.
To enable them, follow these steps:
1. Open your 'php.ini' file. You can find the location of your 'php.ini' file by running the command 'php --ini' in your terminal or command prompt.
2. Search for 'disable_functions' directive. It might look something like this:
disable_functions = shell_exec, curl_version
3. Remove 'shell_exec' and 'curl_version' from this list. It should look like:
disable_functions =
4. Save the 'php.ini' file.
5. Restart your web server for the changes to take effect. If you are using Apache, you can restart it with:
sudo service apache2 restart
or for Nginx:
sudo service nginx restart
If you are using a web hosting service, you might need to contact your hosting provider to enable these functions for you.
For more information on 'shell_exec', visit: https://www.php.net/manual/en/function.shell-exec.php
For more information on 'curl_version', visit: https://www.php.net/manual/en/function.curl-version.php
Thank you!
ERR_FUNC_NOT_FOUND;
echo $message . PHP_EOL;
exit(1);
}
// Is Laravel Herd?
if (is_dir($herd_path)) {
echo "Your're using Laravel Herd\n";
echo "Sorry, Laravel Herd is not supported yet.\n";
exit(0);
}
$search_libsql = shell_exec('php -m | grep libsql');
$search_result = $search_libsql ? trim($search_libsql) : null;
if (!empty($search_result)) {
echo "Turso Client PHP is already installed and configured!\n";
exit(0);
}
echo "Turso need to install the client extension in your PHP environment.\n";
echo "This script will ask your sudo password to modify your php.ini file:\n";
$answers = readline("Are you ok? [y/N]: ");
if (strtolower(trim($answers)) !== 'y') {
echo "Ok... no problem, see you later!\n";
exit(0);
}
$php_ini_file = shell_exec('php --ini');
$lines = explode("\n", $php_ini_file);
$lines = array_slice($lines, 0, 3);
$config_ini = [];
foreach ($lines as $line) {
if (strpos($line, ':') !== false) {
list($key, $value) = array_map('trim', explode(':', $line, 2));
$config_ini[slugify($key)] = $value;
}
}
if (empty($config_ini['loaded_configuration_file'])) {
echo "You don't have PHP install globaly in your environment\n";
echo "Turso Client PHP lookup php.ini file and it's not found\n";
exit(1);
}
echo "Downloading...\n";
download_extextion();
echo "Your extension is already downloaded!\n";
echo "store at $destination.\n";
$module_file = "extension=$destination/$binary_name";
$php_ini = $config_ini['loaded_configuration_file'];
shell_exec("sudo -k");
echo "Please enter your sudo password: ";
shell_exec('stty -echo');
$sudoPassword = trim(fgets(STDIN));
shell_exec('stty echo');
echo "\n\n";
$command = "echo '$sudoPassword' | sudo -S bash -c 'echo \"$module_file\" >> $php_ini' && sudo -k";
shell_exec($command);
echo "\n\n";
$finish_message = <<<FINISH_MESSAGE
TURSO CLIENT PHP SUCCESSFULLY INSTALLED!
To get extension class autocompletion you need to modify your IDE Settings
in this case VSCode Settings:
- Open your VSCode setting (cmd/ctrl+,) then search "intelephense.stubs"
- add this: $destination value on the lists
Thank you for using Turso Database!
FINISH_MESSAGE;
echo $finish_message . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment