Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple connections to be defined #8

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require_once('vendor/autoload.php');

\WPEloquent\Core\Laravel::connect([
'global' => true,

'multiple_connections' => false,
'config' => [

'database' => [
Expand All @@ -45,7 +45,8 @@ require_once('vendor/autoload.php');
],

// your wpdb prefix
'prefix' => 'wp_',
'prefix' => 'wp_',

],

// enable events
Expand All @@ -57,6 +58,34 @@ require_once('vendor/autoload.php');

```

If you would like to create multiple database connections set the multiple_connections value to true, and replace the config element as follows:
```php
'config' => [

'database' => [
'default' => [
'user' => 'user',
'password' => 'password',
'name' => 'database',
'host' => '127.0.0.1',
'port' => '3306',
'prefix' => 'wp_',
],
// Define your extra connections here

// 'extra_connection' => [
// 'user' => 'user2',
// 'password' => 'password2',
// 'name' => 'database2',
// 'host' => '127.0.0.2',
// 'port' => '3306',
// 'prefix' => 'wp_',
//],
]

],
```

If you wanted to enable this on your entire WP install you could create a file with the above code to drop in the `mu-plugins` folder.

### Posts
Expand Down
139 changes: 101 additions & 38 deletions WPEloquent/Core/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,138 @@

use Config;

class Laravel {
class Laravel
{

protected static $_capsule;

/**
* [capsule description]
* @param array $options [description]
*
* @param array $options [description]
* @return [type] [description]
* @author drewjbartlett
*/
public static function connect ($options = []) {
public static function connect($options = [])
{

if (is_null(self::$_capsule)) {

self::$_capsule = new \Illuminate\Database\Capsule\Manager();
if (array_key_exists('multiple_connections', $options) && $options['multiple_connections']) {
self::addMultipleConnections($options);
} else {
self::addSingleConnection($options);
}

self::$_capsule->bootEloquent();

if ($options['events']) {
self::$_capsule->setEventDispatcher(new \Illuminate\Events\Dispatcher);
}

if ($options['global']) {
self::$_capsule->setAsGlobal();
}

if ($options['log']) {
self::$_capsule->getConnection()->enableQueryLog();
}

}

return self::$_capsule;
}

public static function getConnection($name = null)
{
return self::$_capsule->getConnection($name);
}

public static function queryLog()
{
return self::getConnection()->getQueryLog();
}

/**
* @param mixed $capsule
*/
public static function addSingleConnection($options)
{
$defaults = [
'global' => true,

'config' => [
'database' => [
'user' => '',
'password' => '',
'name' => '',
'host' => '',
'port' => '3306'
'port' => '3306',
],
'prefix' => 'wp_',
],
'events' => false,
'log' => true,
];
$options = array_replace_recursive($defaults, $options);
self::$_capsule->addConnection([
'driver' => 'mysql',
'host' => $options['config']['database']['host'],
'database' => $options['config']['database']['name'],
'username' => $options['config']['database']['user'],
'password' => $options['config']['database']['password'],
'port' => $options['config']['database']['port'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => $options['config']['prefix'],
]);
}

'prefix' => 'wp_'
/**
* @param mixed $capsule
*/
public static function addMultipleConnections($options)
{
$defaults = [
'global' => true,

'config' => [
'database' => [
'default' => [
'user' => '',
'password' => '',
'name' => '',
'host' => '',
'port' => '3306',
]
],

'prefix' => '',
],

'events' => false,

'log' => true
'log' => true,
];

$options = array_replace_recursive($defaults, $options);

if(is_null(self::$_capsule)) {

self::$_capsule = new \Illuminate\Database\Capsule\Manager();

foreach ($options['config']['database'] as $name => $config) {
if (count($options['config']['database']) === 1) {
$name = 'default';
}
self::$_capsule->addConnection([
'driver' => 'mysql',
'host' => $options['config']['database']['host'],
'database' => $options['config']['database']['name'],
'username' => $options['config']['database']['user'],
'password' => $options['config']['database']['password'],
'port' => $options['config']['database']['port'],
'host' => $config['host'],
'database' => $config['name'],
'username' => $config['user'],
'password' => $config['password'],
'port' => $config['port'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => $options['config']['prefix']
]);

self::$_capsule->bootEloquent();

if($options['events']) self::$_capsule->setEventDispatcher(new \Illuminate\Events\Dispatcher);

if($options['global']) self::$_capsule->setAsGlobal();

if($options['log']) self::$_capsule->getConnection()->enableQueryLog();

'prefix' => isset($config['prefix']) ? $config['prefix'] : null,
], $name);
}

return self::$_capsule;
}

public static function getConnection () {
return self::$_capsule->getConnection();
}

public static function queryLog () {
return self::getConnection()->getQueryLog();
}
}

?>