Merge pull request #1581 from flarum/fl/1463-extension-install-hooks

Extension enable/disable hooks
This commit is contained in:
Toby Zerner 2018-09-29 08:19:26 +09:30 committed by GitHub
commit 692194ec7d
13 changed files with 100 additions and 95 deletions

View File

@ -67,11 +67,7 @@ class MigrateCommand extends AbstractCommand
$extensions = $this->container->make('Flarum\Extension\ExtensionManager'); $extensions = $this->container->make('Flarum\Extension\ExtensionManager');
$extensions->getMigrator()->setOutput($this->output); $extensions->getMigrator()->setOutput($this->output);
foreach ($extensions->getExtensions() as $name => $extension) { foreach ($extensions->getEnabledExtensions() as $name => $extension) {
if (! $extension->isEnabled()) {
continue;
}
$this->info('Migrating extension: '.$name); $this->info('Migrating extension: '.$name);
$extensions->migrate($extension); $extensions->migrate($extension);

View File

@ -32,7 +32,7 @@ class Compat implements ExtenderInterface
$this->callback = $callback; $this->callback = $callback;
} }
public function __invoke(Container $container, Extension $extension = null) public function extend(Container $container, Extension $extension = null)
{ {
$container->call($this->callback); $container->call($this->callback);
} }

View File

@ -16,5 +16,5 @@ use Illuminate\Contracts\Container\Container;
interface ExtenderInterface interface ExtenderInterface
{ {
public function __invoke(Container $container, Extension $extension = null); public function extend(Container $container, Extension $extension = null);
} }

View File

@ -11,15 +11,13 @@
namespace Flarum\Extend; namespace Flarum\Extend;
use Flarum\Extension\Event\Disabled;
use Flarum\Extension\Event\Enabled;
use Flarum\Extension\Extension; use Flarum\Extension\Extension;
use Flarum\Formatter\Event\Configuring; use Flarum\Formatter\Event\Configuring;
use Flarum\Formatter\Formatter as ActualFormatter; use Flarum\Formatter\Formatter as ActualFormatter;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Illuminate\Events\Dispatcher; use Illuminate\Events\Dispatcher;
class Formatter implements ExtenderInterface class Formatter implements ExtenderInterface, LifecycleInterface
{ {
protected $callback; protected $callback;
@ -30,7 +28,7 @@ class Formatter implements ExtenderInterface
return $this; return $this;
} }
public function __invoke(Container $container, Extension $extension = null) public function extend(Container $container, Extension $extension = null)
{ {
$events = $container->make(Dispatcher::class); $events = $container->make(Dispatcher::class);
@ -40,16 +38,17 @@ class Formatter implements ExtenderInterface
call_user_func($this->callback, $event->configurator); call_user_func($this->callback, $event->configurator);
} }
); );
}
// Also set up an event listener to flush the formatter cache whenever public function onEnable(Container $container, Extension $extension)
// this extension is enabled or disabled. {
$events->listen( // FLush the formatter cache when this extension is enabled
[Enabled::class, Disabled::class],
function ($event) use ($container, $extension) {
if ($event->extension === $extension) {
$container->make(ActualFormatter::class)->flush(); $container->make(ActualFormatter::class)->flush();
} }
}
); public function onDisable(Container $container, Extension $extension)
{
// FLush the formatter cache when this extension is disabled
$container->make(ActualFormatter::class)->flush();
} }
} }

View File

@ -51,7 +51,7 @@ class Frontend implements ExtenderInterface
return $this; return $this;
} }
public function __invoke(Container $container, Extension $extension = null) public function extend(Container $container, Extension $extension = null)
{ {
$this->registerAssets($container, $this->getModuleName($extension)); $this->registerAssets($container, $this->getModuleName($extension));
$this->registerRoutes($container); $this->registerRoutes($container);

View File

@ -20,7 +20,7 @@ use RuntimeException;
class LanguagePack implements ExtenderInterface class LanguagePack implements ExtenderInterface
{ {
public function __invoke(Container $container, Extension $extension = null) public function extend(Container $container, Extension $extension = null)
{ {
if (is_null($extension)) { if (is_null($extension)) {
throw new InvalidArgumentException( throw new InvalidArgumentException(

View File

@ -0,0 +1,22 @@
<?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 Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
interface LifecycleInterface
{
public function onEnable(Container $container, Extension $extension);
public function onDisable(Container $container, Extension $extension);
}

View File

@ -25,7 +25,7 @@ class Locales implements ExtenderInterface
$this->directory = $directory; $this->directory = $directory;
} }
public function __invoke(Container $container, Extension $extension = null) public function extend(Container $container, Extension $extension = null)
{ {
/** @var LocaleManager $locales */ /** @var LocaleManager $locales */
$locales = $container->make(LocaleManager::class); $locales = $container->make(LocaleManager::class);

View File

@ -63,7 +63,7 @@ class Routes implements ExtenderInterface
return $this; return $this;
} }
public function __invoke(Container $container, Extension $extension = null) public function extend(Container $container, Extension $extension = null)
{ {
if (empty($this->routes)) { if (empty($this->routes)) {
return; return;

View File

@ -12,6 +12,7 @@
namespace Flarum\Extension; namespace Flarum\Extension;
use Flarum\Extend\Compat; use Flarum\Extend\Compat;
use Flarum\Extend\LifecycleInterface;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
@ -83,13 +84,6 @@ class Extension implements Arrayable
*/ */
protected $version; protected $version;
/**
* Whether the extension is enabled.
*
* @var bool
*/
protected $enabled = false;
/** /**
* @param $path * @param $path
* @param array $composerJson * @param array $composerJson
@ -121,7 +115,7 @@ class Extension implements Arrayable
$extender = new Compat($extender); $extender = new Compat($extender);
} }
$extender($app, $this); $extender->extend($app, $this);
} }
} }
@ -223,23 +217,18 @@ class Extension implements Arrayable
return $icon; return $icon;
} }
/** public function enable(Container $container)
* @param bool $enabled
* @return Extension
*/
public function setEnabled($enabled)
{ {
$this->enabled = $enabled; foreach ($this->getLifecycleExtenders() as $extender) {
$extender->onEnable($container, $this);
return $this; }
} }
/** public function disable(Container $container)
* @return bool
*/
public function isEnabled()
{ {
return $this->enabled; foreach ($this->getLifecycleExtenders() as $extender) {
$extender->onDisable($container, $this);
}
} }
/** /**
@ -277,6 +266,19 @@ class Extension implements Arrayable
return array_flatten($extenders); return array_flatten($extenders);
} }
/**
* @return LifecycleInterface[]
*/
private function getLifecycleExtenders(): array
{
return array_filter(
$this->getExtenders(),
function ($extender) {
return $extender instanceof LifecycleInterface;
}
);
}
private function getExtenderFile(): ?string private function getExtenderFile(): ?string
{ {
$filename = "{$this->path}/extend.php"; $filename = "{$this->path}/extend.php";

View File

@ -83,7 +83,6 @@ class ExtensionManager
// Per default all extensions are installed if they are registered in composer. // Per default all extensions are installed if they are registered in composer.
$extension->setInstalled(true); $extension->setInstalled(true);
$extension->setVersion(Arr::get($package, 'version')); $extension->setVersion(Arr::get($package, 'version'));
$extension->setEnabled($this->isEnabled($extension->getId()));
$extensions->put($extension->getId(), $extension); $extensions->put($extension->getId(), $extension);
} }
@ -113,7 +112,10 @@ class ExtensionManager
*/ */
public function enable($name) public function enable($name)
{ {
if (! $this->isEnabled($name)) { if ($this->isEnabled($name)) {
return;
}
$extension = $this->getExtension($name); $extension = $this->getExtension($name);
$this->dispatcher->dispatch(new Enabling($extension)); $this->dispatcher->dispatch(new Enabling($extension));
@ -128,11 +130,10 @@ class ExtensionManager
$this->setEnabled($enabled); $this->setEnabled($enabled);
$extension->setEnabled(true); $extension->enable($this->app);
$this->dispatcher->dispatch(new Enabled($extension)); $this->dispatcher->dispatch(new Enabled($extension));
} }
}
/** /**
* Disables an extension. * Disables an extension.
@ -143,7 +144,10 @@ class ExtensionManager
{ {
$enabled = $this->getEnabled(); $enabled = $this->getEnabled();
if (($k = array_search($name, $enabled)) !== false) { if (($k = array_search($name, $enabled)) === false) {
return;
}
$extension = $this->getExtension($name); $extension = $this->getExtension($name);
$this->dispatcher->dispatch(new Disabling($extension)); $this->dispatcher->dispatch(new Disabling($extension));
@ -152,11 +156,10 @@ class ExtensionManager
$this->setEnabled($enabled); $this->setEnabled($enabled);
$extension->setEnabled(false); $extension->disable($this->app);
$this->dispatcher->dispatch(new Disabled($extension)); $this->dispatcher->dispatch(new Disabled($extension));
} }
}
/** /**
* Uninstalls an extension. * Uninstalls an extension.

View File

@ -11,23 +11,11 @@
namespace Flarum\Formatter; namespace Flarum\Formatter;
use Flarum\Extension\Event\Disabled;
use Flarum\Extension\Event\Enabled;
use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\AbstractServiceProvider;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
class FormatterServiceProvider extends AbstractServiceProvider class FormatterServiceProvider extends AbstractServiceProvider
{ {
/**
* {@inheritdoc}
*/
public function boot(Dispatcher $events)
{
$events->listen(Enabled::class, [$this, 'flushFormatter']);
$events->listen(Disabled::class, [$this, 'flushFormatter']);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -43,9 +31,4 @@ class FormatterServiceProvider extends AbstractServiceProvider
$this->app->alias('flarum.formatter', Formatter::class); $this->app->alias('flarum.formatter', Formatter::class);
} }
public function flushFormatter()
{
$this->app->make('flarum.formatter')->flush();
}
} }

View File

@ -173,7 +173,7 @@ class InstalledSite implements SiteInterface
$laravel->boot(); $laravel->boot();
foreach ($this->extenders as $extension) { foreach ($this->extenders as $extension) {
$extension($laravel); $extension->extend($laravel);
} }
return $laravel; return $laravel;