diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-05-18 21:43:59 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-06-11 09:30:56 +0200 |
commit | 59404d7909b21682ec0782778452a8a70e38b25e (patch) | |
tree | 52bbe0b390ecd37f80128d1f8b4f80c3834734a8 /application/config/ConfigPlugin.php | |
parent | 823a363c3b2e10008a607c8b69c1a3d4e9b44ea1 (diff) | |
download | Shaarli-59404d7909b21682ec0782778452a8a70e38b25e.tar.gz Shaarli-59404d7909b21682ec0782778452a8a70e38b25e.tar.zst Shaarli-59404d7909b21682ec0782778452a8a70e38b25e.zip |
Introduce a configuration manager (not plugged yet)
Diffstat (limited to 'application/config/ConfigPlugin.php')
-rw-r--r-- | application/config/ConfigPlugin.php | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/application/config/ConfigPlugin.php b/application/config/ConfigPlugin.php new file mode 100644 index 00000000..8af89d04 --- /dev/null +++ b/application/config/ConfigPlugin.php | |||
@@ -0,0 +1,118 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Functions related to configuration management. | ||
4 | */ | ||
5 | |||
6 | /** | ||
7 | * Process plugin administration form data and save it in an array. | ||
8 | * | ||
9 | * @param array $formData Data sent by the plugin admin form. | ||
10 | * | ||
11 | * @return array New list of enabled plugin, ordered. | ||
12 | * | ||
13 | * @throws PluginConfigOrderException Plugins can't be sorted because their order is invalid. | ||
14 | */ | ||
15 | function save_plugin_config($formData) | ||
16 | { | ||
17 | // Make sure there are no duplicates in orders. | ||
18 | if (!validate_plugin_order($formData)) { | ||
19 | throw new PluginConfigOrderException(); | ||
20 | } | ||
21 | |||
22 | $plugins = array(); | ||
23 | $newEnabledPlugins = array(); | ||
24 | foreach ($formData as $key => $data) { | ||
25 | if (startsWith($key, 'order')) { | ||
26 | continue; | ||
27 | } | ||
28 | |||
29 | // If there is no order, it means a disabled plugin has been enabled. | ||
30 | if (isset($formData['order_' . $key])) { | ||
31 | $plugins[(int) $formData['order_' . $key]] = $key; | ||
32 | } | ||
33 | else { | ||
34 | $newEnabledPlugins[] = $key; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | // New enabled plugins will be added at the end of order. | ||
39 | $plugins = array_merge($plugins, $newEnabledPlugins); | ||
40 | |||
41 | // Sort plugins by order. | ||
42 | if (!ksort($plugins)) { | ||
43 | throw new PluginConfigOrderException(); | ||
44 | } | ||
45 | |||
46 | $finalPlugins = array(); | ||
47 | // Make plugins order continuous. | ||
48 | foreach ($plugins as $plugin) { | ||
49 | $finalPlugins[] = $plugin; | ||
50 | } | ||
51 | |||
52 | return $finalPlugins; | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * Validate plugin array submitted. | ||
57 | * Will fail if there is duplicate orders value. | ||
58 | * | ||
59 | * @param array $formData Data from submitted form. | ||
60 | * | ||
61 | * @return bool true if ok, false otherwise. | ||
62 | */ | ||
63 | function validate_plugin_order($formData) | ||
64 | { | ||
65 | $orders = array(); | ||
66 | foreach ($formData as $key => $value) { | ||
67 | // No duplicate order allowed. | ||
68 | if (in_array($value, $orders)) { | ||
69 | return false; | ||
70 | } | ||
71 | |||
72 | if (startsWith($key, 'order')) { | ||
73 | $orders[] = $value; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | return true; | ||
78 | } | ||
79 | |||
80 | /** | ||
81 | * Affect plugin parameters values into plugins array. | ||
82 | * | ||
83 | * @param mixed $plugins Plugins array ($plugins[<plugin_name>]['parameters']['param_name'] = <value>. | ||
84 | * @param mixed $conf Plugins configuration. | ||
85 | * | ||
86 | * @return mixed Updated $plugins array. | ||
87 | */ | ||
88 | function load_plugin_parameter_values($plugins, $conf) | ||
89 | { | ||
90 | $out = $plugins; | ||
91 | foreach ($plugins as $name => $plugin) { | ||
92 | if (empty($plugin['parameters'])) { | ||
93 | continue; | ||
94 | } | ||
95 | |||
96 | foreach ($plugin['parameters'] as $key => $param) { | ||
97 | if (!empty($conf[$key])) { | ||
98 | $out[$name]['parameters'][$key] = $conf[$key]; | ||
99 | } | ||
100 | } | ||
101 | } | ||
102 | |||
103 | return $out; | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * Exception used if an error occur while saving plugin configuration. | ||
108 | */ | ||
109 | class PluginConfigOrderException extends Exception | ||
110 | { | ||
111 | /** | ||
112 | * Construct exception. | ||
113 | */ | ||
114 | public function __construct() | ||
115 | { | ||
116 | $this->message = 'An error occurred while trying to save plugins loading order.'; | ||
117 | } | ||
118 | } | ||