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 |
||
| 16 | class admin_plugin_config extends DokuWiki_Admin_Plugin { |
||
| 17 | |||
| 18 | const METADATA = __DIR__ . 'settings/config.metadata.php'; |
||
| 19 | const IMGDIR = DOKU_BASE.'lib/plugins/config/images/'; |
||
| 20 | |||
| 21 | protected $_config = null; |
||
| 22 | protected $_input = null; |
||
| 23 | protected $_changed = false; // set to true if configuration has altered |
||
| 24 | protected $_error = false; |
||
| 25 | protected $_session_started = false; |
||
| 26 | protected $_localised_prompts = false; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @return int |
||
| 30 | */ |
||
| 31 | public function getMenuSort() { return 100; } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * handle user request |
||
| 35 | */ |
||
| 36 | public function handle() { |
||
| 37 | global $ID, $INPUT; |
||
| 38 | |||
| 39 | if(!$this->_restore_session() || $INPUT->int('save') != 1 || !checkSecurityToken()) { |
||
| 40 | $this->_close_session(); |
||
| 41 | return; |
||
| 42 | } |
||
| 43 | |||
| 44 | if(is_null($this->_config)) { |
||
| 45 | $this->_config = new Configuration(self::METADATA); |
||
| 46 | } |
||
| 47 | |||
| 48 | // don't go any further if the configuration is locked |
||
| 49 | if($this->_config->locked) { |
||
| 50 | $this->_close_session(); |
||
| 51 | return; |
||
| 52 | } |
||
| 53 | |||
| 54 | $this->_input = $INPUT->arr('config'); |
||
| 55 | |||
| 56 | foreach ($this->_config->setting as $key => $value){ |
||
| 57 | $input = isset($this->_input[$key]) ? $this->_input[$key] : null; |
||
| 58 | if ($this->_config->setting[$key]->update($input)) { |
||
| 59 | $this->_changed = true; |
||
| 60 | } |
||
| 61 | if ($this->_config->setting[$key]->error()) $this->_error = true; |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($this->_changed && !$this->_error) { |
||
| 65 | $this->_config->save_settings($this->getPluginName()); |
||
| 66 | |||
| 67 | // save state & force a page reload to get the new settings to take effect |
||
| 68 | $_SESSION['PLUGIN_CONFIG'] = array('state' => 'updated', 'time' => time()); |
||
| 69 | $this->_close_session(); |
||
| 70 | send_redirect(wl($ID,array('do'=>'admin','page'=>'config'),true,'&')); |
||
| 71 | exit(); |
||
| 72 | } elseif(!$this->_error) { |
||
| 73 | $this->_config->touch_settings(); // just touch to refresh cache |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->_close_session(); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * output appropriate html |
||
| 81 | */ |
||
| 82 | public function html() { |
||
| 83 | $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. |
||
| 84 | global $lang; |
||
| 85 | global $ID; |
||
| 86 | |||
| 87 | if (is_null($this->_config)) { $this->_config = new Configuration(self::METADATA); } |
||
| 88 | $this->setupLocale(true); |
||
| 89 | |||
| 90 | print $this->locale_xhtml('intro'); |
||
| 91 | |||
| 92 | ptln('<div id="config__manager">'); |
||
| 93 | |||
| 94 | if ($this->_config->locked) |
||
| 95 | ptln('<div class="info">'.$this->getLang('locked').'</div>'); |
||
| 96 | elseif ($this->_error) |
||
| 97 | ptln('<div class="error">'.$this->getLang('error').'</div>'); |
||
| 98 | elseif ($this->_changed) |
||
| 99 | ptln('<div class="success">'.$this->getLang('updated').'</div>'); |
||
| 100 | |||
| 101 | // POST to script() instead of wl($ID) so config manager still works if |
||
| 102 | // rewrite config is broken. Add $ID as hidden field to remember |
||
| 103 | // current ID in most cases. |
||
| 104 | ptln('<form action="'.script().'" method="post">'); |
||
| 105 | ptln('<div class="no"><input type="hidden" name="id" value="'.$ID.'" /></div>'); |
||
| 106 | formSecurityToken(); |
||
| 107 | $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki')); |
||
| 108 | |||
| 109 | /** @var setting[] $undefined_settings */ |
||
| 110 | $undefined_settings = array(); |
||
| 111 | $in_fieldset = false; |
||
| 112 | $first_plugin_fieldset = true; |
||
| 113 | $first_template_fieldset = true; |
||
| 114 | foreach($this->_config->setting as $setting) { |
||
| 115 | if (is_a($setting, 'setting_hidden')) { |
||
| 116 | // skip hidden (and undefined) settings |
||
| 117 | if ($allow_debug && is_a($setting, 'setting_undefined')) { |
||
| 118 | $undefined_settings[] = $setting; |
||
| 119 | } else { |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | } else if (is_a($setting, 'setting_fieldset')) { |
||
| 123 | // config setting group |
||
| 124 | if ($in_fieldset) { |
||
| 125 | ptln(' </table>'); |
||
| 126 | ptln(' </div>'); |
||
| 127 | ptln(' </fieldset>'); |
||
| 128 | } else { |
||
| 129 | $in_fieldset = true; |
||
| 130 | } |
||
| 131 | if ($first_plugin_fieldset && substr($setting->_key, 0, 10)=='plugin'.Configuration::KEYMARKER) { |
||
|
|
|||
| 132 | $this->_print_h1('plugin_settings', $this->getLang('_header_plugin')); |
||
| 133 | $first_plugin_fieldset = false; |
||
| 134 | } else if ($first_template_fieldset && substr($setting->_key, 0, 7)=='tpl'.Configuration::KEYMARKER) { |
||
| 135 | $this->_print_h1('template_settings', $this->getLang('_header_template')); |
||
| 136 | $first_template_fieldset = false; |
||
| 137 | } |
||
| 138 | ptln(' <fieldset id="'.$setting->_key.'">'); |
||
| 139 | ptln(' <legend>'.$setting->prompt($this).'</legend>'); |
||
| 140 | ptln(' <div class="table">'); |
||
| 141 | ptln(' <table class="inline">'); |
||
| 142 | } else { |
||
| 143 | // config settings |
||
| 144 | list($label,$input) = $setting->html($this, $this->_error); |
||
| 145 | |||
| 146 | $class = $setting->is_default() |
||
| 147 | ? ' class="default"' |
||
| 148 | : ($setting->is_protected() ? ' class="protected"' : ''); |
||
| 149 | $error = $setting->error() |
||
| 150 | ? ' class="value error"' |
||
| 151 | : ' class="value"'; |
||
| 152 | $icon = $setting->caution() |
||
| 153 | ? '<img src="'.self::IMGDIR.$setting->caution().'.png" '. |
||
| 154 | 'alt="'.$setting->caution().'" title="'.$this->getLang($setting->caution()).'" />' |
||
| 155 | : ''; |
||
| 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')); |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return boolean true - proceed with handle, false - don't proceed |
||
| 234 | */ |
||
| 235 | protected function _restore_session() { |
||
| 260 | |||
| 261 | protected function _close_session() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param bool $prompts |
||
| 267 | */ |
||
| 268 | public function setupLocale($prompts=false) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | protected function _setup_localised_plugin_prompts() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Generates a two-level table of contents for the config plugin. |
||
| 333 | * |
||
| 334 | * @author Ben Coburn <[email protected]> |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | public function getTOC() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $id |
||
| 395 | * @param string $text |
||
| 396 | */ |
||
| 397 | protected function _print_h1($id, $text) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Adds a translation to this plugin's language array |
||
| 403 | * |
||
| 404 | * @param string $key |
||
| 405 | * @param string $value |
||
| 406 | */ |
||
| 407 | public function addLang($key, $value) { |
||
| 411 | } |
||
| 412 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.