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 |
||
| 48 | abstract class PluginBase extends AbstractPlugin |
||
| 49 | { |
||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | public $prefixId = 'tx_solr'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $extKey = 'solr'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The plugin's query |
||
| 62 | * |
||
| 63 | * @deprecated use $this->searchResultSet->getUsedQuery() instead, will be removed in version 5.0 |
||
| 64 | * @var Query |
||
| 65 | */ |
||
| 66 | protected $query = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * An instance of ApacheSolrForTypo3\Solr\Template |
||
| 70 | * |
||
| 71 | * @var Template |
||
| 72 | */ |
||
| 73 | protected $template; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * An instance of ApacheSolrForTypo3\Solr\JavascriptManager |
||
| 77 | * |
||
| 78 | * @var JavascriptManager |
||
| 79 | */ |
||
| 80 | protected $javascriptManager; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * An instance of the localization factory |
||
| 84 | * |
||
| 85 | * @var \TYPO3\CMS\Core\Localization\LocalizationFactory |
||
| 86 | */ |
||
| 87 | protected $languageFactory; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The user's raw query. |
||
| 91 | * |
||
| 92 | * Private to enforce API usage. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | private $rawUserQuery; |
||
| 97 | |||
| 98 | // Main |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var TypoScriptConfiguration |
||
| 102 | */ |
||
| 103 | public $typoScriptConfiguration; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var SearchResultSetService |
||
| 107 | */ |
||
| 108 | private $searchResultsSetService; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The main method of the plugin |
||
| 112 | * |
||
| 113 | * @param string $content The plugin content |
||
| 114 | * @param array $configuration The plugin configuration |
||
| 115 | * @return string The content that is displayed on the website |
||
| 116 | */ |
||
| 117 | 25 | public function main($content, $configuration) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Adds the possibility to use stdWrap on the plugins content instead of wrapInBaseClass. |
||
| 154 | * Defaults to wrapInBaseClass to ensure downward compatibility. |
||
| 155 | * |
||
| 156 | * @param string $content The plugin content |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | 25 | protected function baseWrap($content) |
|
| 160 | { |
||
| 161 | 25 | $baseWrap = $this->typoScriptConfiguration->getObjectByPath('plugin.tx_solr.general.baseWrap.'); |
|
| 162 | 25 | if (isset($baseWrap)) { |
|
| 163 | 25 | return $this->cObj->stdWrap($content, |
|
| 164 | 25 | $baseWrap); |
|
| 165 | } else { |
||
| 166 | return $this->pi_wrapInBaseClass($content); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Implements the action logic. The result of this method is passed to the |
||
| 172 | * render method. |
||
| 173 | * |
||
| 174 | * @return string Action result |
||
| 175 | */ |
||
| 176 | abstract protected function performAction(); |
||
| 177 | |||
| 178 | // Initialization |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Initializes the plugin - configuration, language, caching, search... |
||
| 182 | * |
||
| 183 | * @param array $configuration configuration array as provided by the TYPO3 core |
||
| 184 | */ |
||
| 185 | 25 | protected function initialize($configuration) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Overwrites pi_setPiVarDefaults to add stdWrap-functionality to _DEFAULT_PI_VARS |
||
| 209 | * |
||
| 210 | * @author Grigori Prokhorov <[email protected]> |
||
| 211 | * @author Ivan Kartolo <[email protected]> |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | 25 | public function pi_setPiVarDefaults() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Overwrites pi_loadLL() to handle custom location of language files. |
||
| 233 | * |
||
| 234 | * Loads local-language values by looking for a "locallang" file in the |
||
| 235 | * plugin class directory ($this->scriptRelPath) and if found includes it. |
||
| 236 | * Also locallang values set in the TypoScript property "_LOCAL_LANG" are |
||
| 237 | * merged onto the values found in the "locallang" file. |
||
| 238 | * Supported file extensions xlf, xml, php |
||
| 239 | * |
||
| 240 | * @param string $languageFilePath path to the plugin language file in format EXT:.... |
||
| 241 | * @return void |
||
| 242 | */ |
||
| 243 | 25 | public function pi_loadLL($languageFilePath = '') |
|
| 244 | { |
||
| 245 | 25 | if (!$this->LOCAL_LANG_loaded && $this->scriptRelPath) { |
|
| 246 | 25 | $pathElements = pathinfo($this->scriptRelPath); |
|
| 247 | 25 | $languageFileName = $pathElements['filename']; |
|
| 248 | |||
| 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 = array(); |
|
| 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 = 1; |
|
| 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('ApacheSolrForTypo3\\Solr\\ConnectionManager')->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('ApacheSolrForTypo3\\Solr\\Search', $solrConnection); |
|
| 322 | /** @var $this->searchService ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService */ |
||
| 323 | 25 | $this->searchResultsSetService = GeneralUtility::makeInstance('ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService', $this->typoScriptConfiguration, $search, $this); |
|
| 324 | 25 | $this->solrAvailable = $this->searchResultsSetService->getIsSolrAvailable(); |
|
| 325 | 25 | $this->search = $this->searchResultsSetService->getSearch(); |
|
| 326 | 25 | } |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @return SearchResultSetService |
||
| 330 | */ |
||
| 331 | 25 | public function getSearchResultSetService() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Initializes the template engine and returns the initialized instance. |
||
| 338 | * |
||
| 339 | * @return Template |
||
| 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 | 'ApacheSolrForTypo3\\Solr\\Template', |
|
| 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', array( |
|
| 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 | $helperAdded = $template->addViewHelperObject($helperName, |
||
| 380 | $helperObject); |
||
| 381 | } |
||
| 382 | } else { |
||
| 383 | throw new \UnexpectedValueException( |
||
| 384 | get_class($viewHelperProvider) . ' must implement interface ApacheSolrForTypo3\Solr\ViewHelper\ViewHelperProvider', |
||
| 385 | 1310387296 |
||
| 386 | ); |
||
| 387 | } |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | 25 | $template = $this->postInitializeTemplateEngine($template); |
|
| 392 | |||
| 393 | 25 | $this->template = $template; |
|
| 394 | 25 | } |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Initializes the javascript manager. |
||
| 398 | * |
||
| 399 | */ |
||
| 400 | 25 | protected function initializeJavascriptManager() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Initializes the language factory; |
||
| 407 | */ |
||
| 408 | 25 | protected function initializeLanguageFactory() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * This method is called after initializing in the initialize method. |
||
| 415 | * Overwrite this method to do your own initialization. |
||
| 416 | * |
||
| 417 | * @return void |
||
| 418 | */ |
||
| 419 | 3 | protected function postInitialize() |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Overwrite this method to do own initialisations of the template. |
||
| 425 | * |
||
| 426 | * @param Template $template Template |
||
| 427 | * @return Template |
||
| 428 | */ |
||
| 429 | protected function postInitializeTemplateEngine(Template $template) |
||
| 433 | |||
| 434 | // Rendering |
||
| 435 | |||
| 436 | /** |
||
| 437 | * This method executes the requested commands and applies the changes to |
||
| 438 | * the template. |
||
| 439 | * |
||
| 440 | * @param $actionResult |
||
| 441 | * @return string Rendered plugin content |
||
| 442 | */ |
||
| 443 | abstract protected function render($actionResult); |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Renders a solr error. |
||
| 447 | * |
||
| 448 | * @return string A representation of the error that should be understandable for the user. |
||
| 449 | */ |
||
| 450 | protected function renderError() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Renders a solr exception. |
||
| 459 | * |
||
| 460 | * @return string A representation of the exception that should be understandable for the user. |
||
| 461 | */ |
||
| 462 | protected function renderException() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Should be overwritten to do things before rendering. |
||
| 471 | * |
||
| 472 | */ |
||
| 473 | 2 | protected function preRender() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Overwrite this method to perform changes to the content after rendering. |
||
| 479 | * |
||
| 480 | * @param string $content The content rendered by the plugin so far |
||
| 481 | * @return string The content that should be presented on the website, might be different from the output rendered before |
||
| 482 | */ |
||
| 483 | 25 | protected function postRender($content) |
|
| 491 | |||
| 492 | // Helper methods |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Determines the template file from the configuration. |
||
| 496 | * |
||
| 497 | * Overwrite this method to use a different template. |
||
| 498 | * |
||
| 499 | * @return string The template file name to be used for the plugin |
||
| 500 | */ |
||
| 501 | 25 | protected function getTemplateFile() |
|
| 505 | |||
| 506 | /** |
||
| 507 | * This method should be implemented to return the TSconfig key which |
||
| 508 | * contains the template name for this template. |
||
| 509 | * |
||
| 510 | * @see initializeTemplateEngine() |
||
| 511 | * @return string The TSconfig key containing the template name |
||
| 512 | */ |
||
| 513 | abstract protected function getTemplateFileKey(); |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Gets the plugin's template instance. |
||
| 517 | * |
||
| 518 | * @return Template The plugin's template. |
||
| 519 | */ |
||
| 520 | 25 | public function getTemplate() |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Gets the plugin's javascript manager. |
||
| 527 | * |
||
| 528 | * @return JavascriptManager The plugin's javascript manager. |
||
| 529 | */ |
||
| 530 | 25 | public function getJavascriptManager() |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Should return the relevant subpart of the template. |
||
| 537 | * |
||
| 538 | * @see initializeTemplateEngine() |
||
| 539 | * @return string The subpart of the template to be used |
||
| 540 | */ |
||
| 541 | abstract protected function getSubpart(); |
||
| 542 | |||
| 543 | /** |
||
| 544 | * This method should return the plugin key. Reads some configuration |
||
| 545 | * options in initializeTemplateEngine() |
||
| 546 | * |
||
| 547 | * @see initializeTemplateEngine() |
||
| 548 | * @return string The plugin key |
||
| 549 | */ |
||
| 550 | abstract protected function getPluginKey(); |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Gets the target page Id for links. Might have been set through either |
||
| 554 | * flexform or TypoScript. If none is set, TSFE->id is used. |
||
| 555 | * |
||
| 556 | * @return int The page Id to be used for links |
||
| 557 | */ |
||
| 558 | 18 | public function getLinkTargetPageId() |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Gets the user's query term and cleans it so that it can be used in |
||
| 565 | * templates f.e. |
||
| 566 | * |
||
| 567 | * @return string The cleaned user query. |
||
| 568 | */ |
||
| 569 | 25 | public function getCleanUserQuery() |
|
| 570 | { |
||
| 571 | 25 | $userQuery = $this->getRawUserQuery(); |
|
| 572 | |||
| 573 | 25 | if (!is_null($userQuery)) { |
|
| 574 | 21 | $userQuery = Query::cleanKeywords($userQuery); |
|
| 575 | 21 | } |
|
| 576 | |||
| 577 | // escape triple hashes as they are used in the template engine |
||
| 578 | // TODO remove after switching to fluid templates |
||
| 579 | 25 | $userQuery = Template::escapeMarkers($userQuery); |
|
| 580 | |||
| 581 | 25 | return $userQuery; |
|
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Gets the raw user query |
||
| 586 | * |
||
| 587 | * @return string Raw user query. |
||
| 588 | */ |
||
| 589 | 25 | public function getRawUserQuery() |
|
| 593 | |||
| 594 | /** |
||
| 595 | * @return string |
||
| 596 | */ |
||
| 597 | 25 | protected function getCurrentUrlWithQueryLinkBuilder() |
|
| 613 | } |
||
| 614 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.