Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
godefroy-le-hardi committed Jan 9, 2025
1 parent 040d5f8 commit 7414075
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 2 deletions.
183 changes: 183 additions & 0 deletions source/Internal/Framework/Controller/AbstractController.php
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&param2=val2" => "cl=view_name&param1=val1&param2=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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit 7414075

Please sign in to comment.