refactoring to support array closures migrations and fixed issues with previous pr for extension rewriting

This commit is contained in:
Daniel Klabbers 2016-02-25 23:25:22 +09:00 committed by Franz Liedke
parent 2b5dab73f9
commit e4412178b1
5 changed files with 60 additions and 49 deletions

View File

@ -42,12 +42,10 @@ class UpdateExtensionController implements ControllerInterface
$enabled = array_get($request->getParsedBody(), 'enabled');
$name = array_get($request->getQueryParams(), 'name');
$extension = $this->extensions->getExtension($name);
if ($enabled === true) {
$this->extensions->enable($extension);
$this->extensions->enable($name);
} elseif ($enabled === false) {
$this->extensions->disable($extension);
$this->extensions->disable($name);
}
}
}

View File

@ -12,10 +12,8 @@
namespace Flarum\Database;
use Flarum\Extension\Extension;
use Illuminate\Container\Container;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
class Migrator
{
@ -88,18 +86,17 @@ class Migrator
$migrations = array_diff($files, $ran);
$this->runMigrationList($migrations, $path, $extension);
$this->runMigrationList($path, $migrations, $extension);
}
/**
* Run an array of migrations.
*
* @param array $migrations
* @param string $path
* @param Extension $extension
* @return void
* @param string $path
* @param array $migrations
* @param Extension $extension
*/
public function runMigrationList($migrations, $path, Extension $extension = null)
public function runMigrationList($path, $migrations, Extension $extension = null)
{
// First we will just make sure that there are any migrations to run. If there
// aren't, we will just make a note of it to the developer so they're aware
@ -114,28 +111,24 @@ class Migrator
// migrations "up" so the changes are made to the databases. We'll then log
// that the migration was run so we don't repeat it next time we execute.
foreach ($migrations as $file) {
$this->runUp($file, $path, $extension);
$this->runUp($path, $file, $extension);
}
}
/**
* Run "up" a migration instance.
*
* @param string $path
* @param string $file
* @param string $path
* @param Extension $extension
* @return void
*/
protected function runUp($file, $path, Extension $extension = null)
protected function runUp($path, $file, Extension $extension = null)
{
// First we will resolve a "real" instance of the migration class from this
// migration file name. Once we have the instances we can run the actual
// command such as "up" or "down", or we can just simulate the action.
$migration = $this->resolve($file, $path);
$migration = $this->resolve($path, $file);
if (isset($migration['up'])) {
app()->call($migration['up']);
}
$this->runClosureMigration($migration);
// Once we have run a migrations class, we will log that it was run in this
// repository so that we don't try to run it next time we do a migration
@ -164,7 +157,7 @@ class Migrator
$this->note('<info>Nothing to rollback.</info>');
} else {
foreach ($migrations as $migration) {
$this->runDown($migration, $path, $extension);
$this->runDown($path, $migration, $extension);
}
}
@ -174,21 +167,16 @@ class Migrator
/**
* Run "down" a migration instance.
*
* @param $path
* @param string $file
* @param string $path
* @param Extension $extension
* @return void
*/
protected function runDown($file, $path, Extension $extension = null)
protected function runDown($path, $file, Extension $extension = null)
{
// First we will get the file name of the migration so we can resolve out an
// instance of the migration. Once we get an instance we can either run a
// pretend execution of the migration or we can run the real migration.
$migration = $this->resolve($file, $path);
$migration = $this->resolve($path, $file);
if (isset($migration['down'])) {
app()->call($migration['down']);
}
$this->runClosureMigration($migration, 'down');
// Once we have successfully run the migration "down" we will remove it from
// the migration repository so it will be considered to have not been run
@ -198,6 +186,22 @@ class Migrator
$this->note("<info>Rolled back:</info> $file");
}
/**
* Runs a closure migration based on the migrate direction.
*
* @param $migration
* @param string $direction
* @throws \Exception
*/
protected function runClosureMigration($migration, $direction = 'up')
{
if (is_array($migration) && array_key_exists($direction, $migration)) {
app()->call($migration[$direction]);
} else {
throw new \Exception("Migration file should contain an array with up/down.");
}
}
/**
* Get all of the migration files in a given path.
*
@ -208,9 +212,6 @@ class Migrator
{
$files = $this->files->glob($path . '/*_*.php');
// Once we have the array of files in the directory we will just remove the
// extension and take the basename of the file which is all we need when
// finding the migrations that haven't been run against the databases.
if ($files === false) {
return [];
}
@ -228,15 +229,19 @@ class Migrator
}
/**
* Load a migration instance from a file.
* Resolve a migration instance from a file.
*
* @param string $file
* @param string $path
* @return object
* @param string $file
* @return array
*/
public function resolve($file, $path)
public function resolve($path, $file)
{
return $this->files->getRequire("$path/$file.php");
$migration = "$path/$file.php";
if ($this->files->exists($migration)) {
return $this->files->getRequire($migration);
}
}
/**

View File

@ -39,7 +39,7 @@ class Extension implements Arrayable
/**
* Unique Id of the extension.
*
* @info Identical to the directory in the extensions directory.
* @info Identical to the directory in the extensions directory.
* @example flarum_suspend
*
* @var string
@ -222,6 +222,14 @@ class Extension implements Arrayable
return $this->id;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Tests whether the extension has assets.
*
@ -250,11 +258,11 @@ class Extension implements Arrayable
public function toArray()
{
return (array) array_merge([
'id' => $this->getId(),
'version' => $this->getVersion(),
'path' => $this->path,
'icon' => $this->getIcon(),
'hasAssets' => $this->hasAssets(),
'id' => $this->getId(),
'version' => $this->getVersion(),
'path' => $this->path,
'icon' => $this->getIcon(),
'hasAssets' => $this->hasAssets(),
'hasMigrations' => $this->hasMigrations(),
], $this->composerJson);
}

View File

@ -182,7 +182,7 @@ class ExtensionManager
{
if ($extension->hasAssets()) {
$this->filesystem->copyDirectory(
$this->app->basePath() . '/extensions/' . $extension->getId() . '/assets',
$extension->getPath() . '/assets',
$this->app->basePath() . '/assets/extensions/' . $extension->getId()
);
}
@ -219,7 +219,7 @@ class ExtensionManager
public function migrate(Extension $extension, $up = true)
{
if ($extension->hasMigrations()) {
$migrationDir = public_path('extensions/' . $extension->getId() . '/migrations');
$migrationDir = $extension->getPath() . '/migrations';
$this->app->bind('Illuminate\Database\Schema\Builder', function ($container) {
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
@ -240,7 +240,7 @@ class ExtensionManager
*/
public function migrateDown(Extension $extension)
{
$this->migrate($extension->getId(), false);
$this->migrate($extension, false);
}
public function getMigrator()

View File

@ -81,7 +81,7 @@ class MigrateCommand extends AbstractCommand
$this->info('Migrating extension: '.$name);
$extensions->migrate($name);
$extensions->migrate($extension);
foreach ($migrator->getNotes() as $note) {
$this->info($note);