From 3c827d2fced991680682204380d6b9ea4014cf25 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 26 Sep 2018 22:36:36 +0200 Subject: [PATCH 1/6] Tweak extender interface in preparation for adding more methods --- src/Extend/Compat.php | 2 +- src/Extend/ExtenderInterface.php | 2 +- src/Extend/Formatter.php | 2 +- src/Extend/Frontend.php | 2 +- src/Extend/LanguagePack.php | 2 +- src/Extend/Locales.php | 2 +- src/Extend/Routes.php | 2 +- src/Extension/Extension.php | 2 +- src/Foundation/InstalledSite.php | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Extend/Compat.php b/src/Extend/Compat.php index a50d89191..3f1b0b88e 100644 --- a/src/Extend/Compat.php +++ b/src/Extend/Compat.php @@ -32,7 +32,7 @@ class Compat implements ExtenderInterface $this->callback = $callback; } - public function __invoke(Container $container, Extension $extension = null) + public function extend(Container $container, Extension $extension = null) { $container->call($this->callback); } diff --git a/src/Extend/ExtenderInterface.php b/src/Extend/ExtenderInterface.php index 6f9bfc7c9..26c58fa92 100644 --- a/src/Extend/ExtenderInterface.php +++ b/src/Extend/ExtenderInterface.php @@ -16,5 +16,5 @@ use Illuminate\Contracts\Container\Container; interface ExtenderInterface { - public function __invoke(Container $container, Extension $extension = null); + public function extend(Container $container, Extension $extension = null); } diff --git a/src/Extend/Formatter.php b/src/Extend/Formatter.php index 13245db4a..243f54d81 100644 --- a/src/Extend/Formatter.php +++ b/src/Extend/Formatter.php @@ -30,7 +30,7 @@ class Formatter implements ExtenderInterface return $this; } - public function __invoke(Container $container, Extension $extension = null) + public function extend(Container $container, Extension $extension = null) { $events = $container->make(Dispatcher::class); diff --git a/src/Extend/Frontend.php b/src/Extend/Frontend.php index 45282316a..1b77d541b 100644 --- a/src/Extend/Frontend.php +++ b/src/Extend/Frontend.php @@ -51,7 +51,7 @@ class Frontend implements ExtenderInterface 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->registerRoutes($container); diff --git a/src/Extend/LanguagePack.php b/src/Extend/LanguagePack.php index c8a7f0c09..e579bfaee 100644 --- a/src/Extend/LanguagePack.php +++ b/src/Extend/LanguagePack.php @@ -20,7 +20,7 @@ use RuntimeException; class LanguagePack implements ExtenderInterface { - public function __invoke(Container $container, Extension $extension = null) + public function extend(Container $container, Extension $extension = null) { if (is_null($extension)) { throw new InvalidArgumentException( diff --git a/src/Extend/Locales.php b/src/Extend/Locales.php index 8b2b83e97..02a3d52a9 100644 --- a/src/Extend/Locales.php +++ b/src/Extend/Locales.php @@ -25,7 +25,7 @@ class Locales implements ExtenderInterface $this->directory = $directory; } - public function __invoke(Container $container, Extension $extension = null) + public function extend(Container $container, Extension $extension = null) { /** @var LocaleManager $locales */ $locales = $container->make(LocaleManager::class); diff --git a/src/Extend/Routes.php b/src/Extend/Routes.php index c84cde797..2d3cc71ab 100644 --- a/src/Extend/Routes.php +++ b/src/Extend/Routes.php @@ -63,7 +63,7 @@ class Routes implements ExtenderInterface return $this; } - public function __invoke(Container $container, Extension $extension = null) + public function extend(Container $container, Extension $extension = null) { if (empty($this->routes)) { return; diff --git a/src/Extension/Extension.php b/src/Extension/Extension.php index 74038eae8..89b0a90a4 100644 --- a/src/Extension/Extension.php +++ b/src/Extension/Extension.php @@ -121,7 +121,7 @@ class Extension implements Arrayable $extender = new Compat($extender); } - $extender($app, $this); + $extender->extend($app, $this); } } diff --git a/src/Foundation/InstalledSite.php b/src/Foundation/InstalledSite.php index 3aa8ca415..2362fa11f 100644 --- a/src/Foundation/InstalledSite.php +++ b/src/Foundation/InstalledSite.php @@ -173,7 +173,7 @@ class InstalledSite implements SiteInterface $laravel->boot(); foreach ($this->extenders as $extension) { - $extension($laravel); + $extension->extend($laravel); } return $laravel; From f48101dc0416141138bc44f59ea8804958775142 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 26 Sep 2018 22:46:44 +0200 Subject: [PATCH 2/6] Add a new extender interface for extension lifecycle hooks --- src/Extend/Formatter.php | 25 +++++++++++------------ src/Extend/LifecycleInterface.php | 22 ++++++++++++++++++++ src/Extension/Extension.php | 32 ++++++++++++++++++++++++++++++ src/Extension/ExtensionManager.php | 4 ++-- 4 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 src/Extend/LifecycleInterface.php diff --git a/src/Extend/Formatter.php b/src/Extend/Formatter.php index 243f54d81..1ae2a6ed8 100644 --- a/src/Extend/Formatter.php +++ b/src/Extend/Formatter.php @@ -11,15 +11,13 @@ namespace Flarum\Extend; -use Flarum\Extension\Event\Disabled; -use Flarum\Extension\Event\Enabled; use Flarum\Extension\Extension; use Flarum\Formatter\Event\Configuring; use Flarum\Formatter\Formatter as ActualFormatter; use Illuminate\Contracts\Container\Container; use Illuminate\Events\Dispatcher; -class Formatter implements ExtenderInterface +class Formatter implements ExtenderInterface, LifecycleInterface { protected $callback; @@ -40,16 +38,17 @@ class Formatter implements ExtenderInterface call_user_func($this->callback, $event->configurator); } ); + } - // Also set up an event listener to flush the formatter cache whenever - // this extension is enabled or disabled. - $events->listen( - [Enabled::class, Disabled::class], - function ($event) use ($container, $extension) { - if ($event->extension === $extension) { - $container->make(ActualFormatter::class)->flush(); - } - } - ); + public function onEnable(Container $container, Extension $extension) + { + // FLush the formatter cache when this extension is enabled + $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(); } } diff --git a/src/Extend/LifecycleInterface.php b/src/Extend/LifecycleInterface.php new file mode 100644 index 000000000..cbe8a96e4 --- /dev/null +++ b/src/Extend/LifecycleInterface.php @@ -0,0 +1,22 @@ + + * + * 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); +} diff --git a/src/Extension/Extension.php b/src/Extension/Extension.php index 89b0a90a4..f29885ce8 100644 --- a/src/Extension/Extension.php +++ b/src/Extension/Extension.php @@ -12,6 +12,7 @@ namespace Flarum\Extension; use Flarum\Extend\Compat; +use Flarum\Extend\LifecycleInterface; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; @@ -223,6 +224,24 @@ class Extension implements Arrayable return $icon; } + public function enable(Container $container) + { + $this->setEnabled(true); + + foreach ($this->getLifecycleExtenders() as $extender) { + $extender->onEnable($container, $this); + } + } + + public function disable(Container $container) + { + $this->setEnabled(false); + + foreach ($this->getLifecycleExtenders() as $extender) { + $extender->onDisable($container, $this); + } + } + /** * @param bool $enabled * @return Extension @@ -277,6 +296,19 @@ class Extension implements Arrayable 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 { $filename = "{$this->path}/extend.php"; diff --git a/src/Extension/ExtensionManager.php b/src/Extension/ExtensionManager.php index 6ca5aed90..f5f4c9117 100644 --- a/src/Extension/ExtensionManager.php +++ b/src/Extension/ExtensionManager.php @@ -128,7 +128,7 @@ class ExtensionManager $this->setEnabled($enabled); - $extension->setEnabled(true); + $extension->enable($this->app); $this->dispatcher->dispatch(new Enabled($extension)); } @@ -152,7 +152,7 @@ class ExtensionManager $this->setEnabled($enabled); - $extension->setEnabled(false); + $extension->disable($this->app); $this->dispatcher->dispatch(new Disabled($extension)); } From 86215005015dd2f5c1205f631ec8e4c04cba7ac2 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 26 Sep 2018 22:59:48 +0200 Subject: [PATCH 3/6] Use early returns to flatten methods --- src/Extension/ExtensionManager.php | 64 ++++++++++++++++-------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/Extension/ExtensionManager.php b/src/Extension/ExtensionManager.php index f5f4c9117..5a8d62d82 100644 --- a/src/Extension/ExtensionManager.php +++ b/src/Extension/ExtensionManager.php @@ -113,25 +113,27 @@ class ExtensionManager */ public function enable($name) { - if (! $this->isEnabled($name)) { - $extension = $this->getExtension($name); - - $this->dispatcher->dispatch(new Enabling($extension)); - - $enabled = $this->getEnabled(); - - $enabled[] = $name; - - $this->migrate($extension); - - $this->publishAssets($extension); - - $this->setEnabled($enabled); - - $extension->enable($this->app); - - $this->dispatcher->dispatch(new Enabled($extension)); + if ($this->isEnabled($name)) { + return; } + + $extension = $this->getExtension($name); + + $this->dispatcher->dispatch(new Enabling($extension)); + + $enabled = $this->getEnabled(); + + $enabled[] = $name; + + $this->migrate($extension); + + $this->publishAssets($extension); + + $this->setEnabled($enabled); + + $extension->enable($this->app); + + $this->dispatcher->dispatch(new Enabled($extension)); } /** @@ -143,19 +145,21 @@ class ExtensionManager { $enabled = $this->getEnabled(); - if (($k = array_search($name, $enabled)) !== false) { - $extension = $this->getExtension($name); - - $this->dispatcher->dispatch(new Disabling($extension)); - - unset($enabled[$k]); - - $this->setEnabled($enabled); - - $extension->disable($this->app); - - $this->dispatcher->dispatch(new Disabled($extension)); + if (($k = array_search($name, $enabled)) === false) { + return; } + + $extension = $this->getExtension($name); + + $this->dispatcher->dispatch(new Disabling($extension)); + + unset($enabled[$k]); + + $this->setEnabled($enabled); + + $extension->disable($this->app); + + $this->dispatcher->dispatch(new Disabled($extension)); } /** From b823a9df479890463274678c97c3c9fb983af7bb Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 26 Sep 2018 23:03:48 +0200 Subject: [PATCH 4/6] migrate: Use existing public API to filter extensions --- src/Database/Console/MigrateCommand.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Database/Console/MigrateCommand.php b/src/Database/Console/MigrateCommand.php index 6340b3d67..1e0f1b051 100644 --- a/src/Database/Console/MigrateCommand.php +++ b/src/Database/Console/MigrateCommand.php @@ -67,11 +67,7 @@ class MigrateCommand extends AbstractCommand $extensions = $this->container->make('Flarum\Extension\ExtensionManager'); $extensions->getMigrator()->setOutput($this->output); - foreach ($extensions->getExtensions() as $name => $extension) { - if (! $extension->isEnabled()) { - continue; - } - + foreach ($extensions->getEnabledExtensions() as $name => $extension) { $this->info('Migrating extension: '.$name); $extensions->migrate($extension); From 3b70b9e76e74480ddf28bf1a54cfc7d721faca01 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 26 Sep 2018 23:11:27 +0200 Subject: [PATCH 5/6] Let extensions take care of flushing the formatter cache --- src/Formatter/FormatterServiceProvider.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/Formatter/FormatterServiceProvider.php b/src/Formatter/FormatterServiceProvider.php index 25de9fe55..8ed73457b 100644 --- a/src/Formatter/FormatterServiceProvider.php +++ b/src/Formatter/FormatterServiceProvider.php @@ -11,23 +11,11 @@ namespace Flarum\Formatter; -use Flarum\Extension\Event\Disabled; -use Flarum\Extension\Event\Enabled; use Flarum\Foundation\AbstractServiceProvider; use Illuminate\Contracts\Container\Container; -use Illuminate\Contracts\Events\Dispatcher; class FormatterServiceProvider extends AbstractServiceProvider { - /** - * {@inheritdoc} - */ - public function boot(Dispatcher $events) - { - $events->listen(Enabled::class, [$this, 'flushFormatter']); - $events->listen(Disabled::class, [$this, 'flushFormatter']); - } - /** * {@inheritdoc} */ @@ -43,9 +31,4 @@ class FormatterServiceProvider extends AbstractServiceProvider $this->app->alias('flarum.formatter', Formatter::class); } - - public function flushFormatter() - { - $this->app->make('flarum.formatter')->flush(); - } } From f03c954dcc682d5302f4f15efbf46cdc6eea3aa6 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 26 Sep 2018 23:34:33 +0200 Subject: [PATCH 6/6] Extensions do not need to know whether they are enabled --- src/Extension/Extension.php | 30 ------------------------------ src/Extension/ExtensionManager.php | 1 - 2 files changed, 31 deletions(-) diff --git a/src/Extension/Extension.php b/src/Extension/Extension.php index f29885ce8..a85792961 100644 --- a/src/Extension/Extension.php +++ b/src/Extension/Extension.php @@ -84,13 +84,6 @@ class Extension implements Arrayable */ protected $version; - /** - * Whether the extension is enabled. - * - * @var bool - */ - protected $enabled = false; - /** * @param $path * @param array $composerJson @@ -226,8 +219,6 @@ class Extension implements Arrayable public function enable(Container $container) { - $this->setEnabled(true); - foreach ($this->getLifecycleExtenders() as $extender) { $extender->onEnable($container, $this); } @@ -235,32 +226,11 @@ class Extension implements Arrayable public function disable(Container $container) { - $this->setEnabled(false); - foreach ($this->getLifecycleExtenders() as $extender) { $extender->onDisable($container, $this); } } - /** - * @param bool $enabled - * @return Extension - */ - public function setEnabled($enabled) - { - $this->enabled = $enabled; - - return $this; - } - - /** - * @return bool - */ - public function isEnabled() - { - return $this->enabled; - } - /** * The raw path of the directory under extensions. * diff --git a/src/Extension/ExtensionManager.php b/src/Extension/ExtensionManager.php index 5a8d62d82..62e4e0502 100644 --- a/src/Extension/ExtensionManager.php +++ b/src/Extension/ExtensionManager.php @@ -83,7 +83,6 @@ class ExtensionManager // Per default all extensions are installed if they are registered in composer. $extension->setInstalled(true); $extension->setVersion(Arr::get($package, 'version')); - $extension->setEnabled($this->isEnabled($extension->getId())); $extensions->put($extension->getId(), $extension); }