From 86fe1b34c4a2eebbf3d4370bc420e12a86265d9d Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Tue, 24 Apr 2018 23:50:36 +0200 Subject: [PATCH] Simplify interface of migration-related classes Mostly, we only need a database connection, instead of one of Laravel's "connection resolvers". Again, this makes our life easier during installation, where we already instantiate a database connection. We can now use that to instantiate our own Migrator class, instead of using the IoC container to build one. --- .../Database/DatabaseMigrationRepository.php | 60 ++++--------------- .../Database/MigrationRepositoryInterface.php | 8 --- .../src/Database/MigrationServiceProvider.php | 2 +- framework/core/src/Database/Migrator.php | 53 ++++------------ 4 files changed, 22 insertions(+), 101 deletions(-) diff --git a/framework/core/src/Database/DatabaseMigrationRepository.php b/framework/core/src/Database/DatabaseMigrationRepository.php index 5ba06cc09..edf669d88 100644 --- a/framework/core/src/Database/DatabaseMigrationRepository.php +++ b/framework/core/src/Database/DatabaseMigrationRepository.php @@ -11,16 +11,16 @@ namespace Flarum\Database; -use Illuminate\Database\ConnectionResolverInterface as Resolver; +use Illuminate\Database\ConnectionInterface; class DatabaseMigrationRepository implements MigrationRepositoryInterface { /** - * The database connection resolver instance. + * The name of the database connection to use. * - * @var \Illuminate\Database\ConnectionResolverInterface + * @var ConnectionInterface */ - protected $resolver; + protected $connection; /** * The name of the migration table. @@ -29,23 +29,16 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface */ protected $table; - /** - * The name of the database connection to use. - * - * @var string - */ - protected $connection; - /** * Create a new database migration repository instance. * - * @param \Illuminate\Database\ConnectionResolverInterface $resolver - * @param string $table + * @param \Illuminate\Database\ConnectionInterface $connection + * @param string $table */ - public function __construct(Resolver $resolver, $table) + public function __construct(ConnectionInterface $connection, $table) { + $this->connection = $connection; $this->table = $table; - $this->resolver = $resolver; } /** @@ -104,7 +97,7 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface */ public function createRepository() { - $schema = $this->getConnection()->getSchemaBuilder(); + $schema = $this->connection->getSchemaBuilder(); $schema->create($this->table, function ($table) { $table->string('migration'); @@ -119,7 +112,7 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface */ public function repositoryExists() { - $schema = $this->getConnection()->getSchemaBuilder(); + $schema = $this->connection->getSchemaBuilder(); return $schema->hasTable($this->table); } @@ -131,37 +124,6 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface */ protected function table() { - return $this->getConnection()->table($this->table); - } - - /** - * Get the connection resolver instance. - * - * @return \Illuminate\Database\ConnectionResolverInterface - */ - public function getConnectionResolver() - { - return $this->resolver; - } - - /** - * Resolve the database connection instance. - * - * @return \Illuminate\Database\Connection - */ - public function getConnection() - { - return $this->resolver->connection($this->connection); - } - - /** - * Set the information source to gather data. - * - * @param string $name - * @return void - */ - public function setSource($name) - { - $this->connection = $name; + return $this->connection->table($this->table); } } diff --git a/framework/core/src/Database/MigrationRepositoryInterface.php b/framework/core/src/Database/MigrationRepositoryInterface.php index 7bd9265e8..e6ae5a1dd 100644 --- a/framework/core/src/Database/MigrationRepositoryInterface.php +++ b/framework/core/src/Database/MigrationRepositoryInterface.php @@ -52,12 +52,4 @@ interface MigrationRepositoryInterface * @return bool */ public function repositoryExists(); - - /** - * Set the information source to gather data. - * - * @param string $name - * @return void - */ - public function setSource($name); } diff --git a/framework/core/src/Database/MigrationServiceProvider.php b/framework/core/src/Database/MigrationServiceProvider.php index 5a21c9ada..165cec0da 100644 --- a/framework/core/src/Database/MigrationServiceProvider.php +++ b/framework/core/src/Database/MigrationServiceProvider.php @@ -22,7 +22,7 @@ class MigrationServiceProvider extends AbstractServiceProvider public function register() { $this->app->singleton('Flarum\Database\MigrationRepositoryInterface', function ($app) { - return new DatabaseMigrationRepository($app['db'], 'migrations'); + return new DatabaseMigrationRepository($app['flarum.db'], 'migrations'); }); $this->app->bind(MigrationCreator::class, function (Application $app) { diff --git a/framework/core/src/Database/Migrator.php b/framework/core/src/Database/Migrator.php index 926d994c6..2c1272fc0 100644 --- a/framework/core/src/Database/Migrator.php +++ b/framework/core/src/Database/Migrator.php @@ -13,7 +13,8 @@ namespace Flarum\Database; use Exception; use Flarum\Extension\Extension; -use Illuminate\Database\ConnectionResolverInterface as Resolver; +use Illuminate\Database\ConnectionInterface; +use Illuminate\Database\Schema\Builder; use Illuminate\Filesystem\Filesystem; class Migrator @@ -33,18 +34,11 @@ class Migrator protected $files; /** - * The connection resolver instance. + * The database schema builder instance. * - * @var \Illuminate\Database\ConnectionResolverInterface + * @var Builder */ - protected $resolver; - - /** - * The name of the default connection. - * - * @var string - */ - protected $connection; + protected $schemaBuilder; /** * The notes for the current operation. @@ -57,17 +51,18 @@ class Migrator * Create a new migrator instance. * * @param MigrationRepositoryInterface $repository - * @param Resolver $resolver + * @param ConnectionInterface $connection * @param Filesystem $files */ public function __construct( MigrationRepositoryInterface $repository, - Resolver $resolver, + ConnectionInterface $connection, Filesystem $files ) { $this->files = $files; - $this->resolver = $resolver; $this->repository = $repository; + + $this->schemaBuilder = $connection->getSchemaBuilder(); } /** @@ -199,7 +194,7 @@ class Migrator protected function runClosureMigration($migration, $direction = 'up') { if (is_array($migration) && array_key_exists($direction, $migration)) { - app()->call($migration[$direction]); + call_user_func($migration[$direction], $this->schemaBuilder); } else { throw new Exception('Migration file should contain an array with up/down.'); } @@ -268,34 +263,6 @@ class Migrator return $this->notes; } - /** - * Resolve the database connection instance. - * - * @param string $connection - * @return \Illuminate\Database\Connection - */ - public function resolveConnection($connection) - { - return $this->resolver->connection($connection); - } - - /** - * Set the default connection name. - * - * @param string $name - * @return void - */ - public function setConnection($name) - { - if (! is_null($name)) { - $this->resolver->setDefaultConnection($name); - } - - $this->repository->setSource($name); - - $this->connection = $name; - } - /** * Get the migration repository instance. *