diff --git a/Classes/Controller/RedirectController.php b/Classes/Controller/RedirectController.php index 6150c7e..3b2566e 100644 --- a/Classes/Controller/RedirectController.php +++ b/Classes/Controller/RedirectController.php @@ -4,38 +4,21 @@ use Psr\Http\Message\ResponseInterface; use In2code\Ipandlanguageredirect\Domain\Service\RedirectService; -use In2code\Ipandlanguageredirect\Utility\FrontendUtility; -use In2code\Ipandlanguageredirect\Utility\ObjectUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\HttpUtility; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException; +use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; /** * Class RedirectController */ class RedirectController extends ActionController { + public function __construct( + private readonly RedirectService $redirectService + ) {} - /** - * @var array - */ - protected $testArguments = [ - [ - 'browserLanguage' => 'de', - 'referrer' => 'http://www.google.de?foo=bar', - 'ipAddress' => '192.168.0.1', - 'languageUid' => '0', - 'rootpageUid' => '1' - ], - [ - 'browserLanguage' => 'de', - 'referrer' => 'http://www.google.de?foo=bar', - 'ipAddress' => '', - 'languageUid' => '0', - 'rootpageUid' => '1' - ] - ]; /** * Enrich call with ip-address if not given @@ -79,8 +62,7 @@ public function redirectAction( string $domain = '' ): ResponseInterface { /** @noinspection PhpMethodParametersCountMismatchInspection */ - $redirectService = $this->objectManager->get( - RedirectService::class, + $this->redirectService->set( $browserLanguage, $referrer, $ipAddress, @@ -89,35 +71,6 @@ public function redirectAction( $countryCode, $domain ); - return $this->jsonResponse(json_encode($redirectService->buildParameters())); - } - - /** - * Test the redirectAction directly with some predefined parameters from a given set - * call index.php?id=2&type=1556&tx_ipandlanguageredirect_pi1[set]=1 - * - * @param int $set - * @return void - */ - public function testAction($set = 0): ResponseInterface - { - $configuration = [ - 'parameter' => ObjectUtility::getTyposcriptFrontendController()->id, - 'additionalParams' => - FrontendUtility::getParametersStringFromArray($this->testArguments[$set]) . '&type=1555' - ]; - $uri = ObjectUtility::getContentObject()->typoLink_URL($configuration); - HttpUtility::redirect($uri, HttpUtility::HTTP_STATUS_307); - return $this->htmlResponse(); - } - - /** - * Render a suggest container that can be slided down in FE - * - * @return void - */ - public function suggestAction(): ResponseInterface - { - return $this->htmlResponse(); + return $this->jsonResponse(json_encode($this->redirectService->buildParameters())); } } diff --git a/Classes/Controller/SuggestController.php b/Classes/Controller/SuggestController.php new file mode 100644 index 0000000..6c07922 --- /dev/null +++ b/Classes/Controller/SuggestController.php @@ -0,0 +1,26 @@ +htmlResponse(); + } +} diff --git a/Classes/Controller/TestController.php b/Classes/Controller/TestController.php new file mode 100644 index 0000000..b39bc41 --- /dev/null +++ b/Classes/Controller/TestController.php @@ -0,0 +1,75 @@ + 'de', + 'referrer' => 'http://www.google.de?foo=bar', + 'ipAddress' => '192.168.0.1', + 'languageUid' => '0', + 'rootpageUid' => '1' + ], + [ + 'browserLanguage' => 'de', + 'referrer' => 'http://www.google.de?foo=bar', + 'ipAddress' => '', + 'languageUid' => '0', + 'rootpageUid' => '1' + ] + ]; + + public function __construct( + private readonly ContentObjectRenderer $contentObjectRenderer + ) + { + } + + /** + * Test the redirectAction directly with some predefined parameters from a given set + * call index.php?id=2&type=1556&tx_ipandlanguageredirect_pi1[set]=1 + * + * @param int $set + * @return void + */ + public function testAction($set = 0): ResponseInterface + { + $configuration = [ + 'parameter' => ObjectUtility::getTyposcriptFrontendController()->id, + 'additionalParams' => + FrontendUtility::getParametersStringFromArray($this->testArguments[$set]) . '&type=1555' + ]; + + $uri = $this->contentObjectRenderer->typoLink_URL($configuration); + + // Create redirect response + $response = $this->responseFactory + ->createResponse(303) + ->withAddedHeader('location', $uri); + + // Return Response directly + return $response; + + // or throw PropagateResponseException + throw new PropagateResponseException($response); + } +} diff --git a/Classes/Domain/Model/ActionSet.php b/Classes/Domain/Model/ActionSet.php index db9ee80..e147d86 100644 --- a/Classes/Domain/Model/ActionSet.php +++ b/Classes/Domain/Model/ActionSet.php @@ -2,6 +2,7 @@ namespace In2code\Ipandlanguageredirect\Domain\Model; use In2code\Ipandlanguageredirect\Utility\ObjectUtility; +use TYPO3\CMS\Core\Utility\GeneralUtility; /** * Class ActionSet @@ -34,7 +35,7 @@ public function __construct(array $configuration) $this->rawQuantifierConfiguration = $configuration['quantifier']; $this->rawActionsConfiguration = $configuration['actions']; foreach ($this->rawActionsConfiguration as $actionConfiguration) { - $action = ObjectUtility::getObjectManager()->get( + $action = GeneralUtility::makeInstance( Action::class, $actionConfiguration ); diff --git a/Classes/Domain/Model/ConfigurationSet.php b/Classes/Domain/Model/ConfigurationSet.php index 4436f33..f3879b4 100644 --- a/Classes/Domain/Model/ConfigurationSet.php +++ b/Classes/Domain/Model/ConfigurationSet.php @@ -2,6 +2,7 @@ namespace In2code\Ipandlanguageredirect\Domain\Model; use In2code\Ipandlanguageredirect\Utility\ObjectUtility; +use TYPO3\CMS\Core\Utility\GeneralUtility; /** * Class ConfigurationSet @@ -44,7 +45,7 @@ public function __construct(array $configuration, int $rootpageUid) $this->rawRedirectConfiguration = $configuration['redirectConfiguration']; foreach ($this->rawRedirectConfiguration as $pageIdentifier => $treeConfiguration) { foreach ($treeConfiguration as $languageParameter => $setConfiguration) { - $configuration = ObjectUtility::getObjectManager()->get( + $configuration = GeneralUtility::makeInstance( Configuration::class, $pageIdentifier, $languageParameter, diff --git a/Classes/Domain/Service/IpToCountry.php b/Classes/Domain/Service/IpToCountry.php index 2dec476..203d547 100644 --- a/Classes/Domain/Service/IpToCountry.php +++ b/Classes/Domain/Service/IpToCountry.php @@ -4,7 +4,9 @@ use In2code\Ipandlanguageredirect\Domain\Service\IpToCountry\IpToCountryInterface; use In2code\Ipandlanguageredirect\Utility\ConfigurationUtility; use In2code\Ipandlanguageredirect\Utility\ObjectUtility; +use Psr\Container\ContainerInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Object\Container\Container as ExtbaseContainer; /** * Class IpToCountry @@ -25,7 +27,7 @@ public function getCountryFromIp(string $ipAddress = ''): string $countryCode = ''; foreach ($this->getClasses() as $class) { /** @var IpToCountryInterface $countryFromIp */ - $countryFromIp = ObjectUtility::getObjectManager()->get($class, $ipAddress); + $countryFromIp = GeneralUtility::makeInstance($class, $ipAddress); try { $countryCode = $countryFromIp->getCountryCodeFromIp(); } catch (\Exception $exception) { diff --git a/Classes/Domain/Service/IpToCountry/AbstractIpToCountry.php b/Classes/Domain/Service/IpToCountry/AbstractIpToCountry.php index cfce6df..f5efc56 100644 --- a/Classes/Domain/Service/IpToCountry/AbstractIpToCountry.php +++ b/Classes/Domain/Service/IpToCountry/AbstractIpToCountry.php @@ -2,7 +2,7 @@ declare(strict_types=1); namespace In2code\Ipandlanguageredirect\Domain\Service\IpToCountry; -use In2code\Ipandlanguageredirect\Utility\ObjectUtility; +use TYPO3\CMS\Core\SingletonInterface; use TYPO3\CMS\Core\Utility\ArrayUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; @@ -10,7 +10,7 @@ /** * Class AbstractIpToCountry */ -abstract class AbstractIpToCountry +abstract class AbstractIpToCountry implements SingletonInterface { /** @@ -23,7 +23,7 @@ abstract class AbstractIpToCountry /** * @param string $ipAddress */ - public function __construct(string $ipAddress) + public function __construct(string $ipAddress = '') { $this->ipAddress = $ipAddress; } @@ -48,7 +48,7 @@ protected function getCurrentIpAddress(): string protected function getConfiguration(string $className, string $path = '') { $settings = []; - $configurationManager = ObjectUtility::getObjectManager()->get(ConfigurationManagerInterface::class); + $configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class); $setup = $configurationManager->getConfiguration( ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, 'Ipandlanguageredirect' diff --git a/Classes/Domain/Service/RedirectService.php b/Classes/Domain/Service/RedirectService.php index 052994f..2534157 100644 --- a/Classes/Domain/Service/RedirectService.php +++ b/Classes/Domain/Service/RedirectService.php @@ -104,6 +104,11 @@ class RedirectService 'events' => ['none'] ]; + + public function __construct( + private readonly UriBuilder $uriBuilder + ) {} + /** * @param string $browserLanguage * @param string $referrer @@ -113,7 +118,7 @@ class RedirectService * @param string $countryCode * @param string $domain */ - public function __construct( + public function set( string $browserLanguage, string $referrer, string $ipAddress, @@ -129,7 +134,7 @@ public function __construct( $this->rootpageUid = $rootpageUid; $this->countryCodeOverlay = $countryCode; if ($this->countryCodeOverlay === '') { - $ipToCountry = ObjectUtility::getObjectManager()->get(IpToCountry::class); + $ipToCountry = GeneralUtility::makeInstance(IpToCountry::class); $this->countryCode = $ipToCountry->getCountryFromIp($ipAddress); } else { $this->countryCode = $this->countryCodeOverlay; @@ -214,7 +219,7 @@ protected function getBestMatchingLanguageParameter() protected function getBestConfiguration() { if ($this->bestConfiguration === null) { - $configurationSet = ObjectUtility::getObjectManager()->get( + $configurationSet = GeneralUtility::makeInstance( ConfigurationSet::class, $this->configuration, $this->rootpageUid @@ -238,11 +243,10 @@ protected function getBestConfiguration() */ protected function getUriToPageAndLanguage($pageIdentifier = 0, $languageParameter = 0): string { - $uriBuilder = ObjectUtility::getObjectManager()->get(UriBuilder::class); - $uriBuilder->setTargetPageUid($this->getTargetPageForUriCreation($pageIdentifier)); - $uriBuilder->setCreateAbsoluteUri(true); - $uriBuilder->setArguments([$this->languageParameter => $languageParameter]); - return $uriBuilder->buildFrontendUri(); + $this->uriBuilder->setTargetPageUid($this->getTargetPageForUriCreation($pageIdentifier)); + $this->uriBuilder->setCreateAbsoluteUri(true); + $this->uriBuilder->setArguments([$this->languageParameter => $languageParameter]); + return $this->uriBuilder->buildFrontendUri(); } /** @@ -251,7 +255,7 @@ protected function getUriToPageAndLanguage($pageIdentifier = 0, $languageParamet protected function getEvents() { if ($this->bestEvents === null) { - $actionSet = ObjectUtility::getObjectManager()->get(ActionSet::class, $this->configuration); + $actionSet = GeneralUtility::makeInstance(ActionSet::class, $this->configuration); $actionSet->calculateQuantifiers($this->referrer); $events = $actionSet->getEvents(); } else { diff --git a/Classes/Utility/ObjectUtility.php b/Classes/Utility/ObjectUtility.php index 2ef4543..cd7b6da 100644 --- a/Classes/Utility/ObjectUtility.php +++ b/Classes/Utility/ObjectUtility.php @@ -4,8 +4,6 @@ use TYPO3\CMS\Core\Log\Logger; use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Extbase\Object\ObjectManager; -use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; /** @@ -14,14 +12,6 @@ class ObjectUtility { - /** - * @return ObjectManager - */ - public static function getObjectManager() - { - return GeneralUtility::makeInstance(ObjectManager::class); - } - /** * @param string $className * @return Logger @@ -31,14 +21,6 @@ public static function getLogger(string $className): Logger return GeneralUtility::makeInstance(LogManager::class)->getLogger($className); } - /** - * @return ContentObjectRenderer - */ - public static function getContentObject() - { - return self::getObjectManager()->get(ContentObjectRenderer::class); - } - /** * @return TypoScriptFrontendController * @SuppressWarnings(PHPMD.Superglobals) diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml new file mode 100644 index 0000000..b7bfdaa --- /dev/null +++ b/Configuration/Services.yaml @@ -0,0 +1,8 @@ +services: + _defaults: + autowire: true + autoconfigure: true + public: false + + In2code\Ipandlanguageredirect\: + resource: '../Classes/*' diff --git a/Configuration/TCA/Overrides/tt_content.php b/Configuration/TCA/Overrides/tt_content.php index 04dfbfc..adcaa9d 100644 --- a/Configuration/TCA/Overrides/tt_content.php +++ b/Configuration/TCA/Overrides/tt_content.php @@ -8,4 +8,14 @@ 'ipandlanguageredirect', 'Pi1', 'Ipandlanguageredirect' +); +\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin( + 'ipandlanguageredirect', + 'Test', + 'Ipandlanguageredirect' +); +\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin( + 'ipandlanguageredirect', + 'Redirect', + 'Ipandlanguageredirect' ); \ No newline at end of file diff --git a/Configuration/TypoScript/setup.typoscript b/Configuration/TypoScript/setup.typoscript index abbabaa..f6ba90f 100644 --- a/Configuration/TypoScript/setup.typoscript +++ b/Configuration/TypoScript/setup.typoscript @@ -12,7 +12,7 @@ plugin.tx_ipandlanguageredirect { ipToCountry { In2code\Ipandlanguageredirect\Domain\Service\IpToCountry\IpApi { # IpApi Key: Please enter your key for ipapi.co (optional), otherwise extension will have limited access to the service (less then 1000 visitors a day). See ipapi.co for details. - ipApiKey = + ipApiKey = } } } @@ -27,11 +27,8 @@ page { 5 { userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run extensionName = Ipandlanguageredirect - pluginName = Pi1 + pluginName = Suggest vendorName = In2code - controller = Redirect - action = suggest - switchableControllerActions.Redirect.1 = suggest } # Container for informations that will be send to an AJAX service 1555 = COA @@ -126,14 +123,10 @@ redirectAjax { extensionName = Ipandlanguageredirect pluginName = Pi1 vendorName = In2code - controller = Redirect - action = redirect - switchableControllerActions.Redirect.1 = redirect } } testAjax < redirectAjax testAjax { typeNum = 1556 - 10.action = test - 10.switchableControllerActions.Redirect.1 = test + 10.pluginName = Test } diff --git a/Resources/Private/Templates/Redirect/Suggest.html b/Resources/Private/Templates/Suggest/Suggest.html similarity index 100% rename from Resources/Private/Templates/Redirect/Suggest.html rename to Resources/Private/Templates/Suggest/Suggest.html diff --git a/ext_localconf.php b/ext_localconf.php index a035862..a0bda33 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -13,11 +13,31 @@ function () { 'Ipandlanguageredirect', 'Pi1', [ - \In2code\Ipandlanguageredirect\Controller\RedirectController::class => 'redirect,suggest' + \In2code\Ipandlanguageredirect\Controller\RedirectController::class => 'redirect' ], [ \In2code\Ipandlanguageredirect\Controller\RedirectController::class => 'redirect' ] ); + \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin( + 'Ipandlanguageredirect', + 'Suggest', + [ + \In2code\Ipandlanguageredirect\Controller\SuggestController::class => 'suggest' + ], + [ + \In2code\Ipandlanguageredirect\Controller\SuggestController::class => 'suggest' + ] + ); + \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin( + 'Ipandlanguageredirect', + 'Test', + [ + \In2code\Ipandlanguageredirect\Controller\TestController::class => 'test' + ], + [ + \In2code\Ipandlanguageredirect\Controller\TestController::class => 'test' + ] + ); } );