From 80a164c483aa7f51c3bfafd9bb48e91bc855c438 Mon Sep 17 00:00:00 2001 From: jason-napolitano Date: Tue, 29 Aug 2023 13:21:23 -0700 Subject: [PATCH] Dependency Update Removed the hard dependency for `DatabaseFactory\Config` when creating a new `Builder` instance and added the `BaseConfigInterface` as the dependency instead. --- src/Builder.php | 15 +++++++------ src/Facades/DB.php | 21 +----------------- src/Helper.php | 53 +++++++++++++++++++++++----------------------- 3 files changed, 37 insertions(+), 52 deletions(-) diff --git a/src/Builder.php b/src/Builder.php index d452a6f..89c27bf 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -2,10 +2,10 @@ namespace DatabaseFactory { - use DatabaseFactory\Helpers; - use DatabaseFactory\Facades; - use DatabaseFactory\Contracts; - use DatabaseFactory\Exceptions; + use DatabaseFactory\Exceptions; + use DatabaseFactory\Contracts; + use DatabaseFactory\Helpers; + use DatabaseFactory\Facades; /** * The main Query Builder class. Objects initialized from this @@ -89,7 +89,10 @@ class Builder * @param string $table Database table to transact with * @param string $config Config class for custom queries */ - public function __construct(private readonly string $table, private readonly string $config) + public function __construct( + private readonly string $table, + private readonly Contracts\BaseConfigInterface $config + ) { // connection string $this->connection = Facades\DB::connection(); @@ -121,7 +124,7 @@ public function __call(string $module = null, mixed $arguments = null): Builder } // if it does, let's make it the current module - $currentModule = $this->modules[$module] = (new $this->config())->modules()[$module]; + $currentModule = $this->modules[$module] = $this->config->modules()[$module]; // then, we'll see if that module extends the base builder if (!Helpers\Cls::extends($currentModule, Modules\BaseBuilder::class)) { diff --git a/src/Facades/DB.php b/src/Facades/DB.php index bb8a951..1f15cd0 100644 --- a/src/Facades/DB.php +++ b/src/Facades/DB.php @@ -64,27 +64,8 @@ public static function query(string $sql): array|false * * @return \DatabaseFactory\Builder */ - public static function table(string $table, string $config = \DatabaseFactory\Config::class): Builder + public static function table(string $table, Contracts\BaseConfigInterface $config): Builder { - // does the config class implement the BaseConfigInterface? - if (!Helpers\Cls::implements($config, Contracts\BaseConfigInterface::class)) { - // if not, throw a new QueryBuilderException - throw new Exceptions\QueryBuilderException( - 'The config file must implement ' . Contracts\BaseConfigInterface::class - ); - } - - // next, does it extend the BaseConfig class? - if ( - !Helpers\Cls::equals($config, \DatabaseFactory\Config::class) && - !Helpers\Cls::extends($config, \DatabaseFactory\Config::class) - ) { - // if not, throw a new QueryBuilderException - throw new Exceptions\QueryBuilderException( - 'The config file must extend ' . \DatabaseFactory\Config::class - ); - } - // if validation passes, return a new query builder instance return (new Builder($table, $config)); } diff --git a/src/Helper.php b/src/Helper.php index 80e411f..fd73726 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -1,36 +1,37 @@