aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-08-29 09:38:30 +0200
committerArthurHoaro <arthur@hoa.ro>2020-08-29 09:38:30 +0200
commit972daa4513420d3293d46a808ca2d5be0313e78b (patch)
treeaeafedfe73e020c3e798aebd7fd84e536e87c8c7
parentbea062149ebcb4663861edb1cc0a32faf85b273f (diff)
downloadShaarli-972daa4513420d3293d46a808ca2d5be0313e78b.tar.gz
Shaarli-972daa4513420d3293d46a808ca2d5be0313e78b.tar.zst
Shaarli-972daa4513420d3293d46a808ca2d5be0313e78b.zip
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.
-rw-r--r--plugins/default_colors/default_colors.php56
-rw-r--r--tests/plugins/PluginDefaultColorsTest.php19
2 files changed, 41 insertions, 34 deletions
diff --git a/plugins/default_colors/default_colors.php b/plugins/default_colors/default_colors.php
index 1928cc9f..e1fd5cfb 100644
--- a/plugins/default_colors/default_colors.php
+++ b/plugins/default_colors/default_colors.php
@@ -15,6 +15,8 @@ const DEFAULT_COLORS_PLACEHOLDERS = [
15 'DEFAULT_COLORS_DARK_MAIN', 15 'DEFAULT_COLORS_DARK_MAIN',
16]; 16];
17 17
18const DEFAULT_COLORS_CSS_FILE = '/default_colors/default_colors.css';
19
18/** 20/**
19 * Display an error if the plugin is active a no color is configured. 21 * Display an error if the plugin is active a no color is configured.
20 * 22 *
@@ -24,58 +26,62 @@ const DEFAULT_COLORS_PLACEHOLDERS = [
24 */ 26 */
25function default_colors_init($conf) 27function default_colors_init($conf)
26{ 28{
27 $params = ''; 29 $params = [];
28 foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) { 30 foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) {
29 $params .= trim($conf->get('plugins.'. $placeholder, '')); 31 $value = trim($conf->get('plugins.'. $placeholder, ''));
32 if (strlen($value) > 0) {
33 $params[$placeholder] = $value;
34 }
30 } 35 }
31 36
32 if (empty($params)) { 37 if (empty($params)) {
33 $error = t('Default colors plugin error: '. 38 $error = t('Default colors plugin error: '.
34 'This plugin is active and no custom color is configured.'); 39 'This plugin is active and no custom color is configured.');
35 return array($error); 40 return [$error];
41 }
42
43 // Colors are defined but the custom CSS file does not exist -> generate it
44 if (!file_exists(PluginManager::$PLUGINS_PATH . DEFAULT_COLORS_CSS_FILE)) {
45 default_colors_generate_css_file($params);
36 } 46 }
37} 47}
38 48
39/** 49/**
40 * When plugin parameters are saved, we regenerate the custom CSS file with provided settings. 50 * When linklist is displayed, include default_colors CSS file.
41 * 51 *
42 * @param array $data $_POST array 52 * @param array $data - header data.
43 * 53 *
44 * @return array Updated $_POST array 54 * @return mixed - header data with default_colors CSS file added.
45 */ 55 */
46function hook_default_colors_save_plugin_parameters($data) 56function hook_default_colors_render_includes($data)
47{ 57{
48 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css'; 58 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
49 $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template'); 59 if (file_exists($file )) {
50 $content = ''; 60 $data['css_files'][] = $file ;
51 foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) {
52 $content .= ! empty($data[$rule])
53 ? default_colors_format_css_rule($data, $rule) .';'. PHP_EOL
54 : '';
55 }
56
57 if (! empty($content)) {
58 file_put_contents($file, sprintf($template, $content));
59 } 61 }
60 62
61 return $data; 63 return $data;
62} 64}
63 65
64/** 66/**
65 * When linklist is displayed, include default_colors CSS file. 67 * Regenerate the custom CSS file with provided settings.
66 *
67 * @param array $data - header data.
68 * 68 *
69 * @return mixed - header data with default_colors CSS file added. 69 * @param array $params Plugin configuration (CSS rules)
70 */ 70 */
71function hook_default_colors_render_includes($data) 71function default_colors_generate_css_file($params): void
72{ 72{
73 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css'; 73 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
74 if (file_exists($file )) { 74 $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template');
75 $data['css_files'][] = $file ; 75 $content = '';
76 foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) {
77 $content .= !empty($params[$rule])
78 ? default_colors_format_css_rule($params, $rule) .';'. PHP_EOL
79 : '';
76 } 80 }
77 81
78 return $data; 82 if (! empty($content)) {
83 file_put_contents($file, sprintf($template, $content));
84 }
79} 85}
80 86
81/** 87/**
diff --git a/tests/plugins/PluginDefaultColorsTest.php b/tests/plugins/PluginDefaultColorsTest.php
index b9951cca..9835dfa3 100644
--- a/tests/plugins/PluginDefaultColorsTest.php
+++ b/tests/plugins/PluginDefaultColorsTest.php
@@ -2,7 +2,6 @@
2 2
3namespace Shaarli\Plugin\DefaultColors; 3namespace Shaarli\Plugin\DefaultColors;
4 4
5use DateTime;
6use PHPUnit\Framework\TestCase; 5use PHPUnit\Framework\TestCase;
7use Shaarli\Bookmark\LinkDB; 6use Shaarli\Bookmark\LinkDB;
8use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
@@ -57,6 +56,8 @@ class PluginDefaultColorsTest extends TestCase
57 $conf->set('plugins.DEFAULT_COLORS_BACKGROUND', 'value'); 56 $conf->set('plugins.DEFAULT_COLORS_BACKGROUND', 'value');
58 $errors = default_colors_init($conf); 57 $errors = default_colors_init($conf);
59 $this->assertEmpty($errors); 58 $this->assertEmpty($errors);
59
60 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
60 } 61 }
61 62
62 /** 63 /**
@@ -72,9 +73,9 @@ class PluginDefaultColorsTest extends TestCase
72 /** 73 /**
73 * Test the save plugin parameters hook with all colors specified. 74 * Test the save plugin parameters hook with all colors specified.
74 */ 75 */
75 public function testSavePluginParametersAll() 76 public function testGenerateCssFile()
76 { 77 {
77 $post = [ 78 $params = [
78 'other1' => true, 79 'other1' => true,
79 'DEFAULT_COLORS_MAIN' => 'blue', 80 'DEFAULT_COLORS_MAIN' => 'blue',
80 'DEFAULT_COLORS_BACKGROUND' => 'pink', 81 'DEFAULT_COLORS_BACKGROUND' => 'pink',
@@ -82,7 +83,7 @@ class PluginDefaultColorsTest extends TestCase
82 'DEFAULT_COLORS_DARK_MAIN' => 'green', 83 'DEFAULT_COLORS_DARK_MAIN' => 'green',
83 ]; 84 ];
84 85
85 hook_default_colors_save_plugin_parameters($post); 86 default_colors_generate_css_file($params);
86 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css'); 87 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
87 $content = file_get_contents($file); 88 $content = file_get_contents($file);
88 $expected = ':root { 89 $expected = ':root {
@@ -98,16 +99,16 @@ class PluginDefaultColorsTest extends TestCase
98 /** 99 /**
99 * Test the save plugin parameters hook with only one color specified. 100 * Test the save plugin parameters hook with only one color specified.
100 */ 101 */
101 public function testSavePluginParametersSingle() 102 public function testGenerateCssFileSingle()
102 { 103 {
103 $post = [ 104 $params = [
104 'other1' => true, 105 'other1' => true,
105 'DEFAULT_COLORS_BACKGROUND' => 'pink', 106 'DEFAULT_COLORS_BACKGROUND' => 'pink',
106 'other2' => ['yep'], 107 'other2' => ['yep'],
107 'DEFAULT_COLORS_DARK_MAIN' => '', 108 'DEFAULT_COLORS_DARK_MAIN' => '',
108 ]; 109 ];
109 110
110 hook_default_colors_save_plugin_parameters($post); 111 default_colors_generate_css_file($params);
111 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css'); 112 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
112 $content = file_get_contents($file); 113 $content = file_get_contents($file);
113 $expected = ':root { 114 $expected = ':root {
@@ -121,9 +122,9 @@ class PluginDefaultColorsTest extends TestCase
121 /** 122 /**
122 * Test the save plugin parameters hook with no color specified. 123 * Test the save plugin parameters hook with no color specified.
123 */ 124 */
124 public function testSavePluginParametersNone() 125 public function testGenerateCssFileNone()
125 { 126 {
126 hook_default_colors_save_plugin_parameters([]); 127 default_colors_generate_css_file([]);
127 $this->assertFileNotExists($file = 'sandbox/default_colors/default_colors.css'); 128 $this->assertFileNotExists($file = 'sandbox/default_colors/default_colors.css');
128 } 129 }
129 130