diff --git a/src/Extend/Compat.php b/src/Extend/Compat.php new file mode 100644 index 000000000..3678f2a84 --- /dev/null +++ b/src/Extend/Compat.php @@ -0,0 +1,38 @@ + + * + * 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); + } +} diff --git a/src/Extend/Extender.php b/src/Extend/Extender.php new file mode 100644 index 000000000..71274391e --- /dev/null +++ b/src/Extend/Extender.php @@ -0,0 +1,19 @@ + + * + * 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); +} diff --git a/src/Extension/Extension.php b/src/Extension/Extension.php index 1abb6febe..255a7c6df 100644 --- a/src/Extension/Extension.php +++ b/src/Extension/Extension.php @@ -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. * diff --git a/src/Extension/ExtensionManager.php b/src/Extension/ExtensionManager.php index 404638fd5..ad0df4278 100644 --- a/src/Extension/ExtensionManager.php +++ b/src/Extension/ExtensionManager.php @@ -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 []; + } + }); } /** diff --git a/src/Extension/ExtensionServiceProvider.php b/src/Extension/ExtensionServiceProvider.php index 77b88d3e5..78b8be201 100644 --- a/src/Extension/ExtensionServiceProvider.php +++ b/src/Extension/ExtensionServiceProvider.php @@ -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); + } + }); } /**