Turn extenders into callables

This simplifies the API and gives extension developers more
flexibility, for a) maintaining backwards compatibility, and
b) doing advanced stuff that extenders do not allow.

Note that only extenders are guaranteed to work across
different versions of Flarum (once the API surface is stable).

See the discussion in https://github.com/flarum/core/pull/1335.
This commit is contained in:
Franz Liedke 2018-01-09 20:49:51 +01:00
parent 714775cfed
commit 1ce70eeb6e
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
7 changed files with 12 additions and 31 deletions

View File

@ -15,7 +15,7 @@ use Flarum\Frontend\Event\Rendering;
use Illuminate\Contracts\Container\Container;
use Illuminate\Events\Dispatcher;
class Assets implements Extender
class Assets
{
protected $appName;
@ -49,7 +49,7 @@ class Assets implements Extender
return $this;
}
public function apply(Container $container)
public function __invoke(Container $container)
{
$container->make(Dispatcher::class)->listen(
Rendering::class,

View File

@ -22,7 +22,7 @@ use Illuminate\Contracts\Container\Container;
*
* @deprecated
*/
class Compat implements Extender
class Compat
{
protected $callback;
@ -31,7 +31,7 @@ class Compat implements Extender
$this->callback = $callback;
}
public function apply(Container $container)
public function __invoke(Container $container)
{
$container->call($this->callback);
}

View File

@ -1,19 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Illuminate\Contracts\Container\Container;
interface Extender
{
public function apply(Container $container);
}

View File

@ -15,7 +15,7 @@ use Flarum\Formatter\Event\Configuring;
use Illuminate\Contracts\Container\Container;
use Illuminate\Events\Dispatcher;
class FormatterConfiguration implements Extender
class FormatterConfiguration
{
protected $callback;
@ -24,7 +24,7 @@ class FormatterConfiguration implements Extender
$this->callback = $callback;
}
public function apply(Container $container)
public function __invoke(Container $container)
{
$container->make(Dispatcher::class)->listen(
Configuring::class,

View File

@ -16,7 +16,7 @@ use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container;
use RuntimeException;
class Locale implements Extender
class Locale
{
protected $directory;
@ -25,7 +25,7 @@ class Locale implements Extender
$this->directory = $directory;
}
public function apply(Container $container)
public function __invoke(Container $container)
{
$this->loadLanguagePackFrom(
$this->directory,

View File

@ -14,7 +14,7 @@ namespace Flarum\Extend;
use Flarum\Http\RouteHandlerFactory;
use Illuminate\Contracts\Container\Container;
class Routes implements Extender
class Routes
{
protected $appName;
@ -62,7 +62,7 @@ class Routes implements Extender
return $this;
}
public function apply(Container $container)
public function __invoke(Container $container)
{
if (empty($this->routes)) {
return;

View File

@ -24,11 +24,11 @@ class ExtensionServiceProvider extends AbstractServiceProvider
$this->app->bind('flarum.extensions', ExtensionManager::class);
$this->app->booting(function (Container $app) {
/** @var \Flarum\Extend\Extender[] $extenders */
/** @var callable[] $extenders */
$extenders = $app->make('flarum.extensions')->getActiveExtenders();
foreach ($extenders as $extender) {
$extender->apply($app);
$app->call($extender);
}
});
}