]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Default colors plugin: generate CSS file during initialization 1523/head
authorArthurHoaro <arthur@hoa.ro>
Sat, 29 Aug 2020 07:38:30 +0000 (09:38 +0200)
committerArthurHoaro <arthur@hoa.ro>
Sat, 29 Aug 2020 07:38:30 +0000 (09:38 +0200)
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.

plugins/default_colors/default_colors.php
tests/plugins/PluginDefaultColorsTest.php

index 1928cc9f49b527e8aadddf64ed9796d9628bacc7..e1fd5cfbdb19daf547519af2d7f41482c88f7b96 100644 (file)
@@ -15,6 +15,8 @@ const DEFAULT_COLORS_PLACEHOLDERS = [
     '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.
  *
@@ -24,58 +26,62 @@ const DEFAULT_COLORS_PLACEHOLDERS = [
  */
 function default_colors_init($conf)
 {
-    $params = '';
+    $params = [];
     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)) {
         $error = t('Default colors plugin error: '.
             'This plugin is active and no custom color is configured.');
-        return array($error);
+        return [$error];
+    }
+
+    // Colors are defined but the custom CSS file does not exist -> generate it
+    if (!file_exists(PluginManager::$PLUGINS_PATH . DEFAULT_COLORS_CSS_FILE)) {
+        default_colors_generate_css_file($params);
     }
 }
 
 /**
- * When plugin parameters are saved, we regenerate the custom CSS file with provided settings.
+ * When linklist is displayed, include default_colors CSS file.
  *
- * @param array $data $_POST array
+ * @param array $data - header data.
  *
- * @return array Updated $_POST array
+ * @return mixed - header data with default_colors CSS file added.
  */
-function hook_default_colors_save_plugin_parameters($data)
+function hook_default_colors_render_includes($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)) {
-        file_put_contents($file, sprintf($template, $content));
+    if (file_exists($file )) {
+        $data['css_files'][] = $file ;
     }
 
     return $data;
 }
 
 /**
- * When linklist is displayed, include default_colors CSS file.
- *
- * @param array $data - header data.
+ * Regenerate the custom CSS file with provided settings.
  *
- * @return mixed - header data with default_colors CSS file added.
+ * @param array $params Plugin configuration (CSS rules)
  */
-function hook_default_colors_render_includes($data)
+function default_colors_generate_css_file($params): void
 {
     $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
-    if (file_exists($file )) {
-        $data['css_files'][] = $file ;
+    $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
+            : '';
     }
 
-    return $data;
+    if (! empty($content)) {
+        file_put_contents($file, sprintf($template, $content));
+    }
 }
 
 /**
index b9951cca6cb7ce68b4e4cc2024cdce999c6894d0..9835dfa39ac7de4706d0f62b63cf86ed35f9d974 100644 (file)
@@ -2,7 +2,6 @@
 
 namespace Shaarli\Plugin\DefaultColors;
 
-use DateTime;
 use PHPUnit\Framework\TestCase;
 use Shaarli\Bookmark\LinkDB;
 use Shaarli\Config\ConfigManager;
@@ -57,6 +56,8 @@ class PluginDefaultColorsTest extends TestCase
         $conf->set('plugins.DEFAULT_COLORS_BACKGROUND', 'value');
         $errors = default_colors_init($conf);
         $this->assertEmpty($errors);
+
+        $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
     }
 
     /**
@@ -72,9 +73,9 @@ class PluginDefaultColorsTest extends TestCase
     /**
      * Test the save plugin parameters hook with all colors specified.
      */
-    public function testSavePluginParametersAll()
+    public function testGenerateCssFile()
     {
-        $post = [
+        $params = [
             'other1' => true,
             'DEFAULT_COLORS_MAIN' => 'blue',
             'DEFAULT_COLORS_BACKGROUND' => 'pink',
@@ -82,7 +83,7 @@ class PluginDefaultColorsTest extends TestCase
             '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');
         $content = file_get_contents($file);
         $expected = ':root {
@@ -98,16 +99,16 @@ class PluginDefaultColorsTest extends TestCase
     /**
      * Test the save plugin parameters hook with only one color specified.
      */
-    public function testSavePluginParametersSingle()
+    public function testGenerateCssFileSingle()
     {
-        $post = [
+        $params = [
             'other1' => true,
             'DEFAULT_COLORS_BACKGROUND' => 'pink',
             'other2' => ['yep'],
             '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');
         $content = file_get_contents($file);
         $expected = ':root {
@@ -121,9 +122,9 @@ class PluginDefaultColorsTest extends TestCase
     /**
      * 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');
     }