Skip to content

Commit

Permalink
Support project relative paths to sqlite databases regardless of webr…
Browse files Browse the repository at this point in the history
…oot (#191)
  • Loading branch information
mjauvin authored Oct 16, 2024
1 parent a37c1f9 commit 752c162
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Database/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ protected function createPdoResolverWithHosts(array $config)
};
}

/**
* Create a connector instance based on the configuration.
*
* @param array $config
* @return \Illuminate\Database\Connectors\ConnectorInterface
*
* @throws \InvalidArgumentException
*/
public function createConnector(array $config)
{
if (array_get($config, 'driver') === 'sqlite') {
return new SQLiteConnector;
} else {
return parent::createConnector($config);
}
}

/**
* Create a new connection instance.
*
Expand Down
43 changes: 43 additions & 0 deletions src/Database/Connectors/SQLiteConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Winter\Storm\Database\Connectors;

use Illuminate\Database\Connectors\SQLiteConnector as BaseSQLiteConnector;
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;

class SQLiteConnector extends BaseSQLiteConnector
{
/**
* Establish a database connection.
*
* @param array $config
* @return \PDO
*
* @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException
*/
public function connect(array $config)
{
$options = $this->getOptions($config);

// SQLite supports "in-memory" databases that only last as long as the owning
// connection does. These are useful for tests or for short lifetime store
// querying. In-memory databases may only have a single open connection.
if ($config['database'] === ':memory:') {
return $this->createConnection('sqlite::memory:', $config, $options);
}

$path = realpath($config['database']);
if (!file_exists($path)) {
$path = realpath(base_path($config['database']));
}

// Here we'll verify that the SQLite database exists before going any further
// as the developer probably wants to know if the database exists and this
// SQLite driver will not throw any exception if it does not by default.
if ($path === false) {
throw new SQLiteDatabaseDoesNotExistException($config['database']);
}

return $this->createConnection("sqlite:{$path}", $config, $options);
}
}

0 comments on commit 752c162

Please sign in to comment.