Skip to content

Commit

Permalink
Refactoring More for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
hojalatheef committed Sep 8, 2023
1 parent 41c3e27 commit 99b02db
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 47 deletions.
89 changes: 50 additions & 39 deletions Classes/Helper/ReplacerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,50 +45,52 @@ class ReplacerHelper implements LoggerAwareInterface
*/
public function replace(string $contentToReplace): string
{
try {
$typoScriptFrontendController = $this->getTypoScriptFrontendController();
$replacerConfig = ArrayUtility::getValueByPath(
$typoScriptFrontendController->config,
'config/tx_replacer.'
$typoScriptFrontendController = $this->getTypoScriptFrontendController();
$replacerConfig = $this->getArrayValueByPath(
$typoScriptFrontendController->config,
'config/tx_replacer.'
);
return $this->doProcessingForReplacerConfig($contentToReplace, $replacerConfig);
}

protected function doProcessingForReplacerConfig(string $contentToReplace, array $replacerConfig): string
{
$typoscriptConfigurations = $this->getSearchAndReplaceConfigurations($replacerConfig);

if (is_array($typoscriptConfigurations['search']) && is_array($typoscriptConfigurations['replace'])) {
// this will do if the typoscript configuration contains stdWrap
$searchAndReplaceConfigurations = $this->doStandardWrapProcessing(
$typoscriptConfigurations,
$replacerConfig
);
$typoscriptConfigurations = $this->getSearchAndReplaceConfigurations($replacerConfig);

if (is_array($typoscriptConfigurations['search']) && is_array($typoscriptConfigurations['replace'])) {
// this will do if the typoscript configuration contains stdWrap
$searchAndReplaceConfigurations = $this->doStandardWrapProcessing($typoscriptConfigurations);
$search = ArrayUtility::getValueByPath($searchAndReplaceConfigurations, 'search');
$replace = ArrayUtility::getValueByPath($searchAndReplaceConfigurations, 'replace');

// Only replace if search and replace count are equal
if (count($search) === count($replace)) {
if ((is_array($replacerConfig))
&& array_key_exists('enable_regex', $replacerConfig)
&& (int)$replacerConfig['enable_regex'] === 1
) {
// replace using a regex as search pattern
$contentToReplace = preg_replace($search, $replace, $contentToReplace);
} else {
// replace using a regular string as search pattern
$contentToReplace = str_replace($search, $replace, $contentToReplace);
}
$search = $this->getArrayValueByPath($searchAndReplaceConfigurations, 'search');
$replace = $this->getArrayValueByPath($searchAndReplaceConfigurations, 'replace');

// Only replace if search and replace count are equal
if (count($search) === count($replace)) {
if (array_key_exists('enable_regex', $replacerConfig)
&& (int)$replacerConfig['enable_regex'] === 1
) {
// replace using a regex as search pattern
$contentToReplace = preg_replace($search, $replace, $contentToReplace);
} else {
$this->logger->log(
LogLevel::ERROR,
'Each search item must have a replace item!',
$replacerConfig
);
// replace using a regular strings as search pattern
$contentToReplace = str_replace($search, $replace, $contentToReplace);
}
} else {
$this->logger->log(
LogLevel::ERROR,
'Each search item must have a replace item!',
$replacerConfig
);
}
} catch (MissingArrayPathException $missingArrayPathException) {
// If value does not exist then no replacement
}
return $contentToReplace;
}

private function doStandardWrapProcessing(array $typoscriptConfigurations): array
protected function doStandardWrapProcessing(array $typoscriptConfigurations, array $replacerConfig): array
{
$processedConfigurations = [];
$replacerConfig = $this->getTypoScriptFrontendController()->config['config']['tx_replacer.'];
foreach ($typoscriptConfigurations as $configurationPointer => $config) {
foreach ($config as $key => $content) {
if ($this->shouldSkipKey($key)) {
Expand All @@ -110,12 +112,12 @@ private function doStandardWrapProcessing(array $typoscriptConfigurations): arra
return $processedConfigurations;
}

private function shouldSkipKey($key): bool
protected function shouldSkipKey($key): bool
{
return is_string($key) && substr($key, -1) === '.';
}

private function processContent($content, $configKey): ?string
protected function processContent($content, $configKey): ?string
{
if (is_array($configKey) && $this->getTypoScriptFrontendController()->cObj instanceof ContentObjectRenderer) {
return $this->getTypoScriptFrontendController()->cObj->stdWrap($content, $configKey);
Expand All @@ -124,11 +126,20 @@ private function processContent($content, $configKey): ?string
return $content;
}

private function getSearchAndReplaceConfigurations(array $replacerConfig): array
protected function getSearchAndReplaceConfigurations(array $replacerConfig): array
{
return [
'search' => ArrayUtility::getValueByPath($replacerConfig, 'search.'),
'replace' => ArrayUtility::getValueByPath($replacerConfig, 'replace.'),
'search' => $this->getArrayValueByPath($replacerConfig, 'search.'),
'replace' => $this->getArrayValueByPath($replacerConfig, 'replace.'),
];
}

protected function getArrayValueByPath(array $array, $path): array
{
try {
return ArrayUtility::getValueByPath($array, $path);
} catch (MissingArrayPathException $missingArrayPathException) {
return [];
}
}
}
2 changes: 1 addition & 1 deletion Classes/Hooks/TypoScriptFrontendControllerHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Class TypoScriptFrontendControllerHook
* Used for Hook $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all']
*/
class TypoScriptFrontendControllerHook
Expand All @@ -33,6 +32,7 @@ public function contentPostProcAll(
if ($ref->isINTincScript()) {
return;
}

$ref->content = $this->getReplacerHelper()->replace($ref->content);
}

Expand Down
11 changes: 4 additions & 7 deletions Tests/Unit/Helper/ReplacerHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ final class ReplacerHelperTest extends UnitTestCase

protected function setUp(): void
{
$this->configurationManager = $this->getMockBuilder(ConfigurationManager::class)
->disableOriginalConstructor()->getMock();
$this->logger = new NullLogger();

parent::setUp();

$this->configurationManager = $this->createMock(ConfigurationManager::class);
$this->logger = new NullLogger();
}

protected function setupSubject(): void
Expand Down Expand Up @@ -174,9 +173,7 @@ public function replaceContentWithMissingSearchOrReplaceValuesWritesLogEntry(arr
*/
protected function createFrontendControllerMock(array $config = []): void
{
$GLOBALS['TSFE'] = $this->getMockBuilder(TypoScriptFrontendController::class)
->disableOriginalConstructor()
->getMock();
$GLOBALS['TSFE'] = $this->createMock(TypoScriptFrontendController::class);
$GLOBALS['TSFE']->cObj = new ContentObjectRenderer($GLOBALS['TSFE']);
// Set the configuration
$configProperty = new \ReflectionProperty($GLOBALS['TSFE'], 'config');
Expand Down

0 comments on commit 99b02db

Please sign in to comment.