Skip to content

Commit

Permalink
Updating db provider to use laravel db class
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Nov 13, 2024
1 parent f1b1dd7 commit 401c8fb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
11 changes: 6 additions & 5 deletions app/Core/Configuration/laravelConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DB_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'url' => env('LEAN_DB_URL'),
'database' => env('LEAN_DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
'busy_timeout' => null,
Expand All @@ -430,7 +430,7 @@
],
'mysql' => [
'driver' => 'mysql',
'url' => env('DB_URL'),
'url' => env('LEAN_DB_URL'),
'host' => env('LEAN_DB_HOST', '127.0.0.1'),
'port' => env('LEAN_DB_PORT', '3306'),
'database' => env('LEAN_DB_DATABASE', 'laravel'),
Expand All @@ -441,10 +441,11 @@
'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'strict' => false,
'engine' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
PDO::ATTR_EMULATE_PREPARES => true,
]) : [],

],
Expand Down
47 changes: 20 additions & 27 deletions app/Core/Db/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Leantime\Core\Db;

use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB as dbFacade;
use Leantime\Core\Events\DispatchesEvents;
use PDO;

Expand All @@ -20,12 +21,12 @@ class Db extends DatabaseManager
private string $host = '';

/**
* @var string username for db
* @var string username for database
*/
private string $user = '';

/**
* @var string password for db
* @var string password for database
*/
private string $password = '';

Expand All @@ -45,47 +46,39 @@ class Db extends DatabaseManager
public PDO $database;

/**
* __construct - connect to database and select db
* @var ConnectionInterface Laravel database connection
*/
private ConnectionInterface $connection;

/**
* __construct - connect to database and select database
*
* @return void
*/
public function __construct($config = null)
public function __construct($connection = 'mysql')
{
if ($config == null) {
$config = app('config');
}

$this->user = $config->dbUser;
$this->password = $config->dbPassword;
$this->databaseName = $config->dbDatabase;
$this->host = $config->dbHost ?? '127.0.0.1';
$this->port = $config->dbPort ?? '3306';
// Get Laravel's database connection
$this->connection = dbFacade::connection($connection);

// Get the PDO connection from Laravel's connection
try {
$this->database = new PDO(
dsn: "mysql:host={$this->host};port={$this->port};dbname={$this->databaseName}",
username: $this->user,
password: $this->password,
options: [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4,sql_mode="NO_ENGINE_SUBSTITUTION"'],
);
$this->database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->database->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$this->database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

} catch (\PDOException $e) {
$this->database = $this->connection->getPdo();

Log::error("Can't connect to db");
Log::error($e);
} catch (\PDOException $e) {
\Log::error("Can't connect to database");

Check failure on line 70 in app/Core/Db/Db.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method error() on an unknown class Log.
\Log::error($e);

Check failure on line 71 in app/Core/Db/Db.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method error() on an unknown class Log.

exit('Cannot connect to database');
}
}

/**
* This function will generate a pdo binding string (":editors0,:editors1,:editors2,:editors3") to be used in a PDO
* This function will generate a PDO binding string (":editors0,:editors1,:editors2,:editors3") to be used in a PDO
* query that uses the IN() clause, to assist in proper PDO array bindings to avoid SQL injection.
*
* A counted for loop is user rather than foreach with a key to avoid issues if the array passed has any
* A counted for loop is used rather than foreach with a key to avoid issues if the array passed has any
* arbitrary keys
*/
public static function arrayToPdoBindingString(string $name, int $count): string
Expand Down
10 changes: 7 additions & 3 deletions app/Core/Providers/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Leantime\Core\Providers;

use Illuminate\Database\DatabaseServiceProvider;
use Illuminate\Support\ServiceProvider;

class Db extends ServiceProvider
class Db extends DatabaseServiceProvider
{
/**
* Register any application services.
Expand All @@ -13,7 +14,10 @@ class Db extends ServiceProvider
*/
public function register()
{
$this->app->singleton(\Leantime\Core\Db\Db::class, \Leantime\Core\Db\Db::class);
$this->app->alias(\Leantime\Core\Db\Db::class, 'db');

// Register Laravel's database service first
parent::register();

$this->app->singleton(\Leantime\Core\Db\Db::class);
}
}

0 comments on commit 401c8fb

Please sign in to comment.