Allow extensions to return a callback instead of a provider name

This is useful for very simple extensions like language packs, because it means no Composer/namespacing and thus bootstrap.php doesn't have to be changed at all.
This commit is contained in:
Toby Zerner 2015-09-25 16:01:34 +09:30
parent 575ff76002
commit 7889b15f09
2 changed files with 10 additions and 23 deletions

View File

@ -53,8 +53,6 @@ class ExtensionManager
$enabled[] = $extension;
$this->load($extension);
$this->migrate($extension);
$this->setEnabled($enabled);
@ -76,8 +74,6 @@ class ExtensionManager
{
$this->disable($extension);
$this->load($extension);
$this->migrateDown($extension);
}
@ -125,15 +121,6 @@ class ExtensionManager
return in_array($extension, $this->getEnabled());
}
protected function load($extension)
{
if (file_exists($file = $this->getExtensionsDir() . '/' . $extension . '/bootstrap.php')) {
$className = require $file;
$ext = new $className($this->app);
}
}
protected function getExtensionsDir()
{
return public_path('extensions');

View File

@ -23,19 +23,19 @@ class ExtensionsServiceProvider extends ServiceProvider
$config = $this->app->make('Flarum\Core\Settings\SettingsRepository')->get('extensions_enabled');
$extensions = json_decode($config, true);
$providers = [];
foreach ($extensions as $extension) {
if (file_exists($file = public_path().'/extensions/'.$extension.'/bootstrap.php')) {
$providerName = require $file;
$providers[$extension] = $this->app->register($providerName);
}
}
$events = $this->app->make('events');
foreach ($providers as $provider) {
$provider->listen($events);
foreach ($extensions as $extension) {
if (file_exists($file = public_path().'/extensions/'.$extension.'/bootstrap.php')) {
$provider = require $file;
if (is_string($provider)) {
$this->app->register($provider)->listen($events);
} elseif (is_callable($provider)) {
$provider($events);
}
}
}
}
}