From dcb29a3c8d0c11367c7449e6339072053f96c428 Mon Sep 17 00:00:00 2001 From: Kevin Dees Date: Thu, 4 Aug 2022 09:30:34 -0400 Subject: [PATCH] add multi database connection system. --- typerocket-v5.php | 4 +- typerocket/composer.lock | 12 ++-- typerocket/config/database.php | 12 ++-- typerocket/vendor/composer/installed.json | 14 ++-- typerocket/vendor/composer/installed.php | 10 +-- .../core/src/Core/ApplicationKernel.php | 13 +--- .../core/src/Database/Connection.php | 67 ++++++++++++++++--- .../typerocket/core/src/Database/Query.php | 19 +++++- .../typerocket/core/src/Models/Model.php | 29 +++++++- 9 files changed, 127 insertions(+), 53 deletions(-) diff --git a/typerocket-v5.php b/typerocket-v5.php index 82e3c9e6..95fe962c 100644 --- a/typerocket-v5.php +++ b/typerocket-v5.php @@ -3,7 +3,7 @@ Plugin Name: TypeRocket - Andromeda Plugin URI: https://typerocket.com/ Description: TypeRocket is a framework that joins refined UI elements and modern programming architecture together. -Version: 5.1.2 +Version: 5.1.3 Requires at least: 6.0 Requires PHP: 7.4 Author: TypeRocket @@ -34,7 +34,7 @@ public function __construct() return; } - define('TYPEROCKET_PLUGIN_VERSION', '5.1.2'); + define('TYPEROCKET_PLUGIN_VERSION', '5.1.3'); define('TYPEROCKET_PLUGIN_INSTALL', __DIR__); if(!defined('TYPEROCKET_ROOT_WP')) diff --git a/typerocket/composer.lock b/typerocket/composer.lock index 04e2eb4f..a178c511 100644 --- a/typerocket/composer.lock +++ b/typerocket/composer.lock @@ -883,16 +883,16 @@ }, { "name": "typerocket/core", - "version": "v5.1.7", + "version": "v5.1.8", "source": { "type": "git", "url": "https://github.com/TypeRocket/core.git", - "reference": "d73b97feb257481b61f56733ce60bf2b27c9a4e9" + "reference": "4543e51a1d3331a0fb5ce8005cbe9374f9bfb04f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/TypeRocket/core/zipball/d73b97feb257481b61f56733ce60bf2b27c9a4e9", - "reference": "d73b97feb257481b61f56733ce60bf2b27c9a4e9", + "url": "https://api.github.com/repos/TypeRocket/core/zipball/4543e51a1d3331a0fb5ce8005cbe9374f9bfb04f", + "reference": "4543e51a1d3331a0fb5ce8005cbe9374f9bfb04f", "shasum": "" }, "require": { @@ -933,9 +933,9 @@ "support": { "docs": "https://typerocket.com/docs/v5/", "issues": "https://github.com/TypeRocket/core/issues", - "source": "https://github.com/TypeRocket/core/tree/v5.1.7" + "source": "https://github.com/TypeRocket/core/tree/v5.1.8" }, - "time": "2022-08-04T03:45:11+00:00" + "time": "2022-08-04T13:22:18+00:00" }, { "name": "woocommerce/action-scheduler", diff --git a/typerocket/config/database.php b/typerocket/config/database.php index 56674131..7e80ef1d 100644 --- a/typerocket/config/database.php +++ b/typerocket/config/database.php @@ -6,8 +6,8 @@ |-------------------------------------------------------------------------- | | This option defines the default database driver that is used when a query - | or model is instantiated. See the list of connections below for more - | details or add your own to the list. There is one by default. + | or model is instantiated. See the list of drivers below the available + | options. You can add your own to the list. | */ 'default' => typerocket_env('TYPEROCKET_DATABASE_DEFAULT', 'wp'), @@ -28,10 +28,10 @@ 'alt' => [ 'driver' => '\TypeRocket\Database\Connectors\CoreDatabaseConnector', - 'username' => '', - 'password' => '', - 'database' => '', - 'host' => '', + 'username' => typerocket_env('TYPEROCKET_ALT_DATABASE_USER'), + 'password' => typerocket_env('TYPEROCKET_ALT_DATABASE_PASSWORD'), + 'database' => typerocket_env('TYPEROCKET_ALT_DATABASE_DATABASE'), + 'host' => typerocket_env('TYPEROCKET_ALT_DATABASE_HOST'), ], ] ]; \ No newline at end of file diff --git a/typerocket/vendor/composer/installed.json b/typerocket/vendor/composer/installed.json index 5bec44a6..f13c18ff 100644 --- a/typerocket/vendor/composer/installed.json +++ b/typerocket/vendor/composer/installed.json @@ -2963,17 +2963,17 @@ }, { "name": "typerocket/core", - "version": "v5.1.7", - "version_normalized": "5.1.7.0", + "version": "v5.1.8", + "version_normalized": "5.1.8.0", "source": { "type": "git", "url": "https://github.com/TypeRocket/core.git", - "reference": "d73b97feb257481b61f56733ce60bf2b27c9a4e9" + "reference": "4543e51a1d3331a0fb5ce8005cbe9374f9bfb04f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/TypeRocket/core/zipball/d73b97feb257481b61f56733ce60bf2b27c9a4e9", - "reference": "d73b97feb257481b61f56733ce60bf2b27c9a4e9", + "url": "https://api.github.com/repos/TypeRocket/core/zipball/4543e51a1d3331a0fb5ce8005cbe9374f9bfb04f", + "reference": "4543e51a1d3331a0fb5ce8005cbe9374f9bfb04f", "shasum": "" }, "require": { @@ -2987,7 +2987,7 @@ "require-dev": { "phpunit/phpunit": "^9.5" }, - "time": "2022-08-04T03:45:11+00:00", + "time": "2022-08-04T13:22:18+00:00", "type": "library", "installation-source": "dist", "autoload": { @@ -3016,7 +3016,7 @@ "support": { "docs": "https://typerocket.com/docs/v5/", "issues": "https://github.com/TypeRocket/core/issues", - "source": "https://github.com/TypeRocket/core/tree/v5.1.7" + "source": "https://github.com/TypeRocket/core/tree/v5.1.8" }, "install-path": "../typerocket/core" }, diff --git a/typerocket/vendor/composer/installed.php b/typerocket/vendor/composer/installed.php index 2a00bedd..735c09ac 100644 --- a/typerocket/vendor/composer/installed.php +++ b/typerocket/vendor/composer/installed.php @@ -5,7 +5,7 @@ 'type' => 'library', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), - 'reference' => 'abb534c072b1f482af046364cbe0df6dfdc54ec4', + 'reference' => '15bc9611510e96bd92a4deda27e23a414fa1c8a7', 'name' => 'typerocket/typerocket', 'dev' => true, ), @@ -404,12 +404,12 @@ 'dev_requirement' => true, ), 'typerocket/core' => array( - 'pretty_version' => 'v5.1.7', - 'version' => '5.1.7.0', + 'pretty_version' => 'v5.1.8', + 'version' => '5.1.8.0', 'type' => 'library', 'install_path' => __DIR__ . '/../typerocket/core', 'aliases' => array(), - 'reference' => 'd73b97feb257481b61f56733ce60bf2b27c9a4e9', + 'reference' => '4543e51a1d3331a0fb5ce8005cbe9374f9bfb04f', 'dev_requirement' => false, ), 'typerocket/typerocket' => array( @@ -418,7 +418,7 @@ 'type' => 'library', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), - 'reference' => 'abb534c072b1f482af046364cbe0df6dfdc54ec4', + 'reference' => '15bc9611510e96bd92a4deda27e23a414fa1c8a7', 'dev_requirement' => false, ), 'webmozart/assert' => array( diff --git a/typerocket/vendor/typerocket/core/src/Core/ApplicationKernel.php b/typerocket/vendor/typerocket/core/src/Core/ApplicationKernel.php index c0573076..25128879 100644 --- a/typerocket/vendor/typerocket/core/src/Core/ApplicationKernel.php +++ b/typerocket/vendor/typerocket/core/src/Core/ApplicationKernel.php @@ -94,18 +94,7 @@ public function boot() }, RuntimeCache::ALIAS); Container::singleton(Connection::class, function() { - - $default = Config::getFromContainer()->locate('database.default'); - $config = null; - - if(is_null($default)) { - $default = 'wp'; - $config = [ - 'driver' => WordPressCoreDatabaseConnector::class - ]; - } - - return (new Connection)->addFromConfig($default, $config); + return Connection::initDefault(); }, Connection::ALIAS); Container::singleton(AuthUser::class, function() { diff --git a/typerocket/vendor/typerocket/core/src/Database/Connection.php b/typerocket/vendor/typerocket/core/src/Database/Connection.php index e79493c9..8cbe9e94 100644 --- a/typerocket/vendor/typerocket/core/src/Database/Connection.php +++ b/typerocket/vendor/typerocket/core/src/Database/Connection.php @@ -5,6 +5,7 @@ use TypeRocket\Core\Config; use TypeRocket\Core\Container; use TypeRocket\Database\Connectors\DatabaseConnector; +use TypeRocket\Database\Connectors\WordPressCoreDatabaseConnector; class Connection { @@ -13,7 +14,7 @@ class Connection /** * @var array */ - protected $connections = []; + protected array $connections = []; /** * @return array @@ -57,23 +58,45 @@ public function addFromConfig(string $name, ?array $config = null) } /** - * @param $name + * @param string $name * @return \wpdb */ - public function get($name) + public function getOrAddFromConfig(string $name, ?array $config = null) { if(!array_key_exists($name, $this->connections)) { - $this->addFromConfig($name); + $this->addFromConfig($name, $config); } return $this->connections[$name]; } /** - * @param $name + * @param string $name + * @return \wpdb + */ + public function get(string $name, ?string $fallback = null) + { + if(!array_key_exists($name, $this->connections) && is_null($fallback)) { + $this->addFromConfig($name); + } + + return $this->connections[$name] ?? $this->connections[$fallback]; + } + + /** + * @param string $name + * @return bool + */ + public function exists(string $name) : bool + { + return array_key_exists($name, $this->connections); + } + + /** + * @param string $name * @return $this */ - public function close($name) + public function close(string $name) { if(array_key_exists($name, $this->connections)) { $this->connections[$name]->close(); @@ -86,10 +109,34 @@ public function close($name) /** * @return \wpdb */ - public static function getDefault() + public function default() { - $driver = Config::getFromContainer()->locate('database.default'); - return static::getFromContainer()->get($driver); + $default = Config::getFromContainer()->locate('database.default'); + return $this->get($default); + } + + /** + * @return static + */ + public static function initDefault() + { + $default = Config::getFromContainer()->locate('database.default'); + $connection = new static; + + if(!$connection->exists($default)) { + $config = null; + + if(is_null($default)) { + $default = 'wp'; + $config = [ + 'driver' => WordPressCoreDatabaseConnector::class + ]; + } + + $connection->addFromConfig($default, $config); + } + + return $connection; } /** @@ -97,6 +144,6 @@ public static function getDefault() */ public static function getFromContainer() { - return Container::resolve(static::ALIAS); + return Container::resolve(static::ALIAS, true); } } \ No newline at end of file diff --git a/typerocket/vendor/typerocket/core/src/Database/Query.php b/typerocket/vendor/typerocket/core/src/Database/Query.php index 0ec87e64..00c9c1ed 100644 --- a/typerocket/vendor/typerocket/core/src/Database/Query.php +++ b/typerocket/vendor/typerocket/core/src/Database/Query.php @@ -18,7 +18,7 @@ class Query protected $selectTable = null; protected $joinAs = null; protected $tableAs = null; - protected string $connection = 'wp'; + protected ?string $connection = null; protected ?\wpdb $wpdb = null; /** @@ -30,7 +30,7 @@ class Query */ public function __construct($table = null, $selectTable = null, $idColumn = null) { - $wpdb = Connection::getFromContainer()->get($this->connection); + $wpdb = $this->establishConnection(); if(is_string($table)) { $this->query['table'] = $table; @@ -47,6 +47,21 @@ public function __construct($table = null, $selectTable = null, $idColumn = null } } + /** + * @return \wpdb + */ + protected function establishConnection() + { + $connection = Connection::getFromContainer(); + $name = $this->connection; + + if(!is_string($name) || !$connection->exists($name)) { + return $connection->default(); + } + + return $connection->get($name); + } + /** * Merge * diff --git a/typerocket/vendor/typerocket/core/src/Models/Model.php b/typerocket/vendor/typerocket/core/src/Models/Model.php index 44c19e58..5634c4b9 100644 --- a/typerocket/vendor/typerocket/core/src/Models/Model.php +++ b/typerocket/vendor/typerocket/core/src/Models/Model.php @@ -218,9 +218,9 @@ class Model implements Formable, JsonSerializable protected $dataCache = []; /** - * @var string name of connection from database drivers config list + * @var string|null name of connection from database drivers config list */ - protected string $connection = 'wp'; + protected $connection = null; /** * Construct Model based on resource @@ -228,7 +228,7 @@ class Model implements Formable, JsonSerializable */ public function __construct() { - $wpdb = Connection::getFromContainer()->get($this->connection); + $wpdb = $this->establishConnection(); try { $type = (new ReflectionClass( $this ))->getShortName(); @@ -252,6 +252,29 @@ public function __construct() $this->init(); } + /** + * @return \wpdb + */ + protected function establishConnection() + { + $connection = Connection::getFromContainer(); + $name = $this->connection; + + if(!is_string($name) || !$connection->exists($name)) { + return $connection->default(); + } + + return $connection->get($name); + } + + /** + * @return string + */ + public function getConnection() + { + return $this->connection; + } + /** * @param \wpdb $wpdb * @return Query