diff --git a/.editorconfig b/.editorconfig index 0fb398499..e81de0fe0 100644 --- a/.editorconfig +++ b/.editorconfig @@ -18,7 +18,7 @@ trim_trailing_whitespace = false [*.{php,xml,json}] indent_size = 4 -[{tsconfig.json,prettierrc.json}] +[{tsconfig.json,prettierrc.json,package.json}] indent_size = 2 [*.neon] diff --git a/framework/core/js/src/admin/components/EditCustomCssModal.tsx b/framework/core/js/src/admin/components/EditCustomCssModal.tsx index 432961074..6c71e4172 100644 --- a/framework/core/js/src/admin/components/EditCustomCssModal.tsx +++ b/framework/core/js/src/admin/components/EditCustomCssModal.tsx @@ -1,7 +1,19 @@ import app from '../../admin/app'; -import SettingsModal from './SettingsModal'; +import SettingsModal, { type ISettingsModalAttrs } from './SettingsModal'; +import Mithril from 'mithril'; export default class EditCustomCssModal extends SettingsModal { + oninit(vnode: Mithril.Vnode) { + super.oninit(vnode); + + if (this.setting('custom_less_error')()) { + this.alertAttrs = { + type: 'error', + content: this.setting('custom_less_error')(), + }; + } + } + className() { return 'EditCustomCssModal TextareaCodeModal Modal--large'; } diff --git a/framework/core/src/Forum/ForumServiceProvider.php b/framework/core/src/Forum/ForumServiceProvider.php index dff421987..e8c05217a 100644 --- a/framework/core/src/Forum/ForumServiceProvider.php +++ b/framework/core/src/Forum/ForumServiceProvider.php @@ -133,7 +133,7 @@ class ForumServiceProvider extends AbstractServiceProvider $sources->addFile(__DIR__.'/../../less/forum.less'); $sources->addString(function () use ($container) { return $container->make(SettingsRepositoryInterface::class)->get('custom_less', ''); - }); + }, 'custom_less'); }); $container->make(AddTranslations::class)->forFrontend('forum')->to($assets); @@ -195,7 +195,8 @@ class ForumServiceProvider extends AbstractServiceProvider $container->make('flarum.assets.forum'), $container->make('flarum.locales'), $container, - $container->make('flarum.less.config') + $container->make(SettingsRepositoryInterface::class), + $container->make('flarum.less.config'), ); $validator->whenSettingsSaved($event); } @@ -208,7 +209,8 @@ class ForumServiceProvider extends AbstractServiceProvider $container->make('flarum.assets.forum'), $container->make('flarum.locales'), $container, - $container->make('flarum.less.config') + $container->make(SettingsRepositoryInterface::class), + $container->make('flarum.less.config'), ); $validator->whenSettingsSaving($event); } diff --git a/framework/core/src/Forum/ValidateCustomLess.php b/framework/core/src/Forum/ValidateCustomLess.php index e397a13d9..0ed456f68 100644 --- a/framework/core/src/Forum/ValidateCustomLess.php +++ b/framework/core/src/Forum/ValidateCustomLess.php @@ -32,7 +32,8 @@ class ValidateCustomLess protected Assets $assets, protected LocaleManager $locales, protected Container $container, - protected array $customLessSettings = [] + protected SettingsRepositoryInterface $settings, + protected array $customLessSettings = [], ) { } @@ -72,6 +73,8 @@ class ValidateCustomLess $adapter = new InMemoryFilesystemAdapter(); $this->assets->setAssetsDir(new FilesystemAdapter(new Filesystem($adapter), $adapter)); + $this->settings->delete('custom_less_error'); + try { $this->assets->makeCss()->commit(); @@ -82,6 +85,10 @@ class ValidateCustomLess throw new ValidationException(['custom_less' => $e->getMessage()]); } + if (! empty($this->settings->get('custom_less_error'))) { + throw new ValidationException(['custom_less' => $this->settings->get('custom_less_error')]); + } + $this->assets->setAssetsDir($assetsDir); $this->container->instance(SettingsRepositoryInterface::class, $settings); } diff --git a/framework/core/src/Frontend/Compiler/LessCompiler.php b/framework/core/src/Frontend/Compiler/LessCompiler.php index 734e4c59c..0644ccc00 100644 --- a/framework/core/src/Frontend/Compiler/LessCompiler.php +++ b/framework/core/src/Frontend/Compiler/LessCompiler.php @@ -12,6 +12,7 @@ namespace Flarum\Frontend\Compiler; use Flarum\Frontend\Compiler\Source\FileSource; use Illuminate\Support\Collection; use Illuminate\Support\Str; +use Less_Exception_Compiler; use Less_FileManager; use Less_Parser; use Less_Tree_Import; @@ -71,6 +72,10 @@ class LessCompiler extends RevisionCompiler return ''; } + if (! empty($this->settings->get('custom_less_error'))) { + unset($sources['custom_less']); + } + ini_set('xdebug.max_nesting_level', '200'); $parser = new Less_Parser([ @@ -96,7 +101,27 @@ class LessCompiler extends RevisionCompiler $parser->registerFunction($name, $callback); } - return $this->finalize($parser->getCss()); + try { + $compiled = $this->finalize($parser->getCss()); + + if (isset($sources['custom_less'])) { + $this->settings->delete('custom_less_error'); + } + + return $compiled; + } catch (Less_Exception_Compiler $e) { + if (isset($sources['custom_less'])) { + unset($sources['custom_less']); + + $compiled = $this->compile($sources); + + $this->settings->set('custom_less_error', $e->getMessage()); + + return $compiled; + } + + throw $e; + } } protected function finalize(string $parsedCss): string diff --git a/framework/core/src/Frontend/Compiler/RevisionCompiler.php b/framework/core/src/Frontend/Compiler/RevisionCompiler.php index e47ef9bd7..4cd5aad5f 100644 --- a/framework/core/src/Frontend/Compiler/RevisionCompiler.php +++ b/framework/core/src/Frontend/Compiler/RevisionCompiler.php @@ -13,6 +13,7 @@ use Flarum\Frontend\Compiler\Concerns\HasSources; use Flarum\Frontend\Compiler\Source\FileSource; use Flarum\Frontend\Compiler\Source\SourceInterface; use Flarum\Frontend\Compiler\Source\StringSource; +use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Filesystem\Cloud; /** @@ -29,6 +30,7 @@ class RevisionCompiler implements CompilerInterface public function __construct( protected Cloud $assetsDir, protected string $filename, + protected SettingsRepositoryInterface $settings ) { $this->versioner = new FileVersioner($assetsDir); } diff --git a/framework/core/src/Frontend/Compiler/Source/SourceCollector.php b/framework/core/src/Frontend/Compiler/Source/SourceCollector.php index 66cb52be4..4bd1e0435 100644 --- a/framework/core/src/Frontend/Compiler/Source/SourceCollector.php +++ b/framework/core/src/Frontend/Compiler/Source/SourceCollector.php @@ -35,12 +35,18 @@ class SourceCollector return $this; } - public function addString(Closure $callback): static + public function addString(Closure $callback, ?string $key = null): static { - $this->sources[] = $this->validateSourceType( + $source = $this->validateSourceType( new StringSource($callback) ); + if (! empty($key)) { + $this->sources[$key] = $source; + } else { + $this->sources[] = $source; + } + return $this; }