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) |
|
|
|||
118 | { |
||
119 | /** @noinspection PhpUnusedLocalVariableInspection */ |
||
120 | 25 | $content = ''; |
|
121 | |||
122 | try { |
||
123 | 25 | $this->initialize($configuration); |
|
124 | 25 | $this->preRender(); |
|
125 | |||
126 | 25 | $actionResult = $this->performAction(); |
|
127 | |||
128 | 25 | if ($this->getSearchResultSetService()->getIsSolrAvailable()) { |
|
129 | 25 | $content = $this->render($actionResult); |
|
130 | } else { |
||
131 | $content = $this->renderError(); |
||
132 | } |
||
133 | |||
134 | 25 | $content = $this->postRender($content); |
|
135 | } catch (\Exception $e) { |
||
136 | if ($this->typoScriptConfiguration->getLoggingExceptions()) { |
||
137 | GeneralUtility::devLog( |
||
138 | $e->getCode() . ': ' . $e->__toString(), |
||
139 | 'solr', |
||
140 | 3, |
||
141 | (array)$e |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | $this->initializeTemplateEngine(); |
||
146 | $content = $this->renderException(); |
||
147 | } |
||
148 | |||
149 | 25 | return $this->baseWrap($content); |
|
150 | } |
||
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 | if (isset($this->conf['general.']['baseWrap.'])) { |
|
162 | return $this->cObj->stdWrap($content, |
||
163 | $this->conf['general.']['baseWrap.']); |
||
164 | } else { |
||
165 | 25 | 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) |
|
185 | { |
||
186 | /** @var $configurationManager \ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager */ |
||
187 | 25 | $configurationManager = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\System\\Configuration\\ConfigurationManager'); |
|
188 | 25 | $typoScriptConfiguration = $configurationManager->getTypoScriptConfiguration()->mergeSolrConfiguration($configuration); |
|
189 | 25 | $this->typoScriptConfiguration = $typoScriptConfiguration; |
|
190 | |||
191 | 25 | $this->initializeLanguageFactory(); |
|
192 | 25 | $this->pi_setPiVarDefaults(); |
|
193 | 25 | $this->pi_loadLL(); |
|
194 | 25 | $this->pi_initPIflexForm(); |
|
195 | |||
196 | 25 | $this->overrideTyposcriptWithFlexformSettings(); |
|
197 | |||
198 | 25 | $this->initializeQuery(); |
|
199 | 25 | $this->initializeSearch(); |
|
200 | 25 | $this->initializeTemplateEngine(); |
|
201 | 25 | $this->initializeJavascriptManager(); |
|
202 | |||
203 | 25 | $this->postInitialize(); |
|
204 | 25 | } |
|
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() |
|
214 | { |
||
215 | 25 | if (is_array($this->conf['_DEFAULT_PI_VARS.'])) { |
|
216 | foreach ($this->conf['_DEFAULT_PI_VARS.'] as $key => $defaultValue) { |
||
217 | $this->conf['_DEFAULT_PI_VARS.'][$key] = $this->cObj->cObjGetSingle($this->conf['_DEFAULT_PI_VARS.'][$key], |
||
218 | $this->conf['_DEFAULT_PI_VARS.'][$key . '.']); |
||
219 | } |
||
220 | |||
221 | $piVars = is_array($this->piVars) ? $this->piVars : array(); |
||
222 | $this->piVars = $this->conf['_DEFAULT_PI_VARS.']; |
||
223 | ArrayUtility::mergeRecursiveWithOverrule( |
||
224 | $this->piVars, |
||
225 | $piVars |
||
226 | ); |
||
227 | } |
||
228 | 25 | } |
|
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 | $pathElements = pathinfo($this->scriptRelPath); |
|
246 | 25 | $languageFileName = $pathElements['filename']; |
|
247 | |||
248 | 25 | $basePath = 'EXT:' . $this->extKey . '/Resources/Private/Language/locallang.xlf'; |
|
249 | // Read the strings in the required charset (since TYPO3 4.2) |
||
250 | 25 | $this->LOCAL_LANG = $this->languageFactory->getParsedData($basePath, |
|
251 | 25 | $this->LLkey, $GLOBALS['TSFE']->renderCharset, 3); |
|
252 | |||
253 | 25 | $alternativeLanguageKeys = GeneralUtility::trimExplode(',', |
|
254 | 25 | $this->altLLkey, true); |
|
255 | 25 | foreach ($alternativeLanguageKeys as $languageKey) { |
|
256 | $tempLL = $this->languageFactory->getParsedData($basePath, $languageKey, $GLOBALS['TSFE']->renderCharset, 3); |
||
257 | if ($this->LLkey !== 'default' && isset($tempLL[$languageKey])) { |
||
258 | $this->LOCAL_LANG[$languageKey] = $tempLL[$languageKey]; |
||
259 | } |
||
260 | } |
||
261 | // Overlaying labels from TypoScript (including fictitious language keys for non-system languages!): |
||
262 | 25 | $translationInTypoScript = $this->typoScriptConfiguration->getLocalLangConfiguration(); |
|
263 | |||
264 | 25 | if (count($translationInTypoScript) == 0) { |
|
265 | 24 | return; |
|
266 | } |
||
267 | |||
268 | // Clear the "unset memory" |
||
269 | 1 | $this->LOCAL_LANG_UNSET = array(); |
|
270 | 1 | foreach ($translationInTypoScript as $languageKey => $languageArray) { |
|
271 | // Remove the dot after the language key |
||
272 | 1 | $languageKey = substr($languageKey, 0, -1); |
|
273 | // Don't process label if the language is not loaded |
||
274 | 1 | if (is_array($languageArray) && isset($this->LOCAL_LANG[$languageKey])) { |
|
275 | 1 | foreach ($languageArray as $labelKey => $labelValue) { |
|
276 | 1 | if (!is_array($labelValue)) { |
|
277 | 1 | $this->LOCAL_LANG[$languageKey][$labelKey][0]['target'] = $labelValue; |
|
278 | 1 | $this->LOCAL_LANG_charset[$languageKey][$labelKey] = 'utf-8'; |
|
279 | |||
280 | 1 | if ($labelValue === '') { |
|
281 | 1 | $this->LOCAL_LANG_UNSET[$languageKey][$labelKey] = ''; |
|
282 | } |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | 1 | $this->LOCAL_LANG_loaded = 1; |
|
289 | 1 | } |
|
290 | |||
291 | /** |
||
292 | * Allows to override TypoScript settings with Flexform values. |
||
293 | * |
||
294 | */ |
||
295 | 3 | protected function overrideTyposcriptWithFlexformSettings() |
|
296 | { |
||
297 | 3 | } |
|
298 | |||
299 | /** |
||
300 | * Initializes the query from the GET query parameter. |
||
301 | * |
||
302 | */ |
||
303 | 25 | protected function initializeQuery() |
|
304 | { |
||
305 | 25 | $this->rawUserQuery = GeneralUtility::_GET('q'); |
|
306 | 25 | } |
|
307 | |||
308 | /** |
||
309 | * Initializes the Solr connection and tests the connection through a ping. |
||
310 | * |
||
311 | */ |
||
312 | 25 | protected function initializeSearch() |
|
313 | { |
||
314 | 25 | $solrConnection = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\ConnectionManager')->getConnectionByPageId( |
|
315 | 25 | $GLOBALS['TSFE']->id, |
|
316 | 25 | $GLOBALS['TSFE']->sys_language_uid, |
|
317 | 25 | $GLOBALS['TSFE']->MP |
|
318 | ); |
||
319 | |||
320 | 25 | $search = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Search', $solrConnection); |
|
321 | /** @var $this->searchService ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService */ |
||
322 | 25 | $this->searchResultsSetService = GeneralUtility::makeInstance('ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService', $this->typoScriptConfiguration, $search, $this); |
|
323 | 25 | $this->solrAvailable = $this->searchResultsSetService->getIsSolrAvailable(); |
|
324 | 25 | $this->search = $this->searchResultsSetService->getSearch(); |
|
325 | 25 | } |
|
326 | |||
327 | /** |
||
328 | * @return SearchResultSetService |
||
329 | */ |
||
330 | 25 | public function getSearchResultSetService() |
|
331 | { |
||
332 | 25 | return $this->searchResultsSetService; |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * Initializes the template engine and returns the initialized instance. |
||
337 | * |
||
338 | * @return Template |
||
339 | * @throws \UnexpectedValueException if a view helper provider fails to implement interface ApacheSolrForTypo3\Solr\ViewHelper\ViewHelperProvider |
||
340 | */ |
||
341 | 25 | protected function initializeTemplateEngine() |
|
342 | { |
||
343 | 25 | $templateFile = $this->getTemplateFile(); |
|
344 | 25 | $subPart = $this->getSubpart(); |
|
345 | |||
346 | 25 | $flexformTemplateFile = $this->pi_getFFvalue( |
|
347 | 25 | $this->cObj->data['pi_flexform'], |
|
348 | 25 | 'templateFile', |
|
349 | 25 | 'sOptions' |
|
350 | ); |
||
351 | 25 | if (!empty($flexformTemplateFile)) { |
|
352 | $templateFile = $flexformTemplateFile; |
||
353 | } |
||
354 | |||
355 | /** @var Template $template */ |
||
356 | 25 | $template = GeneralUtility::makeInstance( |
|
357 | 25 | 'ApacheSolrForTypo3\\Solr\\Template', |
|
358 | 25 | $this->cObj, |
|
359 | $templateFile, |
||
360 | $subPart |
||
361 | ); |
||
362 | 25 | $template->addViewHelperIncludePath($this->extKey, |
|
363 | 25 | 'Classes/ViewHelper/'); |
|
364 | 25 | $template->addViewHelper('LLL', array( |
|
365 | 25 | 'languageFile' => 'EXT:solr/Resources/Private/Language/locallang.xlf', |
|
366 | 25 | 'llKey' => $this->LLkey |
|
367 | )); |
||
368 | |||
369 | // can be used for view helpers that need configuration during initialization |
||
370 | 25 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr'][$this->getPluginKey()]['addViewHelpers'])) { |
|
371 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr'][$this->getPluginKey()]['addViewHelpers'] as $classReference) { |
||
372 | $viewHelperProvider = &GeneralUtility::getUserObj($classReference); |
||
373 | |||
374 | if ($viewHelperProvider instanceof ViewHelperProvider) { |
||
375 | $viewHelpers = $viewHelperProvider->getViewHelpers(); |
||
376 | foreach ($viewHelpers as $helperName => $helperObject) { |
||
377 | // TODO check whether $helperAdded is TRUE, throw an exception if not |
||
378 | $helperAdded = $template->addViewHelperObject($helperName, |
||
379 | $helperObject); |
||
380 | } |
||
381 | } else { |
||
382 | throw new \UnexpectedValueException( |
||
383 | get_class($viewHelperProvider) . ' must implement interface ApacheSolrForTypo3\Solr\ViewHelper\ViewHelperProvider', |
||
384 | 1310387296 |
||
385 | ); |
||
386 | } |
||
387 | } |
||
388 | } |
||
389 | |||
390 | 25 | $template = $this->postInitializeTemplateEngine($template); |
|
391 | |||
392 | 25 | $this->template = $template; |
|
393 | 25 | } |
|
394 | |||
395 | /** |
||
396 | * Initializes the javascript manager. |
||
397 | * |
||
398 | */ |
||
399 | 25 | protected function initializeJavascriptManager() |
|
400 | { |
||
401 | 25 | $this->javascriptManager = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\JavascriptManager'); |
|
402 | 25 | } |
|
403 | |||
404 | /** |
||
405 | * Initializes the language factory; |
||
406 | */ |
||
407 | 25 | protected function initializeLanguageFactory() |
|
408 | { |
||
409 | 25 | $this->languageFactory = GeneralUtility::makeInstance('TYPO3\CMS\Core\Localization\LocalizationFactory'); |
|
410 | 25 | } |
|
411 | |||
412 | /** |
||
413 | * This method is called after initializing in the initialize method. |
||
414 | * Overwrite this method to do your own initialization. |
||
415 | * |
||
416 | * @return void |
||
417 | */ |
||
418 | 3 | protected function postInitialize() |
|
419 | { |
||
420 | 3 | } |
|
421 | |||
422 | /** |
||
423 | * Overwrite this method to do own initialisations of the template. |
||
424 | * |
||
425 | * @param Template $template Template |
||
426 | * @return Template |
||
427 | */ |
||
428 | protected function postInitializeTemplateEngine(Template $template) |
||
429 | { |
||
430 | return $template; |
||
431 | } |
||
432 | |||
433 | // Rendering |
||
434 | |||
435 | /** |
||
436 | * This method executes the requested commands and applies the changes to |
||
437 | * the template. |
||
438 | * |
||
439 | * @param $actionResult |
||
440 | * @return string Rendered plugin content |
||
441 | */ |
||
442 | abstract protected function render($actionResult); |
||
443 | |||
444 | /** |
||
445 | * Renders a solr error. |
||
446 | * |
||
447 | * @return string A representation of the error that should be understandable for the user. |
||
448 | */ |
||
449 | protected function renderError() |
||
450 | { |
||
451 | $this->template->workOnSubpart('solr_search_unavailable'); |
||
452 | |||
453 | return $this->template->render(); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Renders a solr exception. |
||
458 | * |
||
459 | * @return string A representation of the exception that should be understandable for the user. |
||
460 | */ |
||
461 | protected function renderException() |
||
462 | { |
||
463 | $this->template->workOnSubpart('solr_search_error'); |
||
464 | |||
465 | return $this->template->render(); |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Should be overwritten to do things before rendering. |
||
470 | * |
||
471 | */ |
||
472 | 2 | protected function preRender() |
|
473 | { |
||
474 | 2 | } |
|
475 | |||
476 | /** |
||
477 | * Overwrite this method to perform changes to the content after rendering. |
||
478 | * |
||
479 | * @param string $content The content rendered by the plugin so far |
||
480 | * @return string The content that should be presented on the website, might be different from the output rendered before |
||
481 | */ |
||
482 | 25 | protected function postRender($content) |
|
483 | { |
||
484 | 25 | if (isset($this->conf['stdWrap.'])) { |
|
485 | $content = $this->cObj->stdWrap($content, $this->conf['stdWrap.']); |
||
486 | } |
||
487 | |||
488 | 25 | return $content; |
|
489 | } |
||
490 | |||
491 | // Helper methods |
||
492 | |||
493 | /** |
||
494 | * Determines the template file from the configuration. |
||
495 | * |
||
496 | * Overwrite this method to use a different template. |
||
497 | * |
||
498 | * @return string The template file name to be used for the plugin |
||
499 | */ |
||
500 | 25 | protected function getTemplateFile() |
|
501 | { |
||
502 | 25 | return $this->typoScriptConfiguration->getTemplateByFileKey($this->getTemplateFileKey()); |
|
503 | } |
||
504 | |||
505 | /** |
||
506 | * This method should be implemented to return the TSconfig key which |
||
507 | * contains the template name for this template. |
||
508 | * |
||
509 | * @see initializeTemplateEngine() |
||
510 | * @return string The TSconfig key containing the template name |
||
511 | */ |
||
512 | abstract protected function getTemplateFileKey(); |
||
513 | |||
514 | /** |
||
515 | * Gets the plugin's template instance. |
||
516 | * |
||
517 | * @return Template The plugin's template. |
||
518 | */ |
||
519 | 25 | public function getTemplate() |
|
520 | { |
||
521 | 25 | return $this->template; |
|
522 | } |
||
523 | |||
524 | /** |
||
525 | * Gets the plugin's javascript manager. |
||
526 | * |
||
527 | * @return JavascriptManager The plugin's javascript manager. |
||
528 | */ |
||
529 | 25 | public function getJavascriptManager() |
|
530 | { |
||
531 | 25 | return $this->javascriptManager; |
|
532 | } |
||
533 | |||
534 | /** |
||
535 | * Should return the relevant subpart of the template. |
||
536 | * |
||
537 | * @see initializeTemplateEngine() |
||
538 | * @return string The subpart of the template to be used |
||
539 | */ |
||
540 | abstract protected function getSubpart(); |
||
541 | |||
542 | /** |
||
543 | * This method should return the plugin key. Reads some configuration |
||
544 | * options in initializeTemplateEngine() |
||
545 | * |
||
546 | * @see initializeTemplateEngine() |
||
547 | * @return string The plugin key |
||
548 | */ |
||
549 | abstract protected function getPluginKey(); |
||
550 | |||
551 | /** |
||
552 | * Gets the target page Id for links. Might have been set through either |
||
553 | * flexform or TypoScript. If none is set, TSFE->id is used. |
||
554 | * |
||
555 | * @return int The page Id to be used for links |
||
556 | */ |
||
557 | 18 | public function getLinkTargetPageId() |
|
558 | { |
||
559 | 18 | return $this->typoScriptConfiguration->getSearchTargetPage(); |
|
560 | } |
||
561 | |||
562 | /** |
||
563 | * Gets the user's query term and cleans it so that it can be used in |
||
564 | * templates f.e. |
||
565 | * |
||
566 | * @return string The cleaned user query. |
||
567 | */ |
||
568 | 25 | public function getCleanUserQuery() |
|
569 | { |
||
570 | 25 | $userQuery = $this->getRawUserQuery(); |
|
571 | |||
572 | 25 | if (!is_null($userQuery)) { |
|
573 | 21 | $userQuery = Query::cleanKeywords($userQuery); |
|
574 | } |
||
582 | |||
583 | /** |
||
584 | * Gets the raw user query |
||
585 | * |
||
586 | * @return string Raw user query. |
||
587 | */ |
||
588 | 25 | public function getRawUserQuery() |
|
592 | |||
593 | /** |
||
594 | * @return string |
||
595 | */ |
||
596 | 25 | protected function getCurrentUrlWithQueryLinkBuilder() |
|
612 | } |
||
613 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.