Total Complexity | 53 |
Total Lines | 351 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like TinyMCE 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 TinyMCE, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class TinyMCE |
||
27 | { |
||
28 | public $rootpath; |
||
29 | public $config = []; |
||
30 | public $setting = []; |
||
31 | |||
32 | /** |
||
33 | * PHP 5 Constructor |
||
34 | * |
||
35 | * @param string $config The configuration |
||
36 | **/ |
||
37 | |||
38 | public function __construct($config) |
||
39 | { |
||
40 | $this->setConfig($config); |
||
41 | $this->rootpath = $this->config['rootpath'] . '/tinymce/js/tinymce'; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Creates one instance of the tinyMCE object |
||
46 | * |
||
47 | * @param array $config The configuration |
||
48 | * @return object $instance The instance of tinyMCE object |
||
49 | **/ |
||
50 | |||
51 | public function &instance($config) |
||
52 | { |
||
53 | static $instance; |
||
54 | if (!isset($instance)) { |
||
55 | $instance = new TinyMCE($config); |
||
56 | } else { |
||
57 | $instance->setConfig($config); |
||
58 | } |
||
59 | |||
60 | return $instance; |
||
61 | } |
||
62 | |||
63 | public function setConfig($config) |
||
64 | { |
||
65 | foreach ($config as $key => $val) { |
||
66 | $this->config[$key] = $val; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Initializes the tinyMCE |
||
72 | * @return true |
||
73 | **/ |
||
74 | |||
75 | public function init() |
||
76 | { |
||
77 | // list of configured options |
||
78 | $configured = []; |
||
79 | $this->setting['selector'] = 'textarea'; |
||
80 | $this->setting['theme'] = 'modern'; |
||
81 | |||
82 | // Load default settings |
||
83 | if (!($this->setting = @include($GLOBALS['xoops']->path('var/configs/tinymce.php')))) { |
||
84 | $this->setting = include __DIR__ . '/settings.php'; |
||
85 | } |
||
86 | |||
87 | // get editor language (from ...) |
||
88 | if (is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config['language'] . '.js')) { |
||
89 | $this->setting['language'] = $this->config['language']; |
||
90 | $configured[] = 'language'; |
||
91 | } |
||
92 | |||
93 | $this->setting['content_css'] = implode(',', $this->loadCss()); |
||
94 | $configured[] = 'content_css'; |
||
95 | |||
96 | if (!empty($this->config['theme']) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->config['theme'])) { |
||
97 | $this->setting['theme'] = $this->config['theme']; |
||
98 | $configured[] = 'theme'; |
||
99 | } |
||
100 | |||
101 | if (!empty($this->config['mode'])) { |
||
102 | $this->setting['mode'] = $this->config['mode']; |
||
103 | $configured[] = 'mode'; |
||
104 | } |
||
105 | |||
106 | // load all plugins except the plugins in setting["exclude_plugins"] |
||
107 | $this->setting['plugins'] = implode(',', $this->loadPlugins()); |
||
108 | $configured[] = 'plugins'; |
||
109 | |||
110 | if ('simple' !== $this->setting['theme']) { |
||
111 | if (empty($this->config['buttons'])) { |
||
112 | $this->config['buttons'][] = [ |
||
113 | 'before' => '', |
||
114 | 'add' => '', |
||
115 | ]; |
||
116 | $this->config['buttons'][] = [ |
||
117 | 'before' => '', |
||
118 | 'add' => '', |
||
119 | ]; |
||
120 | $this->config['buttons'][] = [ |
||
121 | 'before' => '', |
||
122 | 'add' => '', |
||
123 | ]; |
||
124 | } |
||
125 | $i = 0; |
||
126 | foreach ($this->config['buttons'] as $button) { |
||
127 | ++$i; |
||
128 | if (isset($button['before'])) { |
||
129 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add_before"] = $button['before']; |
||
130 | } |
||
131 | if (isset($button['add'])) { |
||
132 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add"] = $button['add']; |
||
133 | } |
||
134 | if (isset($button[''])) { |
||
135 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = $button['']; |
||
136 | } |
||
137 | } |
||
138 | $configured[] = 'buttons'; |
||
139 | |||
140 | if (isset($this->config['toolbar_location'])) { |
||
141 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = $this->config['toolbar_location']; |
||
142 | $configured[] = 'toolbar_location'; |
||
143 | } else { |
||
144 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = 'top'; |
||
145 | } |
||
146 | |||
147 | if (isset($this->config['toolbar_align'])) { |
||
148 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = $this->config['toolbar_align']; |
||
149 | $configured[] = 'toolbar_align'; |
||
150 | } else { |
||
151 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = 'left'; |
||
152 | } |
||
153 | |||
154 | if (isset($this->config['statusbar_location'])) { |
||
155 | $this->setting['theme_' . $this->setting['theme'] . '_statusbar_location'] = $this->config['statusbar_location']; |
||
156 | $configured[] = 'statusbar_location'; |
||
157 | } |
||
158 | |||
159 | if (isset($this->config['path_location'])) { |
||
160 | $this->setting['theme_' . $this->setting['theme'] . '_path_location'] = $this->config['path_location']; |
||
161 | $configured[] = 'path_location'; |
||
162 | } |
||
163 | |||
164 | if (isset($this->config['resize_horizontal'])) { |
||
165 | $this->setting['theme_' . $this->setting['theme'] . '_resize_horizontal'] = $this->config['resize_horizontal']; |
||
166 | $configured[] = 'resize_horizontal'; |
||
167 | } |
||
168 | |||
169 | if (isset($this->config['resizing'])) { |
||
170 | $this->setting['theme_' . $this->setting['theme'] . '_resizing'] = $this->config['resizing']; |
||
171 | $configured[] = 'resizing'; |
||
172 | } |
||
173 | |||
174 | if (!empty($this->config['fonts'])) { |
||
175 | $this->setting['theme_' . $this->setting['theme'] . '_fonts'] = $this->config['fonts']; |
||
176 | $configured[] = 'fonts'; |
||
177 | } |
||
178 | |||
179 | for ($i = 1; $i <= 4; ++$i) { |
||
180 | $buttons = []; |
||
181 | if (isset($this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"])) { |
||
182 | $checklist = explode(',', $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"]); |
||
183 | foreach ($checklist as $plugin) { |
||
184 | if (false != strpos(strtolower($plugin), 'xoops')) { |
||
|
|||
185 | if (in_array($plugin, $this->xoopsPlugins)) { |
||
186 | $buttons[] = $plugin; |
||
187 | } |
||
188 | } else { |
||
189 | $buttons[] = $plugin; |
||
190 | } |
||
191 | } |
||
192 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = implode(',', $buttons); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | $configured = array_unique($configured); |
||
198 | foreach ($this->config as $key => $val) { |
||
199 | if (isset($this->setting[$key]) || in_array($key, $configured)) { |
||
200 | continue; |
||
201 | } |
||
202 | $this->setting[$key] = $val; |
||
203 | } |
||
204 | |||
205 | if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->setting['theme'] . '/docs/' . $this->setting['language'] . '/')) { |
||
206 | $this->setting['docs_language'] = 'en'; |
||
207 | } |
||
208 | |||
209 | unset($this->config, $configured); |
||
210 | |||
211 | return true; |
||
212 | } |
||
213 | |||
214 | // load all plugins execpt the plugins in setting["exclude_plugins"] |
||
215 | public function loadPlugins() |
||
232 | } |
||
233 | |||
234 | // return all xoops plugins |
||
235 | public function get_xoopsPlugins() |
||
236 | { |
||
237 | $xoopsPlugins = []; |
||
238 | $allplugins = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins'); |
||
239 | foreach ($allplugins as $plugin) { |
||
240 | if (false != strpos(strtolower($plugin), 'xoops') && file_exists(XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php")) { |
||
241 | if ($right = @include XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php") { |
||
242 | $xoopsPlugins[$plugin] = $plugin; |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | |||
247 | return $xoopsPlugins; |
||
248 | } |
||
249 | |||
250 | public function loadCss($css_file = 'style.css') |
||
251 | { |
||
252 | static $css_url, $css_path; |
||
253 | |||
254 | if (!isset($css_url)) { |
||
255 | $css_url = dirname(xoops_getcss($GLOBALS['xoopsConfig']['theme_set'])); |
||
256 | $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url); |
||
257 | } |
||
258 | |||
259 | $css = []; |
||
260 | $css[] = $css_url . '/' . $css_file; |
||
261 | $css_content = file_get_contents($css_path . '/' . $css_file); |
||
262 | |||
263 | // get all import css files |
||
264 | if (preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER)) { |
||
265 | foreach ($matches[1] as $key => $css_import) { |
||
266 | $css = array_merge($css, $this->loadCss($css_import)); |
||
267 | } |
||
268 | } |
||
269 | |||
270 | return $css; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Renders the tinyMCE |
||
275 | * @return string $ret The rendered HTML string |
||
276 | **/ |
||
277 | public function render() |
||
377 | } |
||
378 | } |
||
379 |