From 3b330a478ee6f2bc40084a44c4503a0c18da2978 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Wed, 11 Nov 2020 23:14:15 +0100 Subject: [PATCH] [TASK] Refactor method fileResource of ContentObjectRenderer Resolves: #662 --- ...ontentObjectRendererFileResourceRector.php | 159 ++++++++++++++++++ stubs/Core/TypoScript/TemplateService.php | 6 +- .../ContentObject/ContentObjectRenderer.php | 24 +-- .../TypoScriptFrontendController.php | 10 +- ...ntObjectRendererFileResourceRectorTest.php | 31 ++++ .../tsfe_content_object_renderer.php.inc | 14 ++ ...e_content_object_renderer_in_class.php.inc | 67 ++++++++ 7 files changed, 285 insertions(+), 26 deletions(-) create mode 100644 src/Rector/v8/v5/ContentObjectRendererFileResourceRector.php create mode 100644 tests/Rector/v8/v5/ContentObjectRendererFileResource/ContentObjectRendererFileResourceRectorTest.php create mode 100644 tests/Rector/v8/v5/ContentObjectRendererFileResource/Fixture/tsfe_content_object_renderer.php.inc create mode 100644 tests/Rector/v8/v5/ContentObjectRendererFileResource/Fixture/tsfe_content_object_renderer_in_class.php.inc diff --git a/src/Rector/v8/v5/ContentObjectRendererFileResourceRector.php b/src/Rector/v8/v5/ContentObjectRendererFileResourceRector.php new file mode 100644 index 000000000..d60cf9a63 --- /dev/null +++ b/src/Rector/v8/v5/ContentObjectRendererFileResourceRector.php @@ -0,0 +1,159 @@ +typo3NodeResolver = $typo3NodeResolver; + } + + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): ?Node + { + if ($this->shouldSkip($node)) { + return null; + } + + if (! $this->isName($node->name, 'fileResource')) { + return null; + } + + $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); + + if (! $parentNode instanceof Assign) { + return null; + } + + $this->addInitializeVariableNode($node); + $this->addTypoScriptFrontendControllerAssignmentNode($node); + $this->addFileNameNode($node); + $this->addIfNode($node); + + $this->removeNode($parentNode); + + return null; + } + + /** + * @codeCoverageIgnore + */ + public function getDefinition(): RectorDefinition + { + return new RectorDefinition('Migrate fileResource method of class ContentObjectRenderer', [ + new CodeSample(<<<'PHP' +$template = $this->cObj->fileResource('EXT:vendor/Resources/Private/Templates/Template.html'); +PHP + , <<<'PHP' +$path = $GLOBALS['TSFE']->tmpl->getFileName('EXT:vendor/Resources/Private/Templates/Template.html'); +if ($path !== null && file_exists($path)) { + $template = file_get_contents($path); +} +PHP + ), + ]); + } + + private function shouldSkip(MethodCall $node): bool + { + if ($this->isObjectType($node->var, ContentObjectRenderer::class)) { + return false; + } + return ! $this->typo3NodeResolver->isMethodCallOnPropertyOfGlobals( + $node, + Typo3NodeResolver::TYPO_SCRIPT_FRONTEND_CONTROLLER, + 'cObj' + ); + } + + private function addInitializeVariableNode(MethodCall $node): void + { + $parentNode = $node->getAttribute('parent'); + if (! $parentNode->var instanceof PropertyFetch) { + $initializeVariable = new Expression(new Assign($parentNode->var, new String_(''))); + $this->addNodeBeforeNode($initializeVariable, $node); + } + } + + private function addTypoScriptFrontendControllerAssignmentNode(MethodCall $node): void + { + $typoscriptFrontendControllerVariable = new Variable('typoscriptFrontendController'); + $typoscriptFrontendControllerNode = new Assign( + $typoscriptFrontendControllerVariable, + new ArrayDimFetch(new Variable('GLOBALS'), new String_('TSFE')) + ); + $this->addNodeBeforeNode($typoscriptFrontendControllerNode, $node); + } + + private function addFileNameNode(MethodCall $node): void + { + $fileNameNode = new Assign( + new Variable(self::PATH), + $this->createMethodCall( + $this->createPropertyFetch(new Variable('typoscriptFrontendController'), 'tmpl'), + 'getFileName', + $node->args + ) + ); + $this->addNodeBeforeNode($fileNameNode, $node); + } + + private function addIfNode(MethodCall $node): void + { + $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); + + $ifNode = new If_(new BooleanAnd( + new NotIdentical(new Variable(self::PATH), $this->createNull()), + $this->createFuncCall('file_exists', [new Variable(self::PATH)]) + )); + + $templateAssignment = new Assign($parentNode->var, $this->createFuncCall( + 'file_get_contents', + [new Variable(self::PATH)] + )); + $ifNode->stmts[] = new Expression($templateAssignment); + + $this->addNodeBeforeNode($ifNode, $node); + } +} diff --git a/stubs/Core/TypoScript/TemplateService.php b/stubs/Core/TypoScript/TemplateService.php index f16362b60..07f5f9e95 100644 --- a/stubs/Core/TypoScript/TemplateService.php +++ b/stubs/Core/TypoScript/TemplateService.php @@ -11,8 +11,12 @@ class TemplateService { /** - * Passed to TypoScript template class and tells it to force template rendering * @var bool */ public $forceTemplateParsing = false; + + public function getFileName($file): void + { + + } } diff --git a/stubs/Frontend/ContentObject/ContentObjectRenderer.php b/stubs/Frontend/ContentObject/ContentObjectRenderer.php index bd2a692fc..80bd540bd 100644 --- a/stubs/Frontend/ContentObject/ContentObjectRenderer.php +++ b/stubs/Frontend/ContentObject/ContentObjectRenderer.php @@ -15,16 +15,14 @@ final class ContentObjectRenderer { public function RECORDS(array $config): void { - $this->cObjGetSingle('RECORDS', $config); } public function cObjGetSingle(string $string, array $config): void { } - public function enableFields($table, $show_hidden = false, array $ignore_array = []) + public function enableFields($table, $show_hidden = false, array $ignore_array = []): void { - return GeneralUtility::makeInstance(PageRepository::class)->enableFields($table, $show_hidden ? true : -1, $ignore_array); } public function getSubpart($content, $marker): void @@ -51,26 +49,20 @@ public function substituteMarkerArray($content, array $markContentArray, $wrap = { } - public function substituteMarkerInObject(&$tree, array $markContentArray) + public function substituteMarkerInObject(&$tree, array $markContentArray): void { - GeneralUtility::logDeprecatedFunction(); - if (is_array($tree)) { - foreach ($tree as $key => $value) { - $this->templateService->substituteMarkerInObject($tree[$key], $markContentArray); - } - } else { - $tree = $this->templateService->substituteMarkerArray($tree, $markContentArray); - } - - return $tree; } public function substituteMarkerAndSubpartArrayRecursive($content, array $markersAndSubparts, $wrap = '', $uppercase = false, $deleteUnused = false): void { } - public function fillInMarkerArray(array $markContentArray, array $row, $fieldList = '', $nl2br = true, $prefix = 'FIELD_', $HSC = false) + public function fillInMarkerArray(array $markContentArray, array $row, $fieldList = '', $nl2br = true, $prefix = 'FIELD_', $HSC = false): void { - return $this->templateService->fillInMarkerArray($markContentArray, $row, $fieldList, $nl2br, $prefix, $HSC, !empty($GLOBALS['TSFE']->xhtmlDoctype)); + } + + public function fileResource($file): void + { + } } diff --git a/stubs/Frontend/Controller/TypoScriptFrontendController.php b/stubs/Frontend/Controller/TypoScriptFrontendController.php index 9689cca7f..954a4d220 100644 --- a/stubs/Frontend/Controller/TypoScriptFrontendController.php +++ b/stubs/Frontend/Controller/TypoScriptFrontendController.php @@ -18,17 +18,11 @@ final class TypoScriptFrontendController { /** - * Doctype to use. - * - * Currently set via PageGenerator - * * @var string */ public $xhtmlDoctype = ''; /** - * Page content render object. - * * @var ContentObjectRenderer */ public $cObj = ''; @@ -59,14 +53,11 @@ final class TypoScriptFrontendController public $showHiddenRecords = ''; /** - * Passed to TypoScript template class and tells it to force template rendering * @var bool */ public $forceTemplateParsing = false; /** - * The TypoScript template object. Used to parse the TypoScript template - * * @var TemplateService */ public $tmpl; @@ -123,6 +114,7 @@ public function __construct() $this->language = new SiteLanguage(); $this->sys_language_isocode = 'ch'; $this->csConvObj = new CharsetConverter(); + $this->cObj = new ContentObjectRenderer(); } public function applyHttpHeadersToResponse(): void diff --git a/tests/Rector/v8/v5/ContentObjectRendererFileResource/ContentObjectRendererFileResourceRectorTest.php b/tests/Rector/v8/v5/ContentObjectRendererFileResource/ContentObjectRendererFileResourceRectorTest.php new file mode 100644 index 000000000..9cf2cb4f1 --- /dev/null +++ b/tests/Rector/v8/v5/ContentObjectRendererFileResource/ContentObjectRendererFileResourceRectorTest.php @@ -0,0 +1,31 @@ +doTestFileInfo($fileInfo); + } + + public function provideDataForTest(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return ContentObjectRendererFileResourceRector::class; + } +} diff --git a/tests/Rector/v8/v5/ContentObjectRendererFileResource/Fixture/tsfe_content_object_renderer.php.inc b/tests/Rector/v8/v5/ContentObjectRendererFileResource/Fixture/tsfe_content_object_renderer.php.inc new file mode 100644 index 000000000..dea0aef44 --- /dev/null +++ b/tests/Rector/v8/v5/ContentObjectRendererFileResource/Fixture/tsfe_content_object_renderer.php.inc @@ -0,0 +1,14 @@ +cObj->fileResource('EXT:vendor/Resources/Private/Templates/Template.html'); + +?> +----- +tmpl->getFileName('EXT:vendor/Resources/Private/Templates/Template.html'); +if ($path !== null && file_exists($path)) { + $template = file_get_contents($path); +} diff --git a/tests/Rector/v8/v5/ContentObjectRendererFileResource/Fixture/tsfe_content_object_renderer_in_class.php.inc b/tests/Rector/v8/v5/ContentObjectRendererFileResource/Fixture/tsfe_content_object_renderer_in_class.php.inc new file mode 100644 index 000000000..1816309a3 --- /dev/null +++ b/tests/Rector/v8/v5/ContentObjectRendererFileResource/Fixture/tsfe_content_object_renderer_in_class.php.inc @@ -0,0 +1,67 @@ +contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class); + } + + public function initialize(): void + { + $this->template = $this->contentObject->fileResource('EXT:vendor/Resources/Private/Templates/Template.html'); + } +} + +?> +----- +contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class); + } + + public function initialize(): void + { + $typoscriptFrontendController = $GLOBALS['TSFE']; + $path = $typoscriptFrontendController->tmpl->getFileName('EXT:vendor/Resources/Private/Templates/Template.html'); + if ($path !== null && file_exists($path)) { + $this->template = file_get_contents($path); + } + } +} + +?>