diff --git a/src/Core.php b/src/Core.php index c817e1c27..6b0a58ffd 100644 --- a/src/Core.php +++ b/src/Core.php @@ -4,7 +4,7 @@ class Core { public static function isInstalled() { - return file_exists(base_path('../config.php')); + return app()->bound('flarum.config'); } public static function inDebugMode() @@ -18,7 +18,7 @@ class Core return $default; } - if (is_null($value = app('db')->table('config')->where('key', $key)->pluck('value'))) { + if (is_null($value = app('flarum.db')->table('config')->where('key', $key)->pluck('value'))) { $value = $default; } diff --git a/src/Core/DatabaseServiceProvider.php b/src/Core/DatabaseServiceProvider.php new file mode 100644 index 000000000..3d1b65e67 --- /dev/null +++ b/src/Core/DatabaseServiceProvider.php @@ -0,0 +1,46 @@ +app->singleton('flarum.db', function () { + $factory = new ConnectionFactory($this->app); + $connection = $factory->make($this->app->make('flarum.config')['database']); + $connection->setEventDispatcher($this->app->make('Illuminate\Contracts\Events\Dispatcher')); + $connection->setFetchMode(PDO::FETCH_CLASS); + return $connection; + }); + + $this->app->alias('flarum.db', 'Illuminate\Database\ConnectionInterface'); + + $this->app->singleton('Illuminate\Database\ConnectionResolverInterface', function () { + $resolver = new ConnectionResolver([ + 'flarum' => $this->app->make('flarum.db'), + ]); + $resolver->setDefaultConnection('flarum'); + return $resolver; + }); + + $this->app->alias('Illuminate\Database\ConnectionResolverInterface', 'db'); + + if (Core::isInstalled()) { + $this->app->booting(function() { + $resolver = $this->app->make('Illuminate\Database\ConnectionResolverInterface'); + Model::setConnectionResolver($resolver); + }); + } + } +} diff --git a/src/Forum/Actions/IndexAction.php b/src/Forum/Actions/IndexAction.php index 914c747b9..1e2d10070 100644 --- a/src/Forum/Actions/IndexAction.php +++ b/src/Forum/Actions/IndexAction.php @@ -9,7 +9,7 @@ use Flarum\Forum\Events\RenderView; use Flarum\Locale\JsCompiler as LocaleJsCompiler; use Flarum\Support\Actor; use Flarum\Support\HtmlAction; -use Illuminate\Database\DatabaseManager; +use Illuminate\Database\ConnectionInterface; use Psr\Http\Message\ServerRequestInterface as Request; use Symfony\Component\HttpFoundation\Session\SessionInterface; @@ -25,8 +25,7 @@ class IndexAction extends HtmlAction public static $translations = []; - // TODO: DatabaseManager should be ConnectionInterface - public function __construct(Client $apiClient, Actor $actor, DatabaseManager $database, SessionInterface $session) + public function __construct(Client $apiClient, Actor $actor, ConnectionInterface $database, SessionInterface $session) { $this->apiClient = $apiClient; $this->actor = $actor;