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