Give Extenders information about which Extension they belong to

This commit is contained in:
Toby Zerner 2018-03-04 10:13:12 +10:30
parent 84be6f3f7f
commit 407515f5a7
7 changed files with 27 additions and 18 deletions

View File

@ -11,6 +11,7 @@
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Flarum\Frontend\Event\Rendering;
use Illuminate\Contracts\Container\Container;
use Illuminate\Events\Dispatcher;
@ -41,7 +42,7 @@ class Assets implements Extender
return $this;
}
public function __invoke(Container $container)
public function __invoke(Container $container, Extension $extension = null)
{
$container->make(Dispatcher::class)->listen(
Rendering::class,

View File

@ -11,6 +11,7 @@
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
/**
@ -31,7 +32,7 @@ class Compat implements Extender
$this->callback = $callback;
}
public function __invoke(Container $container)
public function __invoke(Container $container, Extension $extension = null)
{
$container->call($this->callback);
}

View File

@ -11,9 +11,10 @@
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
interface Extender
{
public function __invoke(Container $container);
public function __invoke(Container $container, Extension $extension = null);
}

View File

@ -11,6 +11,7 @@
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Flarum\Formatter\Event\Configuring;
use Illuminate\Contracts\Container\Container;
use Illuminate\Events\Dispatcher;
@ -24,7 +25,7 @@ class FormatterConfiguration implements Extender
$this->callback = $callback;
}
public function __invoke(Container $container)
public function __invoke(Container $container, Extension $extension = null)
{
$container->make(Dispatcher::class)->listen(
Configuring::class,

View File

@ -12,6 +12,7 @@
namespace Flarum\Extend;
use DirectoryIterator;
use Flarum\Extension\Extension;
use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container;
use RuntimeException;
@ -25,7 +26,7 @@ class Locale implements Extender
$this->directory = $directory;
}
public function __invoke(Container $container)
public function __invoke(Container $container, Extension $extension = null)
{
$this->loadLanguagePackFrom(
$this->directory,

View File

@ -11,6 +11,7 @@
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Flarum\Http\RouteHandlerFactory;
use Illuminate\Contracts\Container\Container;
@ -62,7 +63,7 @@ class Routes implements Extender
return $this;
}
public function __invoke(Container $container)
public function __invoke(Container $container, Extension $extension = null)
{
if (empty($this->routes)) {
return;

View File

@ -284,20 +284,23 @@ class ExtensionManager
return $this->getEnabledExtensions()
->flatMap(function (Extension $extension) {
$bootstrapper = $extension->getBootstrapperPath();
if ($this->filesystem->exists($bootstrapper)) {
return (array) require $bootstrapper;
} else {
return [];
}
})->map(function ($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)) {
return new Compat($extender);
} else {
return $extender;
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 [];
});
}