Skip to content

Commit 401c8fb

Browse files
committed
Updating db provider to use laravel db class
1 parent f1b1dd7 commit 401c8fb

File tree

3 files changed

+33
-35
lines changed

3 files changed

+33
-35
lines changed

app/Core/Configuration/laravelConfig.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@
420420
'connections' => [
421421
'sqlite' => [
422422
'driver' => 'sqlite',
423-
'url' => env('DB_URL'),
424-
'database' => env('DB_DATABASE', database_path('database.sqlite')),
423+
'url' => env('LEAN_DB_URL'),
424+
'database' => env('LEAN_DB_DATABASE', database_path('database.sqlite')),
425425
'prefix' => '',
426426
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
427427
'busy_timeout' => null,
@@ -430,7 +430,7 @@
430430
],
431431
'mysql' => [
432432
'driver' => 'mysql',
433-
'url' => env('DB_URL'),
433+
'url' => env('LEAN_DB_URL'),
434434
'host' => env('LEAN_DB_HOST', '127.0.0.1'),
435435
'port' => env('LEAN_DB_PORT', '3306'),
436436
'database' => env('LEAN_DB_DATABASE', 'laravel'),
@@ -441,10 +441,11 @@
441441
'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
442442
'prefix' => '',
443443
'prefix_indexes' => true,
444-
'strict' => true,
445-
'engine' => null,
444+
'strict' => false,
445+
'engine' => 'InnoDB',
446446
'options' => extension_loaded('pdo_mysql') ? array_filter([
447447
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
448+
PDO::ATTR_EMULATE_PREPARES => true,
448449
]) : [],
449450

450451
],

app/Core/Db/Db.php

+20-27
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Leantime\Core\Db;
44

5+
use Illuminate\Database\ConnectionInterface;
56
use Illuminate\Database\DatabaseManager;
6-
use Illuminate\Support\Facades\Log;
7+
use Illuminate\Support\Facades\DB as dbFacade;
78
use Leantime\Core\Events\DispatchesEvents;
89
use PDO;
910

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

2223
/**
23-
* @var string username for db
24+
* @var string username for database
2425
*/
2526
private string $user = '';
2627

2728
/**
28-
* @var string password for db
29+
* @var string password for database
2930
*/
3031
private string $password = '';
3132

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

4748
/**
48-
* __construct - connect to database and select db
49+
* @var ConnectionInterface Laravel database connection
50+
*/
51+
private ConnectionInterface $connection;
52+
53+
/**
54+
* __construct - connect to database and select database
4955
*
5056
* @return void
5157
*/
52-
public function __construct($config = null)
58+
public function __construct($connection = 'mysql')
5359
{
54-
if ($config == null) {
55-
$config = app('config');
56-
}
5760

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

64+
// Get the PDO connection from Laravel's connection
6465
try {
65-
$this->database = new PDO(
66-
dsn: "mysql:host={$this->host};port={$this->port};dbname={$this->databaseName}",
67-
username: $this->user,
68-
password: $this->password,
69-
options: [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4,sql_mode="NO_ENGINE_SUBSTITUTION"'],
70-
);
71-
$this->database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
72-
$this->database->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
73-
$this->database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
7466

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

77-
Log::error("Can't connect to db");
78-
Log::error($e);
69+
} catch (\PDOException $e) {
70+
\Log::error("Can't connect to database");
71+
\Log::error($e);
7972

8073
exit('Cannot connect to database');
8174
}
8275
}
8376

8477
/**
85-
* This function will generate a pdo binding string (":editors0,:editors1,:editors2,:editors3") to be used in a PDO
78+
* This function will generate a PDO binding string (":editors0,:editors1,:editors2,:editors3") to be used in a PDO
8679
* query that uses the IN() clause, to assist in proper PDO array bindings to avoid SQL injection.
8780
*
88-
* A counted for loop is user rather than foreach with a key to avoid issues if the array passed has any
81+
* A counted for loop is used rather than foreach with a key to avoid issues if the array passed has any
8982
* arbitrary keys
9083
*/
9184
public static function arrayToPdoBindingString(string $name, int $count): string

app/Core/Providers/Db.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Leantime\Core\Providers;
44

5+
use Illuminate\Database\DatabaseServiceProvider;
56
use Illuminate\Support\ServiceProvider;
67

7-
class Db extends ServiceProvider
8+
class Db extends DatabaseServiceProvider
89
{
910
/**
1011
* Register any application services.
@@ -13,7 +14,10 @@ class Db extends ServiceProvider
1314
*/
1415
public function register()
1516
{
16-
$this->app->singleton(\Leantime\Core\Db\Db::class, \Leantime\Core\Db\Db::class);
17-
$this->app->alias(\Leantime\Core\Db\Db::class, 'db');
17+
18+
// Register Laravel's database service first
19+
parent::register();
20+
21+
$this->app->singleton(\Leantime\Core\Db\Db::class);
1822
}
1923
}

0 commit comments

Comments
 (0)