mirror of
https://github.com/flarum/framework.git
synced 2024-11-29 12:43:52 +08:00
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.
This commit is contained in:
parent
0bcc6e74a8
commit
79b00cb94f
55
src/Extend/Formatter.php
Normal file
55
src/Extend/Formatter.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?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\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);
|
||||
}
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
<?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 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);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user