Skip to content

Commit

Permalink
Fix database connection
Browse files Browse the repository at this point in the history
  • Loading branch information
alifallahrn committed Jul 17, 2024
1 parent 8fa6057 commit aeabea2
Showing 1 changed file with 97 additions and 75 deletions.
172 changes: 97 additions & 75 deletions src/Database/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,80 +13,102 @@
class DatabaseServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface, BootablePluginProviderInterface
{

/**
* The provided array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'database'
];

/**
* Add the Capsule\Manager into the plugin.
*
* @return void
*
*/
public function boot()
{
global $wpdb;
$container = $this->getContainer();

$container
->share('database', Capsule::class)
->addMethodCall('addConnection', [
'config' => [
'driver' => 'mysql',
'host' => DB_HOST,
'database' => DB_NAME,
'username' => DB_USER,
'password' => DB_PASSWORD,
'charset' => DB_CHARSET,
'prefix' => $wpdb->prefix,
]
])
->addMethodCall('setAsGlobal');

// Boot eloquent should be when capsule is initialized.
$container->get('database')->bootEloquent();


}

/**
* @return void
*/
public function register()
{


}

/**
* When the plugin is booted, register a new macro.
*
* Adds the `database()` method that returns shard instance of the Illuminate\Database\Capsule\Manager class.
*
* @return void
* @example Model::where('wp_users', 1)->get();
*/
public function bootPlugin()
{

$instance = $this;

$this->getContainer()::macro(
'database',
function () use ($instance) {
return $instance->getContainer()->get('database');
}
);

}
/**
* The provided array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'database'
];

/**
* Add the Capsule\Manager into the plugin.
*
* @return void
*
*/
public function boot()
{
global $wpdb;
$container = $this->getContainer();

// Determine the charset and collation to use.
$charsetCollate = $wpdb->determine_charset((!empty($wpdb->dbcharset) ? $wpdb->dbcharset : 'utf8'), '');

// Prepare the configuration array.
$config = array(
'driver' => 'mysql',
'database' => $wpdb->dbname,
'username' => $wpdb->dbuser,
'password' => $wpdb->dbpassword,
'prefix' => $wpdb->prefix,
'charset' => $charsetCollate['charset'],
);

// Parse the host to determine if it is an IPv6 address.
list($host, $port, $socket, $isIpV6) = $wpdb->parse_db_host($wpdb->dbhost);

// If the host is an IPv6 address, wrap it in square brackets.
if (\extension_loaded('mysqlnd') && $isIpV6) {
$host = "[{$host}]";
}

if (isset($socket)) {
$config['unix_socket'] = $socket;
} else {
$config['host'] = $host;
if (isset($port)) {
$config['port'] = $port;
}
}

$container
->share('database', Capsule::class)
->addMethodCall('addConnection', [
'config' => $config
])
->addMethodCall('setAsGlobal');

// Boot eloquent should be when capsule is initialized.
$container->get('database')->bootEloquent();


}

/**
* @return void
*/
public function register()
{


}

/**
* When the plugin is booted, register a new macro.
*
* Adds the `database()` method that returns shard instance of the Illuminate\Database\Capsule\Manager class.
*
* @return void
* @example Model::where('wp_users', 1)->get();
*/
public function bootPlugin()
{

$instance = $this;

$this->getContainer()::macro(
'database',
function () use ($instance) {
return $instance->getContainer()->get('database');
}
);

}

}

0 comments on commit aeabea2

Please sign in to comment.