Early returns

This commit is contained in:
Franz Liedke 2018-12-13 21:58:00 +01:00
parent b58380e224
commit 4b00f7996b
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
2 changed files with 50 additions and 43 deletions

View File

@ -42,12 +42,15 @@ class DefaultLanguagePackGuard
*/
public function whenExtensionWillBeDisabled(Disabling $event)
{
if (in_array('flarum-locale', $event->extension->extra)) {
$defaultLocale = $this->settings->get('default_locale');
$locale = array_get($event->extension->extra, 'flarum-locale.code');
if ($locale === $defaultLocale) {
throw new ForbiddenException('You cannot disable the default language pack!');
}
if (! in_array('flarum-locale', $event->extension->extra)) {
return;
}
$defaultLocale = $this->settings->get('default_locale');
$locale = array_get($event->extension->extra, 'flarum-locale.code');
if ($locale === $defaultLocale) {
throw new ForbiddenException('You cannot disable the default language pack!');
}
}
}

View File

@ -69,39 +69,41 @@ class ValidateCustomLess
*/
public function whenSettingsSaving(Saving $event)
{
if (isset($event->settings['custom_less'])) {
// We haven't saved the settings yet, but we want to trial a full
// recompile of the CSS to see if this custom LESS will break
// anything. In order to do that, we will temporarily override the
// settings repository with the new settings so that the recompile
// is effective. We will also use a dummy filesystem so that nothing
// is actually written yet.
$settings = $this->container->make(SettingsRepositoryInterface::class);
$this->container->extend(
SettingsRepositoryInterface::class,
function ($settings) use ($event) {
return new OverrideSettingsRepository($settings, $event->settings);
}
);
$assetsDir = $this->assets->getAssetsDir();
$this->assets->setAssetsDir(new FilesystemAdapter(new Filesystem(new NullAdapter)));
try {
$this->assets->makeCss()->commit();
foreach ($this->locales->getLocales() as $locale => $name) {
$this->assets->makeLocaleCss($locale)->commit();
}
} catch (Less_Exception_Parser $e) {
throw new ValidationException(['custom_less' => $e->getMessage()]);
}
$this->assets->setAssetsDir($assetsDir);
$this->container->instance(SettingsRepositoryInterface::class, $settings);
if (! isset($event->settings['custom_less'])) {
return;
}
// We haven't saved the settings yet, but we want to trial a full
// recompile of the CSS to see if this custom LESS will break
// anything. In order to do that, we will temporarily override the
// settings repository with the new settings so that the recompile
// is effective. We will also use a dummy filesystem so that nothing
// is actually written yet.
$settings = $this->container->make(SettingsRepositoryInterface::class);
$this->container->extend(
SettingsRepositoryInterface::class,
function ($settings) use ($event) {
return new OverrideSettingsRepository($settings, $event->settings);
}
);
$assetsDir = $this->assets->getAssetsDir();
$this->assets->setAssetsDir(new FilesystemAdapter(new Filesystem(new NullAdapter)));
try {
$this->assets->makeCss()->commit();
foreach ($this->locales->getLocales() as $locale => $name) {
$this->assets->makeLocaleCss($locale)->commit();
}
} catch (Less_Exception_Parser $e) {
throw new ValidationException(['custom_less' => $e->getMessage()]);
}
$this->assets->setAssetsDir($assetsDir);
$this->container->instance(SettingsRepositoryInterface::class, $settings);
}
/**
@ -109,12 +111,14 @@ class ValidateCustomLess
*/
public function whenSettingsSaved(Saved $event)
{
if (isset($event->settings['custom_less'])) {
$this->assets->makeCss()->flush();
if (! isset($event->settings['custom_less'])) {
return;
}
foreach ($this->locales->getLocales() as $locale => $name) {
$this->assets->makeLocaleCss($locale)->flush();
}
$this->assets->makeCss()->flush();
foreach ($this->locales->getLocales() as $locale => $name) {
$this->assets->makeLocaleCss($locale)->flush();
}
}
}