| Total Complexity | 45 |
| Total Lines | 327 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ViewManager 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.
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 ViewManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ViewManager extends \Slim\Views\Twig |
||
| 22 | { |
||
| 23 | use \PHPPgAdmin\Traits\HelperTrait; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | const BASE_PATH = ContainerUtils::BASE_PATH; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | const THEME_PATH = ContainerUtils::THEME_PATH; |
||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | const SUBFOLDER = ContainerUtils::SUBFOLDER; |
||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const DEBUGMODE = ContainerUtils::DEBUGMODE; |
||
| 42 | |||
| 43 | public $appLangFiles = []; |
||
| 44 | |||
| 45 | public $appName = ''; |
||
| 46 | |||
| 47 | public $appVersion = ''; |
||
| 48 | |||
| 49 | public $form = ''; |
||
| 50 | |||
| 51 | public $href = ''; |
||
| 52 | |||
| 53 | public $lang = []; |
||
| 54 | |||
| 55 | public $conf; |
||
| 56 | |||
| 57 | public $phpMinVer; |
||
| 58 | |||
| 59 | public $postgresqlMinVer; |
||
| 60 | |||
| 61 | public $view; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \PHPPgAdmin\Misc |
||
| 65 | */ |
||
| 66 | public $misc; |
||
| 67 | |||
| 68 | protected $container; |
||
| 69 | |||
| 70 | private $_connection; |
||
|
|
|||
| 71 | |||
| 72 | private $_no_db_connection = false; |
||
| 73 | |||
| 74 | private $_reload_browser = false; |
||
| 75 | |||
| 76 | private $_data; |
||
| 77 | |||
| 78 | private $_database; |
||
| 79 | |||
| 80 | private $_server_id; |
||
| 81 | |||
| 82 | private $_server_info; |
||
| 83 | |||
| 84 | private $_error_msg = ''; |
||
| 85 | |||
| 86 | private static $instance = null; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param \Slim\Container $container The container |
||
| 90 | * @param mixed $path |
||
| 91 | * @param mixed $settings |
||
| 92 | * @param \Slim\Container $c |
||
| 93 | */ |
||
| 94 | public function __construct($path, $settings, \Slim\Container $c) |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | public function maybeRenderIframes($response, $subject, $query_string) |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Gets the theme from |
||
| 152 | * 1. The $_REQUEST global (when it's chosen from start screen) |
||
| 153 | * 2. Server specific config theme 3.- $_SESSION global (subsequent requests after 1.) 4.- $_COOKIE global (mostly |
||
| 154 | * fallback for $_SESSION after 1.- and 3.-) 5.- theme as set in config 6.- 'default' theme. |
||
| 155 | * |
||
| 156 | * @param array $conf The conf |
||
| 157 | * @param null|mixed $_server_info |
||
| 158 | * |
||
| 159 | * @return string the theme |
||
| 160 | */ |
||
| 161 | public function getTheme(array $conf, $_server_info = null) |
||
| 162 | { |
||
| 163 | $_theme = null; |
||
| 164 | // List of themes |
||
| 165 | $themefolders = $this->getThemeFolders(); |
||
| 166 | // Check if theme is in $_REQUEST, $_SESSION or $_COOKIE |
||
| 167 | // 1.- First priority: $_REQUEST, this happens when you use the selector |
||
| 168 | if (\array_key_exists('theme', $_REQUEST) && |
||
| 169 | \array_key_exists($_REQUEST['theme'], $themefolders)) { |
||
| 170 | $_theme = $_REQUEST['theme']; |
||
| 171 | } elseif ( // otherwise, see if there's a theme associated with this particular server |
||
| 172 | null !== $_server_info && |
||
| 173 | \array_key_exists('theme', $_server_info) && |
||
| 174 | \is_string($_server_info['theme']) && |
||
| 175 | \array_key_exists($_COOKIE['ppaTheme'], $themefolders)) { |
||
| 176 | $_theme = $_server_info['theme']; |
||
| 177 | } elseif (isset($_SESSION) && \array_key_exists('ppaTheme', $_SESSION) && |
||
| 178 | \array_key_exists($_SESSION['ppaTheme'], $themefolders)) { |
||
| 179 | // otherwise check $_SESSION |
||
| 180 | $_theme = $_SESSION['ppaTheme']; |
||
| 181 | } elseif (\array_key_exists('ppaTheme', $_COOKIE) && |
||
| 182 | \array_key_exists($_COOKIE['ppaTheme'], $themefolders)) { |
||
| 183 | // oterwise check $_COOKIE |
||
| 184 | $_theme = $_COOKIE['ppaTheme']; |
||
| 185 | } elseif ( // see if there's a valid theme set in config file |
||
| 186 | \array_key_exists('theme', $conf) && |
||
| 187 | \is_string($conf['theme']) && |
||
| 188 | \array_key_exists($conf['theme'], $themefolders)) { |
||
| 189 | $_theme = $conf['theme']; |
||
| 190 | } else { |
||
| 191 | // okay then, use default theme |
||
| 192 | $_theme = 'default'; |
||
| 193 | } |
||
| 194 | |||
| 195 | return $_theme; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Sets the form tracking variable. |
||
| 200 | */ |
||
| 201 | public function setForm() |
||
| 202 | { |
||
| 203 | $form = []; |
||
| 204 | |||
| 205 | if ($this->container->server) { |
||
| 206 | $form[] = \sprintf( |
||
| 207 | '<input type="hidden" name="%s" value="%s" />', |
||
| 208 | 'server', |
||
| 209 | \htmlspecialchars($this->container->server) |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | |||
| 213 | if ($this->container->database) { |
||
| 214 | $form[] = \sprintf( |
||
| 215 | '<input type="hidden" name="%s" value="%s" />', |
||
| 216 | 'database', |
||
| 217 | \htmlspecialchars($this->container->database) |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | if ($this->container->schema) { |
||
| 222 | $form[] = \sprintf( |
||
| 223 | '<input type="hidden" name="%s" value="%s" />', |
||
| 224 | 'schema', |
||
| 225 | \htmlspecialchars($this->container->schema) |
||
| 226 | ); |
||
| 227 | } |
||
| 228 | $this->form = \implode("\n", $form); |
||
| 229 | |||
| 230 | return $this->form; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Displays link to the context help. |
||
| 235 | * |
||
| 236 | * @param string $str the string that the context help is related to (already escaped) |
||
| 237 | * @param string $help help section identifier |
||
| 238 | * @param bool $do_print true to echo, false to return |
||
| 239 | */ |
||
| 240 | public function printHelp($str, $help = null, $do_print = true) |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Gets the help link. |
||
| 257 | * |
||
| 258 | * @param string $help The help subject |
||
| 259 | * |
||
| 260 | * @return string the help link |
||
| 261 | */ |
||
| 262 | public function getHelpLink($help) |
||
| 263 | { |
||
| 264 | return \htmlspecialchars( |
||
| 265 | $this->getSubfolder('help?help=') . |
||
| 266 | \urlencode($help) . |
||
| 267 | '&server=' . |
||
| 268 | \urlencode($this->misc->getServerId()) |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function icon($icon) |
||
| 273 | { |
||
| 274 | if (!\is_string($icon)) { |
||
| 275 | return ''; |
||
| 276 | } |
||
| 277 | |||
| 278 | $theme = $this->conf['theme']; |
||
| 279 | $path = 'assets/images/themes'; |
||
| 280 | $default_icon = \sprintf('%s/%s/default/DisconnectedServer.png', self::SUBFOLDER, $path); |
||
| 281 | |||
| 282 | if (\is_readable(\sprintf('%s/%s/%s/%s.png', self::BASE_PATH, $path, $theme, $icon))) { |
||
| 283 | return \sprintf('%s/%s/%s/%s.png', self::SUBFOLDER, $path, $theme, $icon); |
||
| 284 | } |
||
| 285 | |||
| 286 | if (\is_readable(\sprintf('%s/%s/%s/%s.gif', self::BASE_PATH, $path, $theme, $icon))) { |
||
| 287 | return \sprintf('%s/%s/%s/%s.gif', self::SUBFOLDER, $path, $theme, $icon); |
||
| 288 | } |
||
| 289 | |||
| 290 | if (\is_readable(\sprintf('%s/%s/%s/%s.ico', self::BASE_PATH, $path, $theme, $icon))) { |
||
| 291 | return \sprintf('%s/%s/%s/%s.ico', self::SUBFOLDER, $path, $theme, $icon); |
||
| 292 | } |
||
| 293 | |||
| 294 | if (\is_readable(\sprintf('%s/%s/default/%s.png', self::BASE_PATH, $path, $icon))) { |
||
| 295 | return \sprintf('%s/%s/default/%s.png', self::SUBFOLDER, $path, $icon); |
||
| 296 | } |
||
| 297 | |||
| 298 | if (\is_readable(\sprintf('%s/%s/default/%s.gif', self::BASE_PATH, $path, $icon))) { |
||
| 299 | return \sprintf('%s/%s/default/%s.gif', self::SUBFOLDER, $path, $icon); |
||
| 300 | } |
||
| 301 | |||
| 302 | if (\is_readable(\sprintf('%s/%s/default/%s.ico', self::BASE_PATH, $path, $icon))) { |
||
| 303 | return \sprintf('%s/%s/default/%s.ico', self::SUBFOLDER, $path, $icon); |
||
| 304 | } |
||
| 305 | |||
| 306 | return $default_icon; |
||
| 307 | } |
||
| 308 | |||
| 309 | private function getContainer() |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Traverse THEME_PATH, consider as theme folders those which |
||
| 316 | * contain a `global.css` stylesheet. |
||
| 317 | * |
||
| 318 | * @return array the theme folders |
||
| 319 | */ |
||
| 320 | private function getThemeFolders() |
||
| 350 |