Capsule manager (#1744)

Refactored to use the Capsule Database manager for setting up the
Flarum (mysql) connection.

This will introduce the reconnector automatically, fixing #1740
This commit is contained in:
Daniël Klabbers 2019-02-04 23:31:12 +01:00 committed by Franz Liedke
parent 233b495ff8
commit cd4fc8e7db

View File

@ -12,11 +12,9 @@
namespace Flarum\Database; namespace Flarum\Database;
use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\AbstractServiceProvider;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Capsule\Manager;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\ConnectionResolver;
use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\Connectors\ConnectionFactory;
class DatabaseServiceProvider extends AbstractServiceProvider class DatabaseServiceProvider extends AbstractServiceProvider
{ {
@ -25,30 +23,39 @@ class DatabaseServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton('flarum.db', function () { $this->app->singleton(Manager::class, function ($app) {
$factory = new ConnectionFactory($this->app); $manager = new Manager($app);
$dbConfig = $this->app->config('database'); $config = $app->config('database');
$dbConfig['engine'] = 'InnoDB'; $config['engine'] = 'InnoDB';
$dbConfig['prefix_indexes'] = true; $config['prefix_indexes'] = true;
$connection = $factory->make($dbConfig);
$connection->setEventDispatcher($this->app->make(Dispatcher::class));
return $connection; $manager->addConnection($config, 'flarum');
return $manager;
}); });
$this->app->alias('flarum.db', ConnectionInterface::class); $this->app->singleton(ConnectionResolverInterface::class, function ($app) {
$manager = $app->make(Manager::class);
$manager->setAsGlobal();
$manager->bootEloquent();
$this->app->singleton(ConnectionResolverInterface::class, function () { $dbManager = $manager->getDatabaseManager();
$resolver = new ConnectionResolver([ $dbManager->setDefaultConnection('flarum');
'flarum' => $this->app->make('flarum.db'),
]);
$resolver->setDefaultConnection('flarum');
return $resolver; return $dbManager;
}); });
$this->app->alias(ConnectionResolverInterface::class, 'db'); $this->app->alias(ConnectionResolverInterface::class, 'db');
$this->app->singleton(ConnectionInterface::class, function ($app) {
$resolver = $app->make(ConnectionResolverInterface::class);
return $resolver->connection();
});
$this->app->alias(ConnectionInterface::class, 'db.connection');
$this->app->alias(ConnectionInterface::class, 'flarum.db');
} }
/** /**