mirror of
https://github.com/flarum/framework.git
synced 2024-11-24 17:12:13 +08:00
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:
parent
c41e58531a
commit
5b6d043f80
38
src/Extend/Compat.php
Normal file
38
src/Extend/Compat.php
Normal 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
19
src/Extend/Extender.php
Normal 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);
|
||||
}
|
|
@ -231,6 +231,11 @@ class Extension implements Arrayable
|
|||
return $this->path;
|
||||
}
|
||||
|
||||
public function getBootstrapperPath()
|
||||
{
|
||||
return "{$this->path}/bootstrap.php";
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether the extension has assets.
|
||||
*
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
namespace Flarum\Extension;
|
||||
|
||||
use Flarum\Database\Migrator;
|
||||
use Flarum\Extend\Compat;
|
||||
use Flarum\Extension\Event\Disabled;
|
||||
use Flarum\Extension\Event\Disabling;
|
||||
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
|
||||
*/
|
||||
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 ($this->filesystem->exists($file = $extension->getPath().'/bootstrap.php')) {
|
||||
$bootstrappers->push($file);
|
||||
}
|
||||
}
|
||||
if (is_array($extenders)) {
|
||||
return $extenders;
|
||||
}
|
||||
|
||||
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 [];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
namespace Flarum\Extension;
|
||||
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class ExtensionServiceProvider extends AbstractServiceProvider
|
||||
{
|
||||
|
@ -22,13 +23,14 @@ class ExtensionServiceProvider extends AbstractServiceProvider
|
|||
{
|
||||
$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) {
|
||||
$bootstrapper = require $file;
|
||||
|
||||
$this->app->call($bootstrapper);
|
||||
}
|
||||
foreach ($extenders as $extender) {
|
||||
$extender->apply($app);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user