Complex classes like PluginBase 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 PluginBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | abstract class PluginBase extends AbstractPlugin |
||
53 | { |
||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | public $prefixId = 'tx_solr'; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | public $extKey = 'solr'; |
||
63 | |||
64 | /** |
||
65 | * An instance of ApacheSolrForTypo3\Solr\Template |
||
66 | * |
||
67 | * @var Template |
||
68 | */ |
||
69 | protected $template; |
||
70 | |||
71 | /** |
||
72 | * An instance of ApacheSolrForTypo3\Solr\JavascriptManager |
||
73 | * |
||
74 | * @var JavascriptManager |
||
75 | */ |
||
76 | protected $javascriptManager; |
||
77 | |||
78 | /** |
||
79 | * An instance of the localization factory |
||
80 | * |
||
81 | * @var \TYPO3\CMS\Core\Localization\LocalizationFactory |
||
82 | */ |
||
83 | protected $languageFactory; |
||
84 | |||
85 | /** |
||
86 | * The user's raw query. |
||
87 | * |
||
88 | * Private to enforce API usage. |
||
89 | * |
||
90 | * @var string |
||
91 | */ |
||
92 | private $rawUserQuery; |
||
93 | |||
94 | // Main |
||
95 | |||
96 | /** |
||
97 | * @var TypoScriptConfiguration |
||
98 | */ |
||
99 | public $typoScriptConfiguration; |
||
100 | |||
101 | /** |
||
102 | * @var SearchResultSetService |
||
103 | */ |
||
104 | private $searchResultsSetService; |
||
105 | |||
106 | /** |
||
107 | * The main method of the plugin |
||
108 | * |
||
109 | * @param string $content The plugin content |
||
110 | * @param array $configuration The plugin configuration |
||
111 | * @return string The content that is displayed on the website |
||
112 | */ |
||
113 | 25 | public function main($content, $configuration) |
|
|
|||
114 | { |
||
115 | /** @noinspection PhpUnusedLocalVariableInspection */ |
||
116 | 25 | $content = ''; |
|
117 | |||
118 | try { |
||
119 | 25 | $this->initialize($configuration); |
|
120 | 25 | $this->preRender(); |
|
121 | |||
122 | 25 | $actionResult = $this->performAction(); |
|
123 | |||
124 | 25 | if ($this->getSearchResultSetService()->getIsSolrAvailable()) { |
|
125 | 25 | $content = $this->render($actionResult); |
|
126 | 25 | } else { |
|
127 | $content = $this->renderError(); |
||
128 | } |
||
129 | |||
130 | 25 | $content = $this->postRender($content); |
|
131 | 25 | } catch (\Exception $e) { |
|
132 | if ($this->typoScriptConfiguration->getLoggingExceptions()) { |
||
133 | GeneralUtility::devLog( |
||
134 | $e->getCode() . ': ' . $e->__toString(), |
||
135 | 'solr', |
||
136 | 3, |
||
137 | (array)$e |
||
138 | ); |
||
139 | } |
||
140 | |||
141 | $this->initializeTemplateEngine(); |
||
142 | $content = $this->renderException(); |
||
143 | } |
||
144 | |||
145 | 25 | return $this->baseWrap($content); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Adds the possibility to use stdWrap on the plugins content instead of wrapInBaseClass. |
||
150 | * Defaults to wrapInBaseClass to ensure downward compatibility. |
||
151 | * |
||
152 | * @param string $content The plugin content |
||
153 | * @return string |
||
154 | */ |
||
155 | 25 | protected function baseWrap($content) |
|
156 | { |
||
157 | 25 | $baseWrap = $this->typoScriptConfiguration->getObjectByPath('plugin.tx_solr.general.baseWrap.'); |
|
158 | 25 | if (isset($baseWrap)) { |
|
159 | 25 | return $this->cObj->stdWrap($content, |
|
160 | 25 | $baseWrap); |
|
161 | } else { |
||
162 | return $this->pi_wrapInBaseClass($content); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Implements the action logic. The result of this method is passed to the |
||
168 | * render method. |
||
169 | * |
||
170 | * @return string Action result |
||
171 | */ |
||
172 | abstract protected function performAction(); |
||
173 | |||
174 | // Initialization |
||
175 | |||
176 | /** |
||
177 | * Initializes the plugin - configuration, language, caching, search... |
||
178 | * |
||
179 | * @param array $configuration configuration array as provided by the TYPO3 core |
||
180 | */ |
||
181 | 25 | protected function initialize($configuration) |
|
182 | { |
||
183 | /** @var $configurationManager \ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager */ |
||
184 | 25 | $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class); |
|
185 | 25 | $typoScriptConfiguration = $configurationManager->getTypoScriptConfiguration()->mergeSolrConfiguration($configuration); |
|
186 | 25 | $this->typoScriptConfiguration = $typoScriptConfiguration; |
|
187 | |||
188 | 25 | $this->initializeLanguageFactory(); |
|
189 | 25 | $this->pi_setPiVarDefaults(); |
|
190 | 25 | $this->pi_loadLL(); |
|
191 | 25 | $this->pi_initPIflexForm(); |
|
192 | |||
193 | 25 | $this->overrideTyposcriptWithFlexformSettings(); |
|
194 | |||
195 | 25 | $this->initializeQuery(); |
|
196 | 25 | $this->initializeSearch(); |
|
197 | 25 | $this->initializeTemplateEngine(); |
|
198 | 25 | $this->initializeJavascriptManager(); |
|
199 | |||
200 | 25 | $this->postInitialize(); |
|
201 | 25 | } |
|
202 | |||
203 | /** |
||
204 | * Overwrites pi_setPiVarDefaults to add stdWrap-functionality to _DEFAULT_PI_VARS |
||
205 | * |
||
206 | * @author Grigori Prokhorov <[email protected]> |
||
207 | * @author Ivan Kartolo <[email protected]> |
||
208 | * @return void |
||
209 | */ |
||
210 | 25 | public function pi_setPiVarDefaults() |
|
211 | { |
||
212 | 25 | if (is_array($this->conf['_DEFAULT_PI_VARS.'])) { |
|
213 | foreach ($this->conf['_DEFAULT_PI_VARS.'] as $key => $defaultValue) { |
||
214 | $this->conf['_DEFAULT_PI_VARS.'][$key] = $this->cObj->cObjGetSingle($this->conf['_DEFAULT_PI_VARS.'][$key], |
||
215 | $this->conf['_DEFAULT_PI_VARS.'][$key . '.']); |
||
216 | } |
||
217 | |||
218 | $piVars = is_array($this->piVars) ? $this->piVars : []; |
||
219 | $this->piVars = $this->conf['_DEFAULT_PI_VARS.']; |
||
220 | ArrayUtility::mergeRecursiveWithOverrule( |
||
221 | $this->piVars, |
||
222 | $piVars |
||
223 | ); |
||
224 | } |
||
225 | 25 | } |
|
226 | |||
227 | /** |
||
228 | * Overwrites pi_loadLL() to handle custom location of language files. |
||
229 | * |
||
230 | * Loads local-language values by looking for a "locallang" file in the |
||
231 | * plugin class directory ($this->scriptRelPath) and if found includes it. |
||
232 | * Also locallang values set in the TypoScript property "_LOCAL_LANG" are |
||
233 | * merged onto the values found in the "locallang" file. |
||
234 | * Supported file extensions xlf, xml, php |
||
235 | * |
||
236 | * @param string $languageFilePath path to the plugin language file in format EXT:.... |
||
237 | * @return void |
||
238 | */ |
||
239 | 25 | public function pi_loadLL($languageFilePath = '') |
|
240 | { |
||
241 | 25 | if (!$this->LOCAL_LANG_loaded && $this->scriptRelPath) { |
|
242 | 25 | $basePath = 'EXT:' . $this->extKey . '/Resources/Private/Language/locallang.xlf'; |
|
243 | // Read the strings in the required charset (since TYPO3 4.2) |
||
244 | 25 | $this->LOCAL_LANG = $this->languageFactory->getParsedData($basePath, |
|
245 | 25 | $this->LLkey, $GLOBALS['TSFE']->renderCharset, 3); |
|
246 | |||
247 | 25 | $alternativeLanguageKeys = GeneralUtility::trimExplode(',', |
|
248 | 25 | $this->altLLkey, true); |
|
249 | 25 | foreach ($alternativeLanguageKeys as $languageKey) { |
|
250 | $tempLL = $this->languageFactory->getParsedData($basePath, $languageKey, $GLOBALS['TSFE']->renderCharset, 3); |
||
251 | if ($this->LLkey !== 'default' && isset($tempLL[$languageKey])) { |
||
252 | $this->LOCAL_LANG[$languageKey] = $tempLL[$languageKey]; |
||
253 | } |
||
254 | 25 | } |
|
255 | // Overlaying labels from TypoScript (including fictitious language keys for non-system languages!): |
||
256 | 25 | $translationInTypoScript = $this->typoScriptConfiguration->getLocalLangConfiguration(); |
|
257 | |||
258 | 25 | if (count($translationInTypoScript) == 0) { |
|
259 | 24 | return; |
|
260 | } |
||
261 | |||
262 | // Clear the "unset memory" |
||
263 | 1 | $this->LOCAL_LANG_UNSET = []; |
|
264 | 1 | foreach ($translationInTypoScript as $languageKey => $languageArray) { |
|
265 | // Remove the dot after the language key |
||
266 | 1 | $languageKey = substr($languageKey, 0, -1); |
|
267 | // Don't process label if the language is not loaded |
||
268 | 1 | if (is_array($languageArray) && isset($this->LOCAL_LANG[$languageKey])) { |
|
269 | 1 | foreach ($languageArray as $labelKey => $labelValue) { |
|
270 | 1 | if (!is_array($labelValue)) { |
|
271 | 1 | $this->LOCAL_LANG[$languageKey][$labelKey][0]['target'] = $labelValue; |
|
272 | 1 | $this->LOCAL_LANG_charset[$languageKey][$labelKey] = 'utf-8'; |
|
273 | |||
274 | 1 | if ($labelValue === '') { |
|
275 | $this->LOCAL_LANG_UNSET[$languageKey][$labelKey] = ''; |
||
276 | } |
||
277 | 1 | } |
|
278 | 1 | } |
|
279 | 1 | } |
|
280 | 1 | } |
|
281 | 1 | } |
|
282 | 1 | $this->LOCAL_LANG_loaded = 1; |
|
283 | 1 | } |
|
284 | |||
285 | /** |
||
286 | * Allows to override TypoScript settings with Flexform values. |
||
287 | * |
||
288 | */ |
||
289 | 3 | protected function overrideTyposcriptWithFlexformSettings() |
|
290 | { |
||
291 | 3 | } |
|
292 | |||
293 | /** |
||
294 | * Initializes the query from the GET query parameter. |
||
295 | * |
||
296 | */ |
||
297 | 25 | protected function initializeQuery() |
|
298 | { |
||
299 | 25 | $this->rawUserQuery = GeneralUtility::_GET('q'); |
|
300 | 25 | } |
|
301 | |||
302 | /** |
||
303 | * Initializes the Solr connection and tests the connection through a ping. |
||
304 | * |
||
305 | */ |
||
306 | 25 | protected function initializeSearch() |
|
307 | { |
||
308 | 25 | $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId( |
|
309 | 25 | $GLOBALS['TSFE']->id, |
|
310 | 25 | $GLOBALS['TSFE']->sys_language_uid, |
|
311 | 25 | $GLOBALS['TSFE']->MP |
|
312 | 25 | ); |
|
313 | |||
314 | 25 | $search = GeneralUtility::makeInstance(Search::class, $solrConnection); |
|
315 | /** @var $this->searchService ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService */ |
||
316 | 25 | $this->searchResultsSetService = GeneralUtility::makeInstance(SearchResultSetService::class, $this->typoScriptConfiguration, $search, $this); |
|
317 | 25 | $this->solrAvailable = $this->searchResultsSetService->getIsSolrAvailable(); |
|
318 | 25 | $this->search = $this->searchResultsSetService->getSearch(); |
|
319 | 25 | } |
|
320 | |||
321 | /** |
||
322 | * @return SearchResultSetService |
||
323 | */ |
||
324 | 25 | public function getSearchResultSetService() |
|
325 | { |
||
326 | 25 | return $this->searchResultsSetService; |
|
327 | } |
||
328 | |||
329 | /** |
||
330 | * Initializes the template engine and returns the initialized instance. |
||
331 | * |
||
332 | * @throws \UnexpectedValueException if a view helper provider fails to implement interface ApacheSolrForTypo3\Solr\ViewHelper\ViewHelperProvider |
||
333 | */ |
||
334 | 25 | protected function initializeTemplateEngine() |
|
387 | |||
388 | /** |
||
389 | * Initializes the javascript manager. |
||
390 | * |
||
391 | */ |
||
392 | 25 | protected function initializeJavascriptManager() |
|
396 | |||
397 | /** |
||
398 | * Initializes the language factory; |
||
399 | */ |
||
400 | 25 | protected function initializeLanguageFactory() |
|
404 | |||
405 | /** |
||
406 | * This method is called after initializing in the initialize method. |
||
407 | * Overwrite this method to do your own initialization. |
||
408 | * |
||
409 | * @return void |
||
410 | */ |
||
411 | 3 | protected function postInitialize() |
|
414 | |||
415 | /** |
||
416 | * Overwrite this method to do own initialisations of the template. |
||
417 | * |
||
418 | * @param Template $template Template |
||
419 | * @return Template |
||
420 | */ |
||
421 | protected function postInitializeTemplateEngine(Template $template) |
||
425 | |||
426 | // Rendering |
||
427 | |||
428 | /** |
||
429 | * This method executes the requested commands and applies the changes to |
||
430 | * the template. |
||
431 | * |
||
432 | * @param $actionResult |
||
433 | * @return string Rendered plugin content |
||
434 | */ |
||
435 | abstract protected function render($actionResult); |
||
436 | |||
437 | /** |
||
438 | * Renders a solr error. |
||
439 | * |
||
440 | * @return string A representation of the error that should be understandable for the user. |
||
441 | */ |
||
442 | protected function renderError() |
||
448 | |||
449 | /** |
||
450 | * Renders a solr exception. |
||
451 | * |
||
452 | * @return string A representation of the exception that should be understandable for the user. |
||
453 | */ |
||
454 | protected function renderException() |
||
460 | |||
461 | /** |
||
462 | * Should be overwritten to do things before rendering. |
||
463 | * |
||
464 | */ |
||
465 | 2 | protected function preRender() |
|
468 | |||
469 | /** |
||
470 | * Overwrite this method to perform changes to the content after rendering. |
||
471 | * |
||
472 | * @param string $content The content rendered by the plugin so far |
||
473 | * @return string The content that should be presented on the website, might be different from the output rendered before |
||
474 | */ |
||
475 | 25 | protected function postRender($content) |
|
483 | |||
484 | // Helper methods |
||
485 | |||
486 | /** |
||
487 | * Determines the template file from the configuration. |
||
488 | * |
||
489 | * Overwrite this method to use a different template. |
||
490 | * |
||
491 | * @return string The template file name to be used for the plugin |
||
492 | */ |
||
493 | 25 | protected function getTemplateFile() |
|
497 | |||
498 | /** |
||
499 | * This method should be implemented to return the TSconfig key which |
||
500 | * contains the template name for this template. |
||
501 | * |
||
502 | * @see initializeTemplateEngine() |
||
503 | * @return string The TSconfig key containing the template name |
||
504 | */ |
||
505 | abstract protected function getTemplateFileKey(); |
||
506 | |||
507 | /** |
||
508 | * Gets the plugin's template instance. |
||
509 | * |
||
510 | * @return Template The plugin's template. |
||
511 | */ |
||
512 | 25 | public function getTemplate() |
|
516 | |||
517 | /** |
||
518 | * Gets the plugin's javascript manager. |
||
519 | * |
||
520 | * @return JavascriptManager The plugin's javascript manager. |
||
521 | */ |
||
522 | 25 | public function getJavascriptManager() |
|
526 | |||
527 | /** |
||
528 | * Should return the relevant subpart of the template. |
||
529 | * |
||
530 | * @see initializeTemplateEngine() |
||
531 | * @return string The subpart of the template to be used |
||
532 | */ |
||
533 | abstract protected function getSubpart(); |
||
534 | |||
535 | /** |
||
536 | * This method should return the plugin key. Reads some configuration |
||
537 | * options in initializeTemplateEngine() |
||
538 | * |
||
539 | * @see initializeTemplateEngine() |
||
540 | * @return string The plugin key |
||
541 | */ |
||
542 | abstract protected function getPluginKey(); |
||
543 | |||
544 | /** |
||
545 | * Gets the target page Id for links. Might have been set through either |
||
546 | * flexform or TypoScript. If none is set, TSFE->id is used. |
||
547 | * |
||
548 | * @return int The page Id to be used for links |
||
549 | */ |
||
550 | 18 | public function getLinkTargetPageId() |
|
554 | |||
555 | /** |
||
556 | * Gets the user's query term and cleans it so that it can be used in |
||
557 | * templates f.e. |
||
558 | * |
||
559 | * @return string The cleaned user query. |
||
560 | */ |
||
561 | 25 | public function getCleanUserQuery() |
|
575 | |||
576 | /** |
||
577 | * Gets the raw user query |
||
578 | * |
||
579 | * @return string Raw user query. |
||
580 | */ |
||
581 | 25 | public function getRawUserQuery() |
|
585 | |||
586 | /** |
||
587 | * @return string |
||
588 | */ |
||
589 | 25 | protected function getCurrentUrlWithQueryLinkBuilder() |
|
605 | } |
||
606 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.