diff --git a/framework/core/src/Core/CoreServiceProvider.php b/framework/core/src/Core/CoreServiceProvider.php index 2cc1b4c44..d6c3f7de2 100644 --- a/framework/core/src/Core/CoreServiceProvider.php +++ b/framework/core/src/Core/CoreServiceProvider.php @@ -106,6 +106,7 @@ class CoreServiceProvider extends AbstractServiceProvider $events->subscribe('Flarum\Core\Listener\DiscussionMetadataUpdater'); $events->subscribe('Flarum\Core\Listener\UserMetadataUpdater'); + $events->subscribe('Flarum\Core\Listener\ExtensionValidator'); $events->subscribe('Flarum\Core\Listener\EmailConfirmationMailer'); $events->subscribe('Flarum\Core\Listener\DiscussionRenamedNotifier'); diff --git a/framework/core/src/Core/Listener/ExtensionValidator.php b/framework/core/src/Core/Listener/ExtensionValidator.php new file mode 100644 index 000000000..2f25af476 --- /dev/null +++ b/framework/core/src/Core/Listener/ExtensionValidator.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Core\Listener; + +use Flarum\Event\ExtensionWillBeDisabled; +use Flarum\Http\Exception\MethodNotAllowedException; +use Illuminate\Contracts\Events\Dispatcher; + +class ExtensionValidator +{ + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(ExtensionWillBeDisabled::class, [$this, 'whenExtensionWillBeDisabled']); + } + + /** + * @param ExtensionWillBeDisabled $event + * @throws MethodNotAllowedException + */ + public function whenExtensionWillBeDisabled(ExtensionWillBeDisabled $event) + { + if (in_array('flarum-locale', $event->extension->extra)) { + $default_locale = $this->app->make('flarum.settings')->get('default_locale'); + $locale = array_get($event->extension->extra, 'flarum-locale.code'); + if ($locale === $default_locale) { + throw new MethodNotAllowedException('You cannot disable the default language pack!'); + } + } + } +} diff --git a/framework/core/src/Event/ExtensionWillBeDisabled.php b/framework/core/src/Event/ExtensionWillBeDisabled.php new file mode 100644 index 000000000..6df58a41c --- /dev/null +++ b/framework/core/src/Event/ExtensionWillBeDisabled.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Event; + +use Flarum\Extension\Extension; + +class ExtensionWillBeDisabled +{ + /** + * @var string + */ + protected $extension; + + /** + * @param Extension $extension + */ + public function __construct(Extension $extension) + { + $this->extension = $extension; + } +} diff --git a/framework/core/src/Event/ExtensionWillBeEnabled.php b/framework/core/src/Event/ExtensionWillBeEnabled.php new file mode 100644 index 000000000..e0a9214e3 --- /dev/null +++ b/framework/core/src/Event/ExtensionWillBeEnabled.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Event; + +use Flarum\Extension\Extension; + +class ExtensionWillBeEnabled +{ + /** + * @var string + */ + protected $extension; + + /** + * @param Extension $extension + */ + public function __construct(Extension $extension) + { + $this->extension = $extension; + } +} diff --git a/framework/core/src/Extension/ExtensionManager.php b/framework/core/src/Extension/ExtensionManager.php index 9ea1e9e2d..f0e62838c 100644 --- a/framework/core/src/Extension/ExtensionManager.php +++ b/framework/core/src/Extension/ExtensionManager.php @@ -15,6 +15,8 @@ use Flarum\Database\Migrator; use Flarum\Event\ExtensionWasDisabled; use Flarum\Event\ExtensionWasEnabled; use Flarum\Event\ExtensionWasUninstalled; +use Flarum\Event\ExtensionWillBeDisabled; +use Flarum\Event\ExtensionWillBeEnabled; use Flarum\Foundation\Application; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Events\Dispatcher; @@ -112,6 +114,8 @@ class ExtensionManager { if (! $this->isEnabled($name)) { $extension = $this->getExtension($name); + + $this->dispatcher->fire(new ExtensionWillBeEnabled($extension)); $enabled = $this->getEnabled(); @@ -139,9 +143,11 @@ class ExtensionManager $enabled = $this->getEnabled(); if (($k = array_search($name, $enabled)) !== false) { - unset($enabled[$k]); - $extension = $this->getExtension($name); + + $this->dispatcher->fire(new ExtensionWillBeDisabled($extension)); + + unset($enabled[$k]); $this->setEnabled($enabled);