1
0
mirror of https://github.com/flarum/core.git synced 2025-08-17 22:01:44 +02:00

feat: Allow registering settings as Less config vars through Settings Extender (#3011)

Co-authored-by: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com>
This commit is contained in:
Sami Mazouz
2021-11-01 15:41:19 +01:00
committed by GitHub
parent 2b163025d6
commit d8e7aa54b4
8 changed files with 163 additions and 17 deletions

View File

@@ -21,6 +21,7 @@ class Settings implements ExtenderInterface
{
private $settings = [];
private $defaults = [];
private $lessConfigs = [];
/**
* Serialize a setting value to the ForumSerializer attributes.
@@ -61,6 +62,28 @@ class Settings implements ExtenderInterface
return $this;
}
/**
* Register a setting as a LESS configuration variable.
*
* @param string $configName: The name of the configuration variable, in hyphen case.
* @param string $key: The key of the setting.
* @param string|callable|null $callback: Optional callback to modify the value.
*
* The callback can be a closure or an invokable class, and should accept:
* - mixed $value: The value of the setting.
*
* The callable should return:
* - mixed $value: The modified value.
*
* @return self
*/
public function registerLessConfigVar(string $configName, string $key, $callback = null): self
{
$this->lessConfigs[$configName] = compact('key', 'callback');
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
if (! empty($this->defaults)) {
@@ -97,5 +120,19 @@ class Settings implements ExtenderInterface
}
);
}
if (! empty($this->lessConfigs)) {
$container->extend('flarum.less.config', function (array $existingConfig, Container $container) {
$config = $this->lessConfigs;
foreach ($config as $var => $data) {
if (isset($data['callback'])) {
$config[$var]['callback'] = ContainerUtil::wrapCallback($data['callback'], $container);
}
}
return array_merge($existingConfig, $config);
});
}
}
}