diff --git a/source/Internal/Framework/Controller/AbstractController.php b/source/Internal/Framework/Controller/AbstractController.php new file mode 100644 index 0000000000..1886027636 --- /dev/null +++ b/source/Internal/Framework/Controller/AbstractController.php @@ -0,0 +1,183 @@ + "varValue"). + */ + protected ?array $_aViewData = []; + + protected ?array $_aViewParams = []; + + protected string $_sThisAction; + + protected $_sThisTemplate; + + protected string $_sClass; + + protected string $classKey; + + protected string $_sFnc; + + protected static bool $_blExecuted = false; + + protected ?string $_sCharSet; + + protected bool $_blIsCallForCache = false; + + protected ?ViewConfig $_oViewConf; + + public function init(): void + { + $this->_sThisAction = strtolower(get_class($this)); + } + + public function setViewParameters($aParams = null): void + { + $this->_aViewParams = $aParams; + } + + public function getViewParameter($sKey) + { + return $this->_aViewParams[$sKey] ?? Registry::getRequest()->getRequestEscapedParameter($sKey); + } + + public function getIsCallForCache(): bool + { + return $this->_blIsCallForCache; + } + + public function getViewId() + { + } + + /** + * Entry point to pass controller-specific data to the view. + * @return string current view template file name + */ + public function render(): string + { + return $this->getTemplateName(); + } + + public function getTemplateName() + { + return $this->_sThisTemplate; + } + + public function getViewConfig(): ViewConfig + { + if ($this->_oViewConf === null) { + $this->_oViewConf = oxNew(ViewConfig::class); + } + + return $this->_oViewConf; + } + + public function setClassKey($classKey): void + { + $this->_sClass = $classKey; + $this->classKey = $classKey; + } + + public function getClassKey(): string + { + return $this->classKey; + } + + public function setFncName($fncName): void + { + $this->_sFnc = $fncName; + } + + public function getFncName(): string + { + return $this->_sFnc; + } + + public function setViewData($viewData = null): void + { + $this->_aViewData = $viewData; + } + + public function getViewData(): array + { + return $this->_aViewData ?? []; + } + + public function executeFunction($function): void + { + if ($function && !self::$_blExecuted) { + if (method_exists($this, $function)) { + $sNewAction = $this->$function(); + self::$_blExecuted = true; + ContainerFacade::get(EventDispatcherInterface::class) + ->dispatch(new AfterRequestProcessedEvent()); + + if (isset($sNewAction)) { + $this->executeNewAction($sNewAction); + } + } else { + throw new \OxidEsales\Eshop\Core\Exception\RoutingException( + sprintf("Controller method is not accessible: %s::%s", self::class, $function) + ); + } + } + } + + /** + * Formats header for new controller action + * + * Input example: "view_name?param1=val1¶m2=val2" => "cl=view_name¶m1=val1¶m2=val2" + * + * @param string $newAction new action params + */ + protected function executeNewAction(string $newAction): void + { + if (empty($newAction)) { + return; + } + + $myConfig = Registry::getConfig(); + [$className, $pageParams] = explode('?', $newAction) + [null, null]; + $classNameParts = explode('/', $className); + $resolvedClassName = Registry::getControllerClassNameResolver()->getClassNameById($classNameParts[0]); + $realClassName = $resolvedClassName ? Registry::getUtilsObject()->getClassName($resolvedClassName) : Registry::getUtilsObject()->getClassName($classNameParts[0]); + + if (!class_exists($realClassName)) { + throw new \RuntimeException('Controller class "' . $realClassName . '" does not exist'); + } + + $session = Registry::getSession(); + $header = "cl={$classNameParts[0]}&" . ($pageParams ? "$pageParams&" : '') . $session->sid(); + $url = $myConfig->getCurrentShopUrl() . "index.php?$header"; + $url = Registry::getUtilsUrl()->processUrl($url); + + if (Registry::getUtils()->seoIsActive() && $seoUrl = Registry::getSeoEncoder()->getStaticUrl($url)) { + $url = $seoUrl; + } + + ContainerFacade::dispatch(new AfterRequestProcessedEvent()); + Registry::getUtils()->redirect($url, (bool)Registry::getRequest()->getRequestEscapedParameter('redirected'), 302); + } + + public function getCharSet(): string + { + return $this->_sCharSet ??= Registry::getLang()->translateString('charset'); + } +} diff --git a/tests/Integration/Internal/Framework/Module/Controller/Fixtures/module1/src/Controller/ModuleControllerAsService.php b/tests/Integration/Internal/Framework/Module/Controller/Fixtures/module1/src/Controller/ModuleControllerAsService.php index 251b24d8aa..649ff7c3d8 100644 --- a/tests/Integration/Internal/Framework/Module/Controller/Fixtures/module1/src/Controller/ModuleControllerAsService.php +++ b/tests/Integration/Internal/Framework/Module/Controller/Fixtures/module1/src/Controller/ModuleControllerAsService.php @@ -9,9 +9,9 @@ namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Framework\Module\Controller\Fixtures\module1\src\Controller; -use OxidEsales\EshopCommunity\Core\Controller\BaseController; +use OxidEsales\EshopCommunity\Internal\Framework\Controller\AbstractController; -class ModuleControllerAsService extends BaseController +class ModuleControllerAsService extends AbstractController { protected $_sThisTemplate = '@module1/module_controller_as_service';