From 22c911476b0e7dafd8671fb60b7bd2578e8a5b11 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 17 Jun 2018 10:44:39 +0930 Subject: [PATCH 1/2] Rename and improve FormatterConfiguration extender In the future we may have multiple Formatters, so by moving the config callback to its own instance method we can leave the constructor available to specify which formatter (like Assets and Routes). This format also allows for other methods to be added. Additionally, this adds logic to automatically flush the Formatter cache whenever the extension is enabled or disabled. --- framework/core/src/Extend/Formatter.php | 55 +++++++++++++++++++ .../src/Extend/FormatterConfiguration.php | 37 ------------- 2 files changed, 55 insertions(+), 37 deletions(-) create mode 100644 framework/core/src/Extend/Formatter.php delete mode 100644 framework/core/src/Extend/FormatterConfiguration.php diff --git a/framework/core/src/Extend/Formatter.php b/framework/core/src/Extend/Formatter.php new file mode 100644 index 000000000..3f0c75ff8 --- /dev/null +++ b/framework/core/src/Extend/Formatter.php @@ -0,0 +1,55 @@ + + * + * 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\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 +{ + protected $callback; + + public function configure(callable $callback) + { + $this->callback = $callback; + + return $this; + } + + public function __invoke(Container $container, Extension $extension = null) + { + $events = $container->make(Dispatcher::class); + + $events->listen( + Configuring::class, + function (Configuring $event) { + 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. + $flush = function ($event) use ($container, $extension) { + if ($event->extension === $extension) { + $container->make(ActualFormatter::class)->flush(); + } + }; + + $events->listen(Enabled::class, $flush); + $events->listen(Disabled::class, $flush); + } +} diff --git a/framework/core/src/Extend/FormatterConfiguration.php b/framework/core/src/Extend/FormatterConfiguration.php deleted file mode 100644 index 61a11d1a4..000000000 --- a/framework/core/src/Extend/FormatterConfiguration.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * 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 Flarum\Formatter\Event\Configuring; -use Illuminate\Contracts\Container\Container; -use Illuminate\Events\Dispatcher; - -class FormatterConfiguration implements ExtenderInterface -{ - protected $callback; - - public function __construct(callable $callback) - { - $this->callback = $callback; - } - - public function __invoke(Container $container, Extension $extension = null) - { - $container->make(Dispatcher::class)->listen( - Configuring::class, - function (Configuring $event) { - call_user_func($this->callback, $event->configurator); - } - ); - } -} From ca28f0ca45a71176316e2ac8a1aae82dd876c40e Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 19 Jun 2018 21:59:45 +0930 Subject: [PATCH 2/2] Merge event listener registration --- framework/core/src/Extend/Formatter.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/framework/core/src/Extend/Formatter.php b/framework/core/src/Extend/Formatter.php index 3f0c75ff8..13245db4a 100644 --- a/framework/core/src/Extend/Formatter.php +++ b/framework/core/src/Extend/Formatter.php @@ -43,13 +43,13 @@ class Formatter implements ExtenderInterface // Also set up an event listener to flush the formatter cache whenever // this extension is enabled or disabled. - $flush = function ($event) use ($container, $extension) { - if ($event->extension === $extension) { - $container->make(ActualFormatter::class)->flush(); + $events->listen( + [Enabled::class, Disabled::class], + function ($event) use ($container, $extension) { + if ($event->extension === $extension) { + $container->make(ActualFormatter::class)->flush(); + } } - }; - - $events->listen(Enabled::class, $flush); - $events->listen(Disabled::class, $flush); + ); } }