Default colors plugin: generate CSS file during initialization

Current behaviour only generate the custom CSS file when the plugin settings are saved, which can be annoying if the file is deleted but the settings are set.
Most common use case is Docker deployment, because the plugin directory is not mounted as a volume.
This commit is contained in:
ArthurHoaro 2020-08-29 09:38:30 +02:00
parent bea062149e
commit 972daa4513
2 changed files with 42 additions and 35 deletions

View file

@ -15,6 +15,8 @@
'DEFAULT_COLORS_DARK_MAIN', 'DEFAULT_COLORS_DARK_MAIN',
]; ];
const DEFAULT_COLORS_CSS_FILE = '/default_colors/default_colors.css';
/** /**
* Display an error if the plugin is active a no color is configured. * Display an error if the plugin is active a no color is configured.
* *
@ -24,41 +26,24 @@
*/ */
function default_colors_init($conf) function default_colors_init($conf)
{ {
$params = ''; $params = [];
foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) { foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) {
$params .= trim($conf->get('plugins.'. $placeholder, '')); $value = trim($conf->get('plugins.'. $placeholder, ''));
if (strlen($value) > 0) {
$params[$placeholder] = $value;
}
} }
if (empty($params)) { if (empty($params)) {
$error = t('Default colors plugin error: '. $error = t('Default colors plugin error: '.
'This plugin is active and no custom color is configured.'); 'This plugin is active and no custom color is configured.');
return array($error); return [$error];
}
}
/**
* When plugin parameters are saved, we regenerate the custom CSS file with provided settings.
*
* @param array $data $_POST array
*
* @return array Updated $_POST array
*/
function hook_default_colors_save_plugin_parameters($data)
{
$file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
$template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template');
$content = '';
foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) {
$content .= ! empty($data[$rule])
? default_colors_format_css_rule($data, $rule) .';'. PHP_EOL
: '';
} }
if (! empty($content)) { // Colors are defined but the custom CSS file does not exist -> generate it
file_put_contents($file, sprintf($template, $content)); if (!file_exists(PluginManager::$PLUGINS_PATH . DEFAULT_COLORS_CSS_FILE)) {
default_colors_generate_css_file($params);
} }
return $data;
} }
/** /**
@ -78,6 +63,27 @@ function hook_default_colors_render_includes($data)
return $data; return $data;
} }
/**
* Regenerate the custom CSS file with provided settings.
*
* @param array $params Plugin configuration (CSS rules)
*/
function default_colors_generate_css_file($params): void
{
$file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
$template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template');
$content = '';
foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) {
$content .= !empty($params[$rule])
? default_colors_format_css_rule($params, $rule) .';'. PHP_EOL
: '';
}
if (! empty($content)) {
file_put_contents($file, sprintf($template, $content));
}
}
/** /**
* Create a valid CSS rule from parameters settings and plugin parameter. * Create a valid CSS rule from parameters settings and plugin parameter.
* *

View file

@ -2,7 +2,6 @@
namespace Shaarli\Plugin\DefaultColors; namespace Shaarli\Plugin\DefaultColors;
use DateTime;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\LinkDB; use Shaarli\Bookmark\LinkDB;
use Shaarli\Config\ConfigManager; use Shaarli\Config\ConfigManager;
@ -57,6 +56,8 @@ public function testDefaultColorsInitNoError()
$conf->set('plugins.DEFAULT_COLORS_BACKGROUND', 'value'); $conf->set('plugins.DEFAULT_COLORS_BACKGROUND', 'value');
$errors = default_colors_init($conf); $errors = default_colors_init($conf);
$this->assertEmpty($errors); $this->assertEmpty($errors);
$this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
} }
/** /**
@ -72,9 +73,9 @@ public function testDefaultColorsInitError()
/** /**
* Test the save plugin parameters hook with all colors specified. * Test the save plugin parameters hook with all colors specified.
*/ */
public function testSavePluginParametersAll() public function testGenerateCssFile()
{ {
$post = [ $params = [
'other1' => true, 'other1' => true,
'DEFAULT_COLORS_MAIN' => 'blue', 'DEFAULT_COLORS_MAIN' => 'blue',
'DEFAULT_COLORS_BACKGROUND' => 'pink', 'DEFAULT_COLORS_BACKGROUND' => 'pink',
@ -82,7 +83,7 @@ public function testSavePluginParametersAll()
'DEFAULT_COLORS_DARK_MAIN' => 'green', 'DEFAULT_COLORS_DARK_MAIN' => 'green',
]; ];
hook_default_colors_save_plugin_parameters($post); default_colors_generate_css_file($params);
$this->assertFileExists($file = 'sandbox/default_colors/default_colors.css'); $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
$content = file_get_contents($file); $content = file_get_contents($file);
$expected = ':root { $expected = ':root {
@ -98,16 +99,16 @@ public function testSavePluginParametersAll()
/** /**
* Test the save plugin parameters hook with only one color specified. * Test the save plugin parameters hook with only one color specified.
*/ */
public function testSavePluginParametersSingle() public function testGenerateCssFileSingle()
{ {
$post = [ $params = [
'other1' => true, 'other1' => true,
'DEFAULT_COLORS_BACKGROUND' => 'pink', 'DEFAULT_COLORS_BACKGROUND' => 'pink',
'other2' => ['yep'], 'other2' => ['yep'],
'DEFAULT_COLORS_DARK_MAIN' => '', 'DEFAULT_COLORS_DARK_MAIN' => '',
]; ];
hook_default_colors_save_plugin_parameters($post); default_colors_generate_css_file($params);
$this->assertFileExists($file = 'sandbox/default_colors/default_colors.css'); $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
$content = file_get_contents($file); $content = file_get_contents($file);
$expected = ':root { $expected = ':root {
@ -121,9 +122,9 @@ public function testSavePluginParametersSingle()
/** /**
* Test the save plugin parameters hook with no color specified. * Test the save plugin parameters hook with no color specified.
*/ */
public function testSavePluginParametersNone() public function testGenerateCssFileNone()
{ {
hook_default_colors_save_plugin_parameters([]); default_colors_generate_css_file([]);
$this->assertFileNotExists($file = 'sandbox/default_colors/default_colors.css'); $this->assertFileNotExists($file = 'sandbox/default_colors/default_colors.css');
} }