mirror of
https://github.com/flarum/framework.git
synced 2024-12-02 23:23:52 +08:00
Merge pull request #1032 from dav-is/patch-1
Prevent deletion of default/all locale(s)
This commit is contained in:
commit
59226000da
|
@ -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');
|
||||
|
||||
|
|
42
framework/core/src/Core/Listener/ExtensionValidator.php
Normal file
42
framework/core/src/Core/Listener/ExtensionValidator.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?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\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!');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
30
framework/core/src/Event/ExtensionWillBeDisabled.php
Normal file
30
framework/core/src/Event/ExtensionWillBeDisabled.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?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\Event;
|
||||
|
||||
use Flarum\Extension\Extension;
|
||||
|
||||
class ExtensionWillBeDisabled
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $extension;
|
||||
|
||||
/**
|
||||
* @param Extension $extension
|
||||
*/
|
||||
public function __construct(Extension $extension)
|
||||
{
|
||||
$this->extension = $extension;
|
||||
}
|
||||
}
|
30
framework/core/src/Event/ExtensionWillBeEnabled.php
Normal file
30
framework/core/src/Event/ExtensionWillBeEnabled.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?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\Event;
|
||||
|
||||
use Flarum\Extension\Extension;
|
||||
|
||||
class ExtensionWillBeEnabled
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $extension;
|
||||
|
||||
/**
|
||||
* @param Extension $extension
|
||||
*/
|
||||
public function __construct(Extension $extension)
|
||||
{
|
||||
$this->extension = $extension;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user