Resolve extenders from ExtensionManager

Loading the activated extensions now means retrieving an array of
extenders (classes that implement a certain type of extension of a core
feature in Flarum).

For now, the only existing extender is the Compat extender which is used
to handle old-style bootstrappers that simply return a closure that
receives all of its dependencies via auto injection.

In the future, extensions will be able to return an array of extender
instances from their bootstrapper instead. These extender classes will
be implemented in the next step.
This commit is contained in:
Franz Liedke 2017-10-03 16:15:12 +02:00
parent c41e58531a
commit 5b6d043f80
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
5 changed files with 89 additions and 15 deletions

38
src/Extend/Compat.php Normal file
View File

@ -0,0 +1,38 @@
<?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;
/**
* This class is used to wrap old bootstrap.php closures (as used in versions up
* to 0.1.0-beta7) in the new Extender format.
*
* This gives extensions the chance to work with the new API without making any
* changes, and have some time to convert to the pure usage of extenders.
*
* @deprecated
*/
class Compat implements Extender
{
protected $callback;
public function __construct($callback)
{
$this->callback = $callback;
}
public function apply(Container $container)
{
$container->call($this->callback);
}
}

19
src/Extend/Extender.php Normal file
View File

@ -0,0 +1,19 @@
<?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

@ -231,6 +231,11 @@ class Extension implements Arrayable
return $this->path; return $this->path;
} }
public function getBootstrapperPath()
{
return "{$this->path}/bootstrap.php";
}
/** /**
* Tests whether the extension has assets. * Tests whether the extension has assets.
* *

View File

@ -12,6 +12,7 @@
namespace Flarum\Extension; namespace Flarum\Extension;
use Flarum\Database\Migrator; use Flarum\Database\Migrator;
use Flarum\Extend\Compat;
use Flarum\Extension\Event\Disabled; use Flarum\Extension\Event\Disabled;
use Flarum\Extension\Event\Disabling; use Flarum\Extension\Event\Disabling;
use Flarum\Extension\Event\Enabled; use Flarum\Extension\Event\Enabled;
@ -273,21 +274,30 @@ class ExtensionManager
} }
/** /**
* Loads all bootstrap.php files of the enabled extensions. * Retrieve all extender instances of all enabled extensions.
* *
* @return Collection * @return Collection
*/ */
public function getEnabledBootstrappers() public function getActiveExtenders()
{ {
$bootstrappers = new Collection; return $this->getEnabledExtensions()
->flatMap(function (Extension $extension) {
$bootstrapper = $extension->getBootstrapperPath();
if ($this->filesystem->exists($bootstrapper)) {
$extenders = require $bootstrapper;
foreach ($this->getEnabledExtensions() as $extension) { if (is_array($extenders)) {
if ($this->filesystem->exists($file = $extension->getPath().'/bootstrap.php')) { return $extenders;
$bootstrappers->push($file); }
}
}
return $bootstrappers; // Assume that the extension has not yet switched to the new
// bootstrap.php format, and wrap the callback in a Compat
// extender.
return [new Compat($extenders)];
} else {
return [];
}
});
} }
/** /**

View File

@ -12,6 +12,7 @@
namespace Flarum\Extension; namespace Flarum\Extension;
use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\AbstractServiceProvider;
use Illuminate\Contracts\Container\Container;
class ExtensionServiceProvider extends AbstractServiceProvider class ExtensionServiceProvider extends AbstractServiceProvider
{ {
@ -22,13 +23,14 @@ class ExtensionServiceProvider extends AbstractServiceProvider
{ {
$this->app->bind('flarum.extensions', ExtensionManager::class); $this->app->bind('flarum.extensions', ExtensionManager::class);
$bootstrappers = $this->app->make('flarum.extensions')->getEnabledBootstrappers(); $this->app->booting(function (Container $app) {
/** @var \Flarum\Extend\Extender[] $extenders */
$extenders = $app->make('flarum.extensions')->getActiveExtenders();
foreach ($bootstrappers as $file) { foreach ($extenders as $extender) {
$bootstrapper = require $file; $extender->apply($app);
}
$this->app->call($bootstrapper); });
}
} }
/** /**