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.
This commit is contained in:
Franz Liedke 2018-04-24 23:50:36 +02:00
parent 1c28afce69
commit 86fe1b34c4
4 changed files with 22 additions and 101 deletions

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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) {

View File

@ -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.
*