Skip to content

Commit

Permalink
Split up the installer logic
Browse files Browse the repository at this point in the history
This is probably the most complicated way I could find to fix #1587.

Jokes aside, this was done with a few goals in mind:
- Reduce coupling between the installer and the rest of Flarum's
  "Application", which we are building during installation.
- Move the installer logic to several smaller classes, which can then
  be used by the web frontend and the console task, instead of the
  former hacking its way into the latter to be "DRY".
- Separate installer infrastructure (the "pipeline", with the ability
  to revert steps upon failure) from the actual steps being taken.

The problem was conceptual, and would certainly re-occur in a similar
fashion if we wouldn't tackle it at its roots.

It is fixed now, because we no longer use the ExtensionManager for
enabling extensions, but instead duplicate some of its logic. That is
fine because we don't want to do everything it does, e.g. omit
extenders' lifecycle hooks (which depend on the Application instance
being complete).

> for each desired change, make the change easy (warning: this may be
> hard), then make the easy change

- Kent Beck, https://twitter.com/kentbeck/status/250733358307500032

Fixes #1587.
  • Loading branch information
franzliedke committed Jan 31, 2019
1 parent abf224b commit 790d5be
Show file tree
Hide file tree
Showing 22 changed files with 1,073 additions and 364 deletions.
38 changes: 38 additions & 0 deletions src/Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@

namespace Flarum\Extension;

use Flarum\Database\Migrator;
use Flarum\Extend\Compat;
use Flarum\Extend\LifecycleInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\MountManager;
use League\Flysystem\Plugin\ListFiles;

/**
* @property string $name
Expand Down Expand Up @@ -307,6 +313,25 @@ public function hasAssets()
return realpath($this->path.'/assets/') !== false;
}

public function copyAssetsTo(FilesystemInterface $target)
{
if (! $this->hasAssets()) {
return;
}

$mount = new MountManager([
'source' => $source = new Filesystem(new Local($this->getPath().'/assets')),
'target' => $target,
]);

$source->addPlugin(new ListFiles);
$assetFiles = $source->listFiles('/', true);

foreach ($assetFiles as $file) {
$mount->copy("source://$file[path]", "target://extensions/$this->id/$file[path]");
}
}

/**
* Tests whether the extension has migrations.
*
Expand All @@ -317,6 +342,19 @@ public function hasMigrations()
return realpath($this->path.'/migrations/') !== false;
}

public function migrate(Migrator $migrator, $direction = 'up')
{
if (! $this->hasMigrations()) {
return;
}

if ($direction == 'up') {
return $migrator->run($this->getPath().'/migrations', $this);
} else {
return $migrator->reset($this->getPath().'/migrations', $this);
}
}

/**
* Generates an array result for the object.
*
Expand Down
18 changes: 4 additions & 14 deletions src/Extension/ExtensionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,26 +222,16 @@ public function getAsset(Extension $extension, $path)
* Runs the database migrations for the extension.
*
* @param Extension $extension
* @param bool|true $up
* @param string $direction
* @return void
*/
public function migrate(Extension $extension, $up = true)
public function migrate(Extension $extension, $direction = 'up')
{
if (! $extension->hasMigrations()) {
return;
}

$migrationDir = $extension->getPath().'/migrations';

$this->app->bind('Illuminate\Database\Schema\Builder', function ($container) {
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
});

if ($up) {
$this->migrator->run($migrationDir, $extension);
} else {
$this->migrator->reset($migrationDir, $extension);
}
$extension->migrate($this->migrator, $direction);
}

/**
Expand All @@ -252,7 +242,7 @@ public function migrate(Extension $extension, $up = true)
*/
public function migrateDown(Extension $extension)
{
return $this->migrate($extension, false);
return $this->migrate($extension, 'down');
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/Install/Console/DefaultsDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
class DefaultsDataProvider implements DataProviderInterface
{
protected $databaseConfiguration = [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'flarum',
'username' => 'root',
'password' => '',
'prefix' => '',
'port' => '3306',
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'flarum',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'port' => '3306',
'strict' => false,
];

protected $debug = false;
Expand Down
Loading

0 comments on commit 790d5be

Please sign in to comment.