Move logic to Extension class

The gathering and execution of extenders can actually be done here
in the `Extension` class. This way, the `ExtensionManager` only
deals with the question of which extensions are enabled, the
`Extension` class actually extends the core application, and the
service provider simply calls a method, without having to know
about internals.
This commit is contained in:
Franz Liedke 2018-03-04 01:16:50 +01:00
parent 407515f5a7
commit 33beec3739
3 changed files with 32 additions and 29 deletions

View File

@ -11,6 +11,8 @@
namespace Flarum\Extension; namespace Flarum\Extension;
use Flarum\Extend\Compat;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -109,6 +111,28 @@ class Extension implements Arrayable
$this->id = "$vendor-$package"; $this->id = "$vendor-$package";
} }
public function extend(Container $app)
{
$bootstrapper = $this->getBootstrapperPath();
if (!file_exists($bootstrapper)) {
return;
}
$extenders = array_flatten((array) require $bootstrapper);
foreach ($extenders as $extender) {
// If an extension has not yet switched to the new bootstrap.php
// format, it might return a function (or more of them). We wrap
// these in a Compat extender to enjoy an unique interface.
if ($extender instanceof \Closure || is_string($extender)) {
$extender = new Compat($extender);
}
$extender($app, $this);
}
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -20,6 +20,7 @@ use Flarum\Extension\Event\Enabling;
use Flarum\Extension\Event\Uninstalled; use Flarum\Extension\Event\Uninstalled;
use Flarum\Foundation\Application; use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
@ -275,33 +276,15 @@ class ExtensionManager
} }
/** /**
* Retrieve all extender instances of all enabled extensions. * Call on all enabled extensions to extend the Flarum application.
* *
* @return Collection * @param Container $app
*/ */
public function getActiveExtenders() public function extend(Container $app)
{ {
return $this->getEnabledExtensions() foreach ($this->getEnabledExtensions() as $extension) {
->flatMap(function (Extension $extension) { $extension->extend($app);
$bootstrapper = $extension->getBootstrapperPath();
if ($this->filesystem->exists($bootstrapper)) {
return array_map(function ($extender) use ($extension) {
// If an extension has not yet switched to the new bootstrap.php
// format, it might return a function (or more of them). We wrap
// these in a Compat extender to enjoy an unique interface.
if ($extender instanceof \Closure || is_string($extender)) {
$extender = new Compat($extender);
} }
return function ($app) use ($extension, $extender) {
return $extender($app, $extension);
};
}, array_flatten((array) require $bootstrapper));
}
return [];
});
} }
/** /**

View File

@ -24,11 +24,7 @@ class ExtensionServiceProvider extends AbstractServiceProvider
$this->app->bind('flarum.extensions', ExtensionManager::class); $this->app->bind('flarum.extensions', ExtensionManager::class);
$this->app->booting(function (Container $app) { $this->app->booting(function (Container $app) {
$extenders = $app->make('flarum.extensions')->getActiveExtenders(); $app->make('flarum.extensions')->extend($app);
foreach ($extenders as $extender) {
$extender($app);
}
}); });
} }