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