mirror of
https://github.com/flarum/framework.git
synced 2025-01-23 03:27:30 +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 = $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);
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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],
|
$container->make(ActualFormatter::class)->flush();
|
||||||
function ($event) use ($container, $extension) {
|
}
|
||||||
if ($event->extension === $extension) {
|
|
||||||
$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;
|
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);
|
||||||
|
|
|
@ -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(
|
||||||
|
|
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;
|
$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);
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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";
|
||||||
|
|
|
@ -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,25 +112,27 @@ class ExtensionManager
|
||||||
*/
|
*/
|
||||||
public function enable($name)
|
public function enable($name)
|
||||||
{
|
{
|
||||||
if (! $this->isEnabled($name)) {
|
if ($this->isEnabled($name)) {
|
||||||
$extension = $this->getExtension($name);
|
return;
|
||||||
|
|
||||||
$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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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();
|
$enabled = $this->getEnabled();
|
||||||
|
|
||||||
if (($k = array_search($name, $enabled)) !== false) {
|
if (($k = array_search($name, $enabled)) === false) {
|
||||||
$extension = $this->getExtension($name);
|
return;
|
||||||
|
|
||||||
$this->dispatcher->dispatch(new Disabling($extension));
|
|
||||||
|
|
||||||
unset($enabled[$k]);
|
|
||||||
|
|
||||||
$this->setEnabled($enabled);
|
|
||||||
|
|
||||||
$extension->setEnabled(false);
|
|
||||||
|
|
||||||
$this->dispatcher->dispatch(new Disabled($extension));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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;
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user