mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 04:31:57 +08:00
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:
parent
2967b5d106
commit
db7cd71f19
@ -11,6 +11,8 @@
|
||||
|
||||
namespace Flarum\Extension;
|
||||
|
||||
use Flarum\Extend\Compat;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
@ -109,6 +111,28 @@ class Extension implements Arrayable
|
||||
$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}
|
||||
*/
|
||||
|
@ -20,6 +20,7 @@ use Flarum\Extension\Event\Enabling;
|
||||
use Flarum\Extension\Event\Uninstalled;
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
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()
|
||||
->flatMap(function (Extension $extension) {
|
||||
$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 [];
|
||||
});
|
||||
foreach ($this->getEnabledExtensions() as $extension) {
|
||||
$extension->extend($app);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,11 +24,7 @@ class ExtensionServiceProvider extends AbstractServiceProvider
|
||||
$this->app->bind('flarum.extensions', ExtensionManager::class);
|
||||
|
||||
$this->app->booting(function (Container $app) {
|
||||
$extenders = $app->make('flarum.extensions')->getActiveExtenders();
|
||||
|
||||
foreach ($extenders as $extender) {
|
||||
$extender($app);
|
||||
}
|
||||
$app->make('flarum.extensions')->extend($app);
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user