Complex classes like admin_plugin_config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use admin_plugin_config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class admin_plugin_config extends DokuWiki_Admin_Plugin { |
||
26 | |||
27 | protected $_file = PLUGIN_METADATA; |
||
28 | protected $_config = null; |
||
29 | protected $_input = null; |
||
30 | protected $_changed = false; // set to true if configuration has altered |
||
31 | protected $_error = false; |
||
32 | protected $_session_started = false; |
||
33 | protected $_localised_prompts = false; |
||
34 | |||
35 | /** |
||
36 | * @return int |
||
37 | */ |
||
38 | public function getMenuSort() { return 100; } |
||
39 | |||
40 | /** |
||
41 | * handle user request |
||
42 | */ |
||
43 | public function handle() { |
||
44 | global $ID, $INPUT; |
||
45 | |||
46 | if(!$this->_restore_session() || $INPUT->int('save') != 1 || !checkSecurityToken()) { |
||
47 | $this->_close_session(); |
||
48 | return; |
||
49 | } |
||
50 | |||
51 | if(is_null($this->_config)) { |
||
52 | $this->_config = new configuration($this->_file); |
||
53 | } |
||
54 | |||
55 | // don't go any further if the configuration is locked |
||
56 | if($this->_config->locked) { |
||
57 | $this->_close_session(); |
||
58 | return; |
||
59 | } |
||
60 | |||
61 | $this->_input = $INPUT->arr('config'); |
||
62 | |||
63 | foreach ($this->_config->setting as $key => $value){ |
||
64 | $input = isset($this->_input[$key]) ? $this->_input[$key] : null; |
||
65 | if ($this->_config->setting[$key]->update($input)) { |
||
66 | $this->_changed = true; |
||
67 | } |
||
68 | if ($this->_config->setting[$key]->error()) $this->_error = true; |
||
69 | } |
||
70 | |||
71 | if ($this->_changed && !$this->_error) { |
||
72 | $this->_config->save_settings($this->getPluginName()); |
||
73 | |||
74 | // save state & force a page reload to get the new settings to take effect |
||
75 | $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time()); |
||
76 | $this->_close_session(); |
||
77 | send_redirect(wl($ID, 'do=admin&page=config', true, '&')); |
||
78 | exit(); |
||
79 | } elseif(!$this->_error) { |
||
80 | $this->_config->touch_settings(); // just touch to refresh cache |
||
81 | } |
||
82 | |||
83 | $this->_close_session(); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * output appropriate html |
||
88 | */ |
||
89 | public function html() { |
||
90 | $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. |
||
91 | global $lang; |
||
92 | global $ID; |
||
93 | |||
94 | if (is_null($this->_config)) { $this->_config = new configuration($this->_file); } |
||
95 | $this->setupLocale(true); |
||
96 | |||
97 | print $this->locale_xhtml('intro'); |
||
98 | |||
99 | ptln('<div id="config__manager">'); |
||
100 | |||
101 | if ($this->_config->locked) |
||
102 | ptln('<div class="info">'.$this->getLang('locked').'</div>'); |
||
103 | elseif ($this->_error) |
||
104 | ptln('<div class="error">'.$this->getLang('error').'</div>'); |
||
105 | elseif ($this->_changed) |
||
106 | ptln('<div class="success">'.$this->getLang('updated').'</div>'); |
||
107 | |||
108 | // POST to script() instead of wl($ID) so config manager still works if |
||
109 | // rewrite config is broken. Add $ID as hidden field to remember |
||
110 | // current ID in most cases. |
||
111 | ptln('<form action="'.script().'" method="post">'); |
||
112 | ptln('<div class="no"><input type="hidden" name="id" value="'.$ID.'" /></div>'); |
||
113 | formSecurityToken(); |
||
114 | $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki')); |
||
115 | |||
116 | /** @var setting[] $undefined_settings */ |
||
117 | $undefined_settings = array(); |
||
118 | $in_fieldset = false; |
||
119 | $first_plugin_fieldset = true; |
||
120 | $first_template_fieldset = true; |
||
121 | foreach($this->_config->setting as $setting) { |
||
122 | if (is_a($setting, 'setting_hidden')) { |
||
123 | // skip hidden (and undefined) settings |
||
124 | if ($allow_debug && is_a($setting, 'setting_undefined')) { |
||
125 | $undefined_settings[] = $setting; |
||
126 | } else { |
||
127 | continue; |
||
128 | } |
||
129 | } else if (is_a($setting, 'setting_fieldset')) { |
||
130 | // config setting group |
||
131 | if ($in_fieldset) { |
||
132 | ptln(' </table>'); |
||
133 | ptln(' </div>'); |
||
134 | ptln(' </fieldset>'); |
||
135 | } else { |
||
136 | $in_fieldset = true; |
||
137 | } |
||
138 | if ($first_plugin_fieldset && substr($setting->_key, 0, 10)=='plugin'.CM_KEYMARKER) { |
||
139 | $this->_print_h1('plugin_settings', $this->getLang('_header_plugin')); |
||
140 | $first_plugin_fieldset = false; |
||
141 | } else if ($first_template_fieldset && substr($setting->_key, 0, 7)=='tpl'.CM_KEYMARKER) { |
||
142 | $this->_print_h1('template_settings', $this->getLang('_header_template')); |
||
143 | $first_template_fieldset = false; |
||
144 | } |
||
145 | ptln(' <fieldset id="'.$setting->_key.'">'); |
||
146 | ptln(' <legend>'.$setting->prompt($this).'</legend>'); |
||
147 | ptln(' <div class="table">'); |
||
148 | ptln(' <table class="inline">'); |
||
149 | } else { |
||
150 | // config settings |
||
151 | list($label,$input) = $setting->html($this, $this->_error); |
||
152 | |||
153 | $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : ''); |
||
154 | $error = $setting->error() ? ' class="value error"' : ' class="value"'; |
||
155 | $icon = $setting->caution() ? '<img src="'.DOKU_PLUGIN_IMAGES.$setting->caution().'.png" alt="'.$setting->caution().'" title="'.$this->getLang($setting->caution()).'" />' : ''; |
||
156 | |||
157 | ptln(' <tr'.$class.'>'); |
||
158 | ptln(' <td class="label">'); |
||
159 | ptln(' <span class="outkey">'.$setting->_out_key(true, true).'</span>'); |
||
160 | ptln(' '.$icon.$label); |
||
161 | ptln(' </td>'); |
||
162 | ptln(' <td'.$error.'>'.$input.'</td>'); |
||
163 | ptln(' </tr>'); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | ptln(' </table>'); |
||
168 | ptln(' </div>'); |
||
169 | if ($in_fieldset) { |
||
170 | ptln(' </fieldset>'); |
||
171 | } |
||
172 | |||
173 | // show undefined settings list |
||
174 | if ($allow_debug && !empty($undefined_settings)) { |
||
175 | /** |
||
176 | * Callback for sorting settings |
||
177 | * |
||
178 | * @param setting $a |
||
179 | * @param setting $b |
||
180 | * @return int if $a is lower/equal/higher than $b |
||
181 | */ |
||
182 | function _setting_natural_comparison($a, $b) { |
||
185 | |||
186 | usort($undefined_settings, '_setting_natural_comparison'); |
||
187 | $this->_print_h1('undefined_settings', $this->getLang('_header_undefined')); |
||
224 | |||
225 | /** |
||
226 | * @return boolean true - proceed with handle, false - don't proceed |
||
227 | */ |
||
228 | protected function _restore_session() { |
||
253 | |||
254 | protected function _close_session() { |
||
257 | |||
258 | /** |
||
259 | * @param bool $prompts |
||
260 | */ |
||
261 | public function setupLocale($prompts=false) { |
||
270 | |||
271 | /** |
||
272 | * @return bool |
||
273 | */ |
||
274 | protected function _setup_localised_plugin_prompts() { |
||
323 | |||
324 | /** |
||
325 | * Generates a two-level table of contents for the config plugin. |
||
326 | * |
||
327 | * @author Ben Coburn <[email protected]> |
||
328 | * |
||
329 | * @return array |
||
330 | */ |
||
331 | public function getTOC() { |
||
385 | |||
386 | /** |
||
387 | * @param string $id |
||
388 | * @param string $text |
||
389 | */ |
||
390 | protected function _print_h1($id, $text) { |
||
393 | |||
394 | /** |
||
395 | * Adds a translation to this plugin's language array |
||
396 | * |
||
397 | * @param string $key |
||
398 | * @param string $value |
||
399 | */ |
||
400 | public function addLang($key, $value) { |
||
404 | } |
||
405 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.