mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 11:44:04 +00:00
add DisallowSqliteAttach feature
This commit is contained in:
parent
613ab5bbc8
commit
9bb06afc57
10 changed files with 275 additions and 1 deletions
63
src/Features/DisallowSqliteAttach.php
Normal file
63
src/Features/DisallowSqliteAttach.php
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Features;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Database\Connectors\ConnectionFactory;
|
||||
use Illuminate\Database\SQLiteConnection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use PDO;
|
||||
use Stancl\Tenancy\Contracts\Feature;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
class DisallowSqliteAttach implements Feature
|
||||
{
|
||||
protected static bool|null $loadExtensionSupported = null;
|
||||
public static string|false|null $extensionPath = null;
|
||||
|
||||
public function bootstrap(Tenancy $tenancy): void
|
||||
{
|
||||
// Handle any already resolved connections
|
||||
foreach (DB::getConnections() as $connection) {
|
||||
if ($connection instanceof SQLiteConnection) {
|
||||
if (! $this->loadExtension($connection->getPdo())) return;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the change to all sqlite connections resolved in the future
|
||||
DB::extend('sqlite', function ($config, $name) {
|
||||
$conn = app(ConnectionFactory::class)->make($config, $name);
|
||||
$this->loadExtension($conn->getPdo());
|
||||
|
||||
return $conn;
|
||||
});
|
||||
}
|
||||
|
||||
protected function loadExtension(PDO $pdo): bool
|
||||
{
|
||||
if (static::$loadExtensionSupported === null) {
|
||||
static::$loadExtensionSupported = method_exists($pdo, 'loadExtension');
|
||||
}
|
||||
|
||||
if (static::$loadExtensionSupported === false) return false;
|
||||
|
||||
$suffix = match (PHP_OS_FAMILY) {
|
||||
'Linux' => '.so',
|
||||
'Windows' => '.dll',
|
||||
'Darwin' => '.dylib',
|
||||
default => throw new Exception("The DisallowSqliteAttach feature doesn't support your operating system: " . PHP_OS_FAMILY),
|
||||
};
|
||||
|
||||
$arch = php_uname('m');
|
||||
$arm = $arch === 'aarch64' || $arch === 'arm64';
|
||||
|
||||
static::$extensionPath ??= realpath(base_path('vendor/stancl/tenancy/src/extensions/lib/' . ($arm ? 'arm/' : '') . 'noattach' . $suffix));
|
||||
if (static::$extensionPath === false) return false;
|
||||
|
||||
$pdo->loadExtension(static::$extensionPath);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue