mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 20:27:41 +08:00
refactoring to support array closures migrations and fixed issues with previous pr for extension rewriting
This commit is contained in:
parent
2b5dab73f9
commit
e4412178b1
@ -42,12 +42,10 @@ class UpdateExtensionController implements ControllerInterface
|
|||||||
$enabled = array_get($request->getParsedBody(), 'enabled');
|
$enabled = array_get($request->getParsedBody(), 'enabled');
|
||||||
$name = array_get($request->getQueryParams(), 'name');
|
$name = array_get($request->getQueryParams(), 'name');
|
||||||
|
|
||||||
$extension = $this->extensions->getExtension($name);
|
|
||||||
|
|
||||||
if ($enabled === true) {
|
if ($enabled === true) {
|
||||||
$this->extensions->enable($extension);
|
$this->extensions->enable($name);
|
||||||
} elseif ($enabled === false) {
|
} elseif ($enabled === false) {
|
||||||
$this->extensions->disable($extension);
|
$this->extensions->disable($name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,10 +12,8 @@
|
|||||||
namespace Flarum\Database;
|
namespace Flarum\Database;
|
||||||
|
|
||||||
use Flarum\Extension\Extension;
|
use Flarum\Extension\Extension;
|
||||||
use Illuminate\Container\Container;
|
|
||||||
use Illuminate\Database\ConnectionResolverInterface as Resolver;
|
use Illuminate\Database\ConnectionResolverInterface as Resolver;
|
||||||
use Illuminate\Filesystem\Filesystem;
|
use Illuminate\Filesystem\Filesystem;
|
||||||
use Illuminate\Support\Str;
|
|
||||||
|
|
||||||
class Migrator
|
class Migrator
|
||||||
{
|
{
|
||||||
@ -88,18 +86,17 @@ class Migrator
|
|||||||
|
|
||||||
$migrations = array_diff($files, $ran);
|
$migrations = array_diff($files, $ran);
|
||||||
|
|
||||||
$this->runMigrationList($migrations, $path, $extension);
|
$this->runMigrationList($path, $migrations, $extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run an array of migrations.
|
* Run an array of migrations.
|
||||||
*
|
*
|
||||||
* @param array $migrations
|
|
||||||
* @param string $path
|
* @param string $path
|
||||||
|
* @param array $migrations
|
||||||
* @param Extension $extension
|
* @param Extension $extension
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
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
|
// 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
|
// 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
|
// 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.
|
// that the migration was run so we don't repeat it next time we execute.
|
||||||
foreach ($migrations as $file) {
|
foreach ($migrations as $file) {
|
||||||
$this->runUp($file, $path, $extension);
|
$this->runUp($path, $file, $extension);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run "up" a migration instance.
|
* Run "up" a migration instance.
|
||||||
*
|
*
|
||||||
|
* @param string $path
|
||||||
* @param string $file
|
* @param string $file
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @param Extension $extension
|
* @param Extension $extension
|
||||||
* @return void
|
* @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 = $this->resolve($path, $file);
|
||||||
// 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);
|
|
||||||
|
|
||||||
if (isset($migration['up'])) {
|
$this->runClosureMigration($migration);
|
||||||
app()->call($migration['up']);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Once we have run a migrations class, we will log that it was run in this
|
// 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
|
// 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>');
|
$this->note('<info>Nothing to rollback.</info>');
|
||||||
} else {
|
} else {
|
||||||
foreach ($migrations as $migration) {
|
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.
|
* Run "down" a migration instance.
|
||||||
*
|
*
|
||||||
|
* @param $path
|
||||||
* @param string $file
|
* @param string $file
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @param Extension $extension
|
* @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
|
$migration = $this->resolve($path, $file);
|
||||||
// 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);
|
|
||||||
|
|
||||||
if (isset($migration['down'])) {
|
$this->runClosureMigration($migration, 'down');
|
||||||
app()->call($migration['down']);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Once we have successfully run the migration "down" we will remove it from
|
// 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
|
// 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");
|
$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.
|
* Get all of the migration files in a given path.
|
||||||
*
|
*
|
||||||
@ -208,9 +212,6 @@ class Migrator
|
|||||||
{
|
{
|
||||||
$files = $this->files->glob($path . '/*_*.php');
|
$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) {
|
if ($files === false) {
|
||||||
return [];
|
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
|
* @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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -222,6 +222,14 @@ class Extension implements Arrayable
|
|||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getPath()
|
||||||
|
{
|
||||||
|
return $this->path;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests whether the extension has assets.
|
* Tests whether the extension has assets.
|
||||||
*
|
*
|
||||||
|
@ -182,7 +182,7 @@ class ExtensionManager
|
|||||||
{
|
{
|
||||||
if ($extension->hasAssets()) {
|
if ($extension->hasAssets()) {
|
||||||
$this->filesystem->copyDirectory(
|
$this->filesystem->copyDirectory(
|
||||||
$this->app->basePath() . '/extensions/' . $extension->getId() . '/assets',
|
$extension->getPath() . '/assets',
|
||||||
$this->app->basePath() . '/assets/extensions/' . $extension->getId()
|
$this->app->basePath() . '/assets/extensions/' . $extension->getId()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -219,7 +219,7 @@ class ExtensionManager
|
|||||||
public function migrate(Extension $extension, $up = true)
|
public function migrate(Extension $extension, $up = true)
|
||||||
{
|
{
|
||||||
if ($extension->hasMigrations()) {
|
if ($extension->hasMigrations()) {
|
||||||
$migrationDir = public_path('extensions/' . $extension->getId() . '/migrations');
|
$migrationDir = $extension->getPath() . '/migrations';
|
||||||
|
|
||||||
$this->app->bind('Illuminate\Database\Schema\Builder', function ($container) {
|
$this->app->bind('Illuminate\Database\Schema\Builder', function ($container) {
|
||||||
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
|
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
|
||||||
@ -240,7 +240,7 @@ class ExtensionManager
|
|||||||
*/
|
*/
|
||||||
public function migrateDown(Extension $extension)
|
public function migrateDown(Extension $extension)
|
||||||
{
|
{
|
||||||
$this->migrate($extension->getId(), false);
|
$this->migrate($extension, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMigrator()
|
public function getMigrator()
|
||||||
|
@ -81,7 +81,7 @@ class MigrateCommand extends AbstractCommand
|
|||||||
|
|
||||||
$this->info('Migrating extension: '.$name);
|
$this->info('Migrating extension: '.$name);
|
||||||
|
|
||||||
$extensions->migrate($name);
|
$extensions->migrate($extension);
|
||||||
|
|
||||||
foreach ($migrator->getNotes() as $note) {
|
foreach ($migrator->getNotes() as $note) {
|
||||||
$this->info($note);
|
$this->info($note);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user