Skip to content

Commit 80a164c

Browse files
Dependency Update
Removed the hard dependency for `DatabaseFactory\Config` when creating a new `Builder` instance and added the `BaseConfigInterface` as the dependency instead.
1 parent 23563ce commit 80a164c

File tree

3 files changed

+37
-52
lines changed

3 files changed

+37
-52
lines changed

src/Builder.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace DatabaseFactory {
44

5-
use DatabaseFactory\Helpers;
6-
use DatabaseFactory\Facades;
7-
use DatabaseFactory\Contracts;
8-
use DatabaseFactory\Exceptions;
5+
use DatabaseFactory\Exceptions;
6+
use DatabaseFactory\Contracts;
7+
use DatabaseFactory\Helpers;
8+
use DatabaseFactory\Facades;
99

1010
/**
1111
* The main Query Builder class. Objects initialized from this
@@ -89,7 +89,10 @@ class Builder
8989
* @param string $table Database table to transact with
9090
* @param string $config Config class for custom queries
9191
*/
92-
public function __construct(private readonly string $table, private readonly string $config)
92+
public function __construct(
93+
private readonly string $table,
94+
private readonly Contracts\BaseConfigInterface $config
95+
)
9396
{
9497
// connection string
9598
$this->connection = Facades\DB::connection();
@@ -121,7 +124,7 @@ public function __call(string $module = null, mixed $arguments = null): Builder
121124
}
122125

123126
// if it does, let's make it the current module
124-
$currentModule = $this->modules[$module] = (new $this->config())->modules()[$module];
127+
$currentModule = $this->modules[$module] = $this->config->modules()[$module];
125128

126129
// then, we'll see if that module extends the base builder
127130
if (!Helpers\Cls::extends($currentModule, Modules\BaseBuilder::class)) {

src/Facades/DB.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,8 @@ public static function query(string $sql): array|false
6464
*
6565
* @return \DatabaseFactory\Builder
6666
*/
67-
public static function table(string $table, string $config = \DatabaseFactory\Config::class): Builder
67+
public static function table(string $table, Contracts\BaseConfigInterface $config): Builder
6868
{
69-
// does the config class implement the BaseConfigInterface?
70-
if (!Helpers\Cls::implements($config, Contracts\BaseConfigInterface::class)) {
71-
// if not, throw a new QueryBuilderException
72-
throw new Exceptions\QueryBuilderException(
73-
'The config file must implement ' . Contracts\BaseConfigInterface::class
74-
);
75-
}
76-
77-
// next, does it extend the BaseConfig class?
78-
if (
79-
!Helpers\Cls::equals($config, \DatabaseFactory\Config::class) &&
80-
!Helpers\Cls::extends($config, \DatabaseFactory\Config::class)
81-
) {
82-
// if not, throw a new QueryBuilderException
83-
throw new Exceptions\QueryBuilderException(
84-
'The config file must extend ' . \DatabaseFactory\Config::class
85-
);
86-
}
87-
8869
// if validation passes, return a new query builder instance
8970
return (new Builder($table, $config));
9071
}

src/Helper.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
<?php
22

3-
use DatabaseFactory\Config;
3+
use DatabaseFactory\Contracts;
44
use DatabaseFactory\Facades;
55
use DatabaseFactory\Builder;
6+
use DatabaseFactory\Config;
67

78
// If the function doesn't exist, let's create it!
8-
if (! function_exists('db_factory')) {
9-
/**
10-
* Returns a database factory instance
11-
*
12-
* @param string $table
13-
* @param string $config
14-
*
15-
* @return \DatabaseFactory\Builder
16-
*/
17-
function db_factory(string $table, string $config = DatabaseFactory\Config::class): Builder
18-
{
19-
return Facades\DB::table($table, $config);
20-
}
9+
if (!function_exists('db_factory')) {
10+
/**
11+
* Returns a database factory instance
12+
*
13+
* @param string $table
14+
* @param Contracts\BaseConfigInterface $config
15+
*
16+
* @return Builder
17+
*/
18+
function db_factory(string $table, Contracts\BaseConfigInterface $config = null): Builder
19+
{
20+
return Facades\DB::table($table, $config ?? new Config());
21+
}
2122
}
2223

2324
// If the function doesn't exist, let's create it!
24-
if (! function_exists('dump')) {
25-
/**
26-
* Data dump
27-
*
28-
* @param $data
29-
*
30-
* @return void
31-
*/
32-
function dump($data): void
33-
{
34-
Facades\Debug::dump($data);
35-
}
25+
if (!function_exists('dump')) {
26+
/**
27+
* Data dump
28+
*
29+
* @param $data
30+
*
31+
* @return void
32+
*/
33+
function dump($data): void
34+
{
35+
Facades\Debug::dump($data);
36+
}
3637
}

0 commit comments

Comments
 (0)