Skip to content

Commit 8397792

Browse files
committed
[TASK] Refactor request handling in controller
1 parent 96a0898 commit 8397792

2 files changed

Lines changed: 64 additions & 35 deletions

File tree

Classes/Controller/FileController.php

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,34 @@
1515

1616
namespace Evoweb\EwLlxml2xliff\Controller;
1717

18-
use Evoweb\EwLlxml2xliff\File\Converter;
1918
use Evoweb\EwLlxml2xliff\Service\ExtensionService;
19+
use Exception;
2020
use Psr\Http\Message\ResponseInterface;
2121
use Psr\Http\Message\ServerRequestInterface;
2222
use TYPO3\CMS\Backend\Attribute\AsController;
2323
use TYPO3\CMS\Backend\Routing\UriBuilder;
2424
use TYPO3\CMS\Backend\Template\Components\ComponentFactory;
2525
use TYPO3\CMS\Backend\Template\ModuleTemplate;
2626
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
27-
use TYPO3\CMS\Core\Http\PropagateResponseException;
2827
use TYPO3\CMS\Core\Imaging\IconFactory;
2928
use TYPO3\CMS\Core\Imaging\IconSize;
3029
use TYPO3\CMS\Core\Localization\LanguageService;
31-
use TYPO3\CMS\Core\Page\PageRenderer;
3230

3331
#[AsController]
3432
readonly class FileController
3533
{
3634
public function __construct(
37-
protected IconFactory $iconFactory,
38-
protected UriBuilder $uriBuilder,
39-
protected PageRenderer $pageRenderer,
4035
protected ModuleTemplateFactory $moduleTemplateFactory,
41-
protected Converter $fileConverter,
42-
protected ExtensionService $extensionService,
36+
protected UriBuilder $uriBuilder,
4337
protected ComponentFactory $componentFactory,
44-
) {}
38+
protected IconFactory $iconFactory,
39+
protected ExtensionService $extensionService,
40+
) {
41+
}
4542

4643
public function selectExtensionAction(ServerRequestInterface $request): ResponseInterface
4744
{
48-
[$extensions] = $this->prepareExtensions($request, false);
45+
[, $extensions] = $this->prepareExtensions($request, false);
4946

5047
$moduleTemplate = $this->initializeModuleTemplate($request, 'ew_llxml2xliff.messages:extension');
5148
$moduleTemplate->assign('extensions', $extensions);
@@ -54,8 +51,14 @@ public function selectExtensionAction(ServerRequestInterface $request): Response
5451

5552
public function selectFileAction(ServerRequestInterface $request): ResponseInterface
5653
{
57-
[$extensions, $selectedExtension, $selectedExtensionKey] = $this->prepareExtensions($request);
58-
[$files] = $this->prepareFiles($request, $selectedExtensionKey, false);
54+
[$response, $extensions, $selectedExtension, $selectedExtensionKey] = $this->prepareExtensions($request);
55+
if ($response !== null) {
56+
return $response;
57+
}
58+
[$response, $files] = $this->prepareFiles($request, $selectedExtensionKey, false);
59+
if ($response !== null) {
60+
return $response;
61+
}
5962

6063
$moduleTemplate = $this->initializeModuleTemplate($request, 'ew_llxml2xliff.messages:file');
6164
$moduleTemplate->assignMultiple([
@@ -69,8 +72,14 @@ public function selectFileAction(ServerRequestInterface $request): ResponseInter
6972

7073
public function confirmConversionAction(ServerRequestInterface $request): ResponseInterface
7174
{
72-
[$extensions, $selectedExtension, $selectedExtensionKey] = $this->prepareExtensions($request);
73-
[$files, $selectedFile, $selectedFileKey] = $this->prepareFiles($request, $selectedExtensionKey);
75+
[$response, $extensions, $selectedExtension, $selectedExtensionKey] = $this->prepareExtensions($request);
76+
if ($response !== null) {
77+
return $response;
78+
}
79+
[$response, $files, $selectedFile, $selectedFileKey] = $this->prepareFiles($request, $selectedExtensionKey);
80+
if ($response !== null) {
81+
return $response;
82+
}
7483

7584
$moduleTemplate = $this->initializeModuleTemplate($request, 'ew_llxml2xliff.messages:confirm_selection');
7685
$moduleTemplate->assignMultiple([
@@ -86,8 +95,14 @@ public function confirmConversionAction(ServerRequestInterface $request): Respon
8695

8796
public function convertFileAction(ServerRequestInterface $request): ResponseInterface
8897
{
89-
[$extensions, $selectedExtension, $selectedExtensionKey] = $this->prepareExtensions($request);
90-
[$files, $selectedFile, $selectedFileKey] = $this->prepareFiles($request, $selectedExtensionKey);
98+
[$response, $extensions, $selectedExtension, $selectedExtensionKey] = $this->prepareExtensions($request);
99+
if ($response !== null) {
100+
return $response;
101+
}
102+
[$response, $files, $selectedFile, $selectedFileKey] = $this->prepareFiles($request, $selectedExtensionKey);
103+
if ($response !== null) {
104+
return $response;
105+
}
91106

92107
$conversionResult = $this->extensionService
93108
->convertLanguageFile($selectedExtensionKey, $selectedFileKey, $files);
@@ -106,12 +121,14 @@ public function convertFileAction(ServerRequestInterface $request): ResponseInte
106121
}
107122

108123
/**
109-
* @return array<string|array<array<string, string>>>
124+
* @return array<ResponseInterface|string|array<array<string, string>>|null>
110125
*/
111126
protected function prepareExtensions(ServerRequestInterface $request, bool $selected = true): array
112127
{
113128
$extensions = $this->extensionService->getLocalExtensions();
114-
$selectedExtensionKey = $selected ? $this->getSelectedExtension($request, $extensions) : '';
129+
[$response, $selectedExtensionKey] = $selected
130+
? $this->getSelectedExtension($request, $extensions)
131+
: [null, ''];
115132
$selectedExtension = null;
116133

117134
foreach ($extensions as &$extension) {
@@ -123,16 +140,18 @@ protected function prepareExtensions(ServerRequestInterface $request, bool $sele
123140
}
124141
}
125142

126-
return [$extensions, $selectedExtension, $selectedExtensionKey];
143+
return [$response, $extensions, $selectedExtension, $selectedExtensionKey];
127144
}
128145

129146
/**
130-
* @return array<string|array<array<string, string>>>
147+
* @return array<ResponseInterface|string|array<array<string, string>>|null>
131148
*/
132149
protected function prepareFiles(ServerRequestInterface $request, string $extension, bool $selected = true): array
133150
{
134151
$files = $this->extensionService->getFilesOfExtension($extension);
135-
$selectedFileKey = $selected ? $this->getSelectedFile($request, $files) : '';
152+
[$response, $selectedFileKey] = $selected
153+
? $this->getSelectedFile($request, $files)
154+
: [null, ''];
136155
$selectedFile = null;
137156

138157
foreach ($files as &$file) {
@@ -144,31 +163,35 @@ protected function prepareFiles(ServerRequestInterface $request, string $extensi
144163
}
145164
}
146165

147-
return [$files, $selectedFile, $selectedFileKey];
166+
return [$response, $files, $selectedFile, $selectedFileKey];
148167
}
149168

150169
/**
151170
* @param array<string, mixed> $extensions
171+
* @return array<ResponseInterface|string|null>
152172
*/
153-
protected function getSelectedExtension(ServerRequestInterface $request, array $extensions): string
173+
protected function getSelectedExtension(ServerRequestInterface $request, array $extensions): array
154174
{
175+
$response = null;
155176
$selectedExtension = $this->isArgumentSetAndAvailable($request, $extensions, 'extension');
156177
if ($selectedExtension === '') {
157-
throw new PropagateResponseException($this->selectExtensionAction($request));
178+
$response = $this->selectExtensionAction($request);
158179
}
159-
return $selectedExtension;
180+
return [$response, $selectedExtension];
160181
}
161182

162183
/**
163184
* @param array<string, mixed> $files
185+
* @return array<ResponseInterface|string|null>
164186
*/
165-
protected function getSelectedFile(ServerRequestInterface $request, array $files): string
187+
protected function getSelectedFile(ServerRequestInterface $request, array $files): array
166188
{
189+
$response = null;
167190
$selectedFile = $this->isArgumentSetAndAvailable($request, $files, 'file');
168191
if ($selectedFile === '') {
169-
throw new PropagateResponseException($this->selectFileAction($request));
192+
$response = $this->selectFileAction($request);
170193
}
171-
return $selectedFile;
194+
return [$response, $selectedFile];
172195
}
173196

174197
/**
@@ -204,7 +227,7 @@ protected function initializeModuleTemplate(ServerRequestInterface $request, str
204227
->setShowLabelText(true)
205228
->setIcon($this->iconFactory->getIcon('actions-plus', IconSize::SMALL));
206229
$buttonBar->addButton($newFileConversionButton);
207-
} catch (\Exception) {
230+
} catch (Exception) {
208231
}
209232

210233
return $moduleTemplate;

Classes/Localization/Parser/LocallangXmlParser.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class LocallangXmlParser
4141
/**
4242
* Loads the current XML file before processing.
4343
*
44-
* @return array<string, array<string, string>> An array representing a parsed XML file (structure depends on concrete parser)
44+
* @return array<string, array<string, string>> An array representing a
45+
* parsed XML file (structure depends on concrete parser)
4546
* @throws InvalidXmlFileException
4647
*/
4748
protected function parseXmlFile(): array
@@ -57,7 +58,8 @@ protected function parseXmlFile(): array
5758
if ($rootXmlNode === false) {
5859
$xmlError = libxml_get_last_error();
5960
throw new InvalidXmlFileException(
60-
'The path provided does not point to an existing and accessible well-formed XML file. Reason: ' . $xmlError->message . ' in ' . $this->sourcePath . ', line ' . $xmlError->line,
61+
'The path provided does not point to an existing and accessible well-formed XML file. Reason: '
62+
. $xmlError->message . ' in ' . $this->sourcePath . ', line ' . $xmlError->line,
6163
1278155988
6264
);
6365
}
@@ -70,7 +72,8 @@ protected function parseXmlFile(): array
7072
*
7173
* @param string $fileRef Absolute file reference to a locallang file
7274
* @param string $language Language key
73-
* @param bool $sameLocation If TRUE, then locallang localization file name will be returned with the same directory as $fileRef
75+
* @param bool $sameLocation If TRUE, then locallang localization file
76+
* name will be returned with the same directory as $fileRef
7477
* @return string Absolute path to the language file
7578
*/
7679
protected function getLocalizedFileName(string $fileRef, string $language, bool $sameLocation = false): string
@@ -104,8 +107,10 @@ protected function getLocalizedFileName(string $fileRef, string $language, bool
104107
}
105108
// Add an empty first-entry if not there.
106109
[$file_extPath, $file_fileName] = $temp;
107-
// The filename is prefixed with "[language key]." because it prevents the llxmltranslate tool from detecting it.
108-
return Environment::getLabelsPath() . '/' . $language . '/' . $extensionKey . '/' . ($file_extPath ? $file_extPath . '/' : '') . $language . '.' . $file_fileName;
110+
// The filename is prefixed with "[language key]." because
111+
// it prevents the llxmltranslate tool from detecting it.
112+
return Environment::getLabelsPath() . '/' . $language . '/' . $extensionKey . '/'
113+
. ($file_extPath ? $file_extPath . '/' : '') . $language . '.' . $file_fileName;
109114
}
110115
return '';
111116
}
@@ -202,7 +207,8 @@ protected function doParsingFromRootForElement(SimpleXMLElement $root, string $e
202207
$bodyOfFileTag = $root->data->languageKey;
203208
if ($bodyOfFileTag === null) {
204209
throw new InvalidXmlFileException(
205-
'Invalid locallang.xml language file "' . PathUtility::stripPathSitePrefix($this->sourcePath) . '"',
210+
'Invalid locallang.xml language file "'
211+
. substr($this->sourcePath, strlen(Environment::getPublicPath() . '/')) . '"',
206212
1487944884
207213
);
208214
}

0 commit comments

Comments
 (0)