mirror of
https://github.com/flarum/framework.git
synced 2025-01-22 23:40:45 +08:00
Merge pull request #1581 from flarum/fl/1463-extension-install-hooks
Extension enable/disable hooks
This commit is contained in:
commit
692194ec7d
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -30,7 +28,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);
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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(
|
||||
|
|
22
framework/core/src/Extend/LifecycleInterface.php
Normal file
22
framework/core/src/Extend/LifecycleInterface.php
Normal 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);
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
@ -83,13 +84,6 @@ class Extension implements Arrayable
|
|||
*/
|
||||
protected $version;
|
||||
|
||||
/**
|
||||
* Whether the extension is enabled.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $enabled = false;
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param array $composerJson
|
||||
|
@ -121,7 +115,7 @@ class Extension implements Arrayable
|
|||
$extender = new Compat($extender);
|
||||
}
|
||||
|
||||
$extender($app, $this);
|
||||
$extender->extend($app, $this);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -223,23 +217,18 @@ class Extension implements Arrayable
|
|||
return $icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $enabled
|
||||
* @return Extension
|
||||
*/
|
||||
public function setEnabled($enabled)
|
||||
public function enable(Container $container)
|
||||
{
|
||||
$this->enabled = $enabled;
|
||||
|
||||
return $this;
|
||||
foreach ($this->getLifecycleExtenders() as $extender) {
|
||||
$extender->onEnable($container, $this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnabled()
|
||||
public function disable(Container $container)
|
||||
{
|
||||
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 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";
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
@ -113,25 +112,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->setEnabled(true);
|
||||
|
||||
$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 +144,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->setEnabled(false);
|
||||
|
||||
$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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -173,7 +173,7 @@ class InstalledSite implements SiteInterface
|
|||
$laravel->boot();
|
||||
|
||||
foreach ($this->extenders as $extension) {
|
||||
$extension($laravel);
|
||||
$extension->extend($laravel);
|
||||
}
|
||||
|
||||
return $laravel;
|
||||
|
|
Loading…
Reference in New Issue
Block a user