Total Complexity | 45 |
Total Lines | 322 |
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 | protected $container; |
||
64 | |||
65 | private $_connection; |
||
|
|||
66 | |||
67 | private $_no_db_connection = false; |
||
68 | |||
69 | private $_reload_browser = false; |
||
70 | |||
71 | private $_data; |
||
72 | |||
73 | private $_database; |
||
74 | |||
75 | private $_server_id; |
||
76 | |||
77 | private $_server_info; |
||
78 | |||
79 | private $_error_msg = ''; |
||
80 | |||
81 | private static $instance = null; |
||
82 | |||
83 | /** |
||
84 | * @param \Slim\Container $container The container |
||
85 | * @param mixed $path |
||
86 | * @param mixed $settings |
||
87 | * @param \Slim\Container $c |
||
88 | */ |
||
89 | public function __construct($path, $settings, \Slim\Container $c) |
||
121 | } |
||
122 | } |
||
123 | |||
124 | public function maybeRenderIframes($response, $subject, $query_string) |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Gets the theme from |
||
147 | * 1. The $_REQUEST global (when it's chosen from start screen) |
||
148 | * 2. Server specific config theme 3.- $_SESSION global (subsequent requests after 1.) 4.- $_COOKIE global (mostly |
||
149 | * fallback for $_SESSION after 1.- and 3.-) 5.- theme as set in config 6.- 'default' theme. |
||
150 | * |
||
151 | * @param array $conf The conf |
||
152 | * @param null|mixed $_server_info |
||
153 | * |
||
154 | * @return string the theme |
||
155 | */ |
||
156 | public function getTheme(array $conf, $_server_info = null) |
||
157 | { |
||
158 | $_theme = null; |
||
159 | // List of themes |
||
160 | $themefolders = $this->getThemeFolders(); |
||
161 | // Check if theme is in $_REQUEST, $_SESSION or $_COOKIE |
||
162 | // 1.- First priority: $_REQUEST, this happens when you use the selector |
||
163 | if (\array_key_exists('theme', $_REQUEST) && |
||
164 | \array_key_exists($_REQUEST['theme'], $themefolders)) { |
||
165 | $_theme = $_REQUEST['theme']; |
||
166 | } elseif ( // otherwise, see if there's a theme associated with this particular server |
||
167 | null !== $_server_info && |
||
168 | \array_key_exists('theme', $_server_info) && |
||
169 | \is_string($_server_info['theme']) && |
||
170 | \array_key_exists($_COOKIE['ppaTheme'], $themefolders)) { |
||
171 | $_theme = $_server_info['theme']; |
||
172 | } elseif (isset($_SESSION) && \array_key_exists('ppaTheme', $_SESSION) && |
||
173 | \array_key_exists($_SESSION['ppaTheme'], $themefolders)) { |
||
174 | // otherwise check $_SESSION |
||
175 | $_theme = $_SESSION['ppaTheme']; |
||
176 | } elseif (\array_key_exists('ppaTheme', $_COOKIE) && |
||
177 | \array_key_exists($_COOKIE['ppaTheme'], $themefolders)) { |
||
178 | // oterwise check $_COOKIE |
||
179 | $_theme = $_COOKIE['ppaTheme']; |
||
180 | } elseif ( // see if there's a valid theme set in config file |
||
181 | \array_key_exists('theme', $conf) && |
||
182 | \is_string($conf['theme']) && |
||
183 | \array_key_exists($conf['theme'], $themefolders)) { |
||
184 | $_theme = $conf['theme']; |
||
185 | } else { |
||
186 | // okay then, use default theme |
||
187 | $_theme = 'default'; |
||
188 | } |
||
189 | |||
190 | return $_theme; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Sets the form tracking variable. |
||
195 | */ |
||
196 | public function setForm() |
||
197 | { |
||
198 | $form = []; |
||
199 | |||
200 | if ($this->container->server) { |
||
201 | $form[] = \sprintf( |
||
202 | '<input type="hidden" name="%s" value="%s" />', |
||
203 | 'server', |
||
204 | \htmlspecialchars($this->container->server) |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | if ($this->container->database) { |
||
209 | $form[] = \sprintf( |
||
210 | '<input type="hidden" name="%s" value="%s" />', |
||
211 | 'database', |
||
212 | \htmlspecialchars($this->container->database) |
||
213 | ); |
||
214 | } |
||
215 | |||
216 | if ($this->container->schema) { |
||
217 | $form[] = \sprintf( |
||
218 | '<input type="hidden" name="%s" value="%s" />', |
||
219 | 'schema', |
||
220 | \htmlspecialchars($this->container->schema) |
||
221 | ); |
||
222 | } |
||
223 | $this->form = \implode("\n", $form); |
||
224 | |||
225 | return $this->form; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Displays link to the context help. |
||
230 | * |
||
231 | * @param string $str the string that the context help is related to (already escaped) |
||
232 | * @param string $help help section identifier |
||
233 | * @param bool $do_print true to echo, false to return |
||
234 | */ |
||
235 | public function printHelp($str, $help = null, $do_print = true) |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Gets the help link. |
||
252 | * |
||
253 | * @param string $help The help subject |
||
254 | * |
||
255 | * @return string the help link |
||
256 | */ |
||
257 | public function getHelpLink($help) |
||
258 | { |
||
259 | return \htmlspecialchars( |
||
260 | $this->getSubfolder('help?help=') . |
||
261 | \urlencode($help) . |
||
262 | '&server=' . |
||
263 | \urlencode($this->misc->getServerId()) |
||
264 | ); |
||
265 | } |
||
266 | |||
267 | public function icon($icon) |
||
268 | { |
||
269 | if (!\is_string($icon)) { |
||
270 | return ''; |
||
271 | } |
||
272 | |||
273 | $theme = $this->conf['theme']; |
||
274 | $path = 'assets/images/themes'; |
||
275 | $default_icon = \sprintf('%s/%s/default/DisconnectedServer.png', self::SUBFOLDER, $path); |
||
276 | |||
277 | if (\is_readable(\sprintf('%s/%s/%s/%s.png', self::BASE_PATH, $path, $theme, $icon))) { |
||
278 | return \sprintf('%s/%s/%s/%s.png', self::SUBFOLDER, $path, $theme, $icon); |
||
279 | } |
||
280 | |||
281 | if (\is_readable(\sprintf('%s/%s/%s/%s.gif', self::BASE_PATH, $path, $theme, $icon))) { |
||
282 | return \sprintf('%s/%s/%s/%s.gif', self::SUBFOLDER, $path, $theme, $icon); |
||
283 | } |
||
284 | |||
285 | if (\is_readable(\sprintf('%s/%s/%s/%s.ico', self::BASE_PATH, $path, $theme, $icon))) { |
||
286 | return \sprintf('%s/%s/%s/%s.ico', self::SUBFOLDER, $path, $theme, $icon); |
||
287 | } |
||
288 | |||
289 | if (\is_readable(\sprintf('%s/%s/default/%s.png', self::BASE_PATH, $path, $icon))) { |
||
290 | return \sprintf('%s/%s/default/%s.png', self::SUBFOLDER, $path, $icon); |
||
291 | } |
||
292 | |||
293 | if (\is_readable(\sprintf('%s/%s/default/%s.gif', self::BASE_PATH, $path, $icon))) { |
||
294 | return \sprintf('%s/%s/default/%s.gif', self::SUBFOLDER, $path, $icon); |
||
295 | } |
||
296 | |||
297 | if (\is_readable(\sprintf('%s/%s/default/%s.ico', self::BASE_PATH, $path, $icon))) { |
||
298 | return \sprintf('%s/%s/default/%s.ico', self::SUBFOLDER, $path, $icon); |
||
299 | } |
||
300 | |||
301 | return $default_icon; |
||
302 | } |
||
303 | |||
304 | private function getContainer() |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Traverse THEME_PATH, consider as theme folders those which |
||
311 | * contain a `global.css` stylesheet. |
||
312 | * |
||
313 | * @return array the theme folders |
||
314 | */ |
||
315 | private function getThemeFolders() |
||
345 |