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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -284,20 +284,23 @@ class ExtensionManager
return $this->getEnabledExtensions() return $this->getEnabledExtensions()
->flatMap(function (Extension $extension) { ->flatMap(function (Extension $extension) {
$bootstrapper = $extension->getBootstrapperPath(); $bootstrapper = $extension->getBootstrapperPath();
if ($this->filesystem->exists($bootstrapper)) { if ($this->filesystem->exists($bootstrapper)) {
return (array) require $bootstrapper; return array_map(function ($extender) use ($extension) {
} else {
return [];
}
})->map(function ($extender) {
// If an extension has not yet switched to the new bootstrap.php // If an extension has not yet switched to the new bootstrap.php
// format, it might return a function (or more of them). We wrap // format, it might return a function (or more of them). We wrap
// these in a Compat extender to enjoy an unique interface. // these in a Compat extender to enjoy an unique interface.
if ($extender instanceof \Closure || is_string($extender)) { if ($extender instanceof \Closure || is_string($extender)) {
return new Compat($extender); $extender = new Compat($extender);
} else {
return $extender;
} }
return function ($app) use ($extension, $extender) {
return $extender($app, $extension);
};
}, array_flatten((array) require $bootstrapper));
}
return [];
}); });
} }