diff --git a/src/Core/Listener/ExtensionValidator.php b/src/Core/Listener/ExtensionValidator.php new file mode 100644 index 000000000..142004bfc --- /dev/null +++ b/src/Core/Listener/ExtensionValidator.php @@ -0,0 +1,51 @@ + + * + * 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\ExtensionWillBeEnabled; +use Flarum\Event\ExtensionWillBeDisabled; +use Illuminate\Contracts\Events\Dispatcher; +use Flarum\Http\Exception\MethodNotAllowedException; + +class ExtensionModificationValidator +{ + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(ExtensionWillBeEnabled::class, [$this, 'whenExtensionWillBeEnabled']); + $events->listen(ExtensionWillBeDisabled::class, [$this, 'whenExtensionWillBeDisabled']); + } + + /** + * @param ExtensionWillBeEnabled $event + */ + public function whenExtensionWillBeEnabled(ExtensionWillBeEnabled $event) + { + + } + + /** + * @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 remove all your language packs!'); + } + } + } +}