-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
040d5f8
commit 7414075
Showing
2 changed files
with
185 additions
and
2 deletions.
There are no files selected for viewing
183 changes: 183 additions & 0 deletions
183
source/Internal/Framework/Controller/AbstractController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © OXID eSales AG. All rights reserved. | ||
* See LICENSE file for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OxidEsales\EshopCommunity\Internal\Framework\Controller; | ||
|
||
use OxidEsales\Eshop\Core\Registry; | ||
use OxidEsales\Eshop\Core\ViewConfig; | ||
use OxidEsales\EshopCommunity\Core\Di\ContainerFacade; | ||
use OxidEsales\EshopCommunity\Internal\Transition\ShopEvents\AfterRequestProcessedEvent; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
abstract class AbstractController implements ControllerInterface | ||
{ | ||
/** | ||
* Array of data that is passed to template engine - array( "varName" => "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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters