diff --git a/README.md b/README.md index e1b9e43..6bbb5c9 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ require_once('vendor/autoload.php'); \WPEloquent\Core\Laravel::connect([ 'global' => true, - + 'multiple_connections' => false, 'config' => [ 'database' => [ @@ -45,7 +45,8 @@ require_once('vendor/autoload.php'); ], // your wpdb prefix - 'prefix' => 'wp_', + 'prefix' => 'wp_', + ], // enable events @@ -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 diff --git a/WPEloquent/Core/Laravel.php b/WPEloquent/Core/Laravel.php index 9f0f331..41dd024 100644 --- a/WPEloquent/Core/Laravel.php +++ b/WPEloquent/Core/Laravel.php @@ -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(); } } + ?>