Skip to content

Commit

Permalink
add multi database connection system.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindees committed Aug 4, 2022
1 parent 15bc961 commit dcb29a3
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 53 deletions.
4 changes: 2 additions & 2 deletions typerocket-v5.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'))
Expand Down
12 changes: 6 additions & 6 deletions typerocket/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions typerocket/config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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'),
],
]
];
14 changes: 7 additions & 7 deletions typerocket/vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -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"
},
Expand Down
10 changes: 5 additions & 5 deletions typerocket/vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => 'abb534c072b1f482af046364cbe0df6dfdc54ec4',
'reference' => '15bc9611510e96bd92a4deda27e23a414fa1c8a7',
'name' => 'typerocket/typerocket',
'dev' => true,
),
Expand Down Expand Up @@ -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(
Expand All @@ -418,7 +418,7 @@
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => 'abb534c072b1f482af046364cbe0df6dfdc54ec4',
'reference' => '15bc9611510e96bd92a4deda27e23a414fa1c8a7',
'dev_requirement' => false,
),
'webmozart/assert' => array(
Expand Down
13 changes: 1 addition & 12 deletions typerocket/vendor/typerocket/core/src/Core/ApplicationKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
67 changes: 57 additions & 10 deletions typerocket/vendor/typerocket/core/src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use TypeRocket\Core\Config;
use TypeRocket\Core\Container;
use TypeRocket\Database\Connectors\DatabaseConnector;
use TypeRocket\Database\Connectors\WordPressCoreDatabaseConnector;

class Connection
{
Expand All @@ -13,7 +14,7 @@ class Connection
/**
* @var array<string, \wpdb>
*/
protected $connections = [];
protected array $connections = [];

/**
* @return array
Expand Down Expand Up @@ -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();
Expand All @@ -86,17 +109,41 @@ 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;
}

/**
* @return static
*/
public static function getFromContainer()
{
return Container::resolve(static::ALIAS);
return Container::resolve(static::ALIAS, true);
}
}
19 changes: 17 additions & 2 deletions typerocket/vendor/typerocket/core/src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand All @@ -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
*
Expand Down
29 changes: 26 additions & 3 deletions typerocket/vendor/typerocket/core/src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,17 @@ 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
* @throws \Exception
*/
public function __construct()
{
$wpdb = Connection::getFromContainer()->get($this->connection);
$wpdb = $this->establishConnection();

try {
$type = (new ReflectionClass( $this ))->getShortName();
Expand All @@ -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
Expand Down

0 comments on commit dcb29a3

Please sign in to comment.