N°2060 [WIP] Initialisation of the portal application: Refactor way brick controllers forward actions between each others

This commit is contained in:
Molkobain
2019-07-11 17:44:55 +02:00
parent ee45e546a8
commit cd6fe171cd
4 changed files with 36 additions and 30 deletions

View File

@@ -33,5 +33,32 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
*/
abstract class AbstractController extends Controller
{
/**
* @param string $sRouteName
* @param array $aRouteParams
* @param array $aQueryParameters
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function ForwardFromRoute($sRouteName, $aRouteParams, $aQueryParameters)
{
return $this->forward($this->GetControllerNameFromRoute($sRouteName), $aRouteParams, $aQueryParameters);
}
/**
* Returns a string containing the controller and action name of a specific route, typically used for request forwarding.
*
* Example: 'p_object_create' returns 'Combodo\iTop\Portal\Controller\ObjectController::CreateAction'
*
* @param string $sRouteName
*
* @return string
*/
protected function GetControllerNameFromRoute($sRouteName)
{
$oRouteCollection = $this->get('router')->getRouteCollection();
$aRouteDefaults = $oRouteCollection->get($sRouteName)->getDefaults();
return $aRouteDefaults['_controller'];
}
}

View File

@@ -32,7 +32,6 @@ use Symfony\Component\HttpFoundation\Request;
* @package Combodo\iTop\Portal\Controller
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @since 2.3.0
* @method GetControllerNameFromRoute(string $string)
*/
class CreateBrickController extends BrickController
{
@@ -82,7 +81,7 @@ class CreateBrickController extends BrickController
// - Adding brick id to the params
$aRouteParams['sBrickId'] = $sBrickId;
$oResponse = $this->forward($this->GetControllerNameFromRoute('p_object_create'), $aRouteParams, $oRequest->query->all());
$oResponse = $this->ForwardFromRoute('p_object_create', $aRouteParams, $oRequest->query->all());
}
// - Else, we list the leaf classes as an intermediate step
else

View File

@@ -23,7 +23,6 @@
namespace Combodo\iTop\Portal\Controller;
use Combodo\iTop\Portal\Brick\BrickCollection;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -61,17 +60,15 @@ class DefaultController extends AbstractController
500);
}
$sControllerName = $aControllerActionParts[0];
$sControllerAction = $aControllerActionParts[1];
$oController = new $sControllerName();
if (!$oController instanceof ContainerAwareInterface)
$aRouteParams = array();
// Add sBrickId in the route params as it is necessary for each brick actions
if (is_a($aControllerActionParts[0], BrickController::class, true))
{
return new Response('Tile controller must be implement ContainerAwareInterface for brick "'.$oBrick->GetId().'"', 500);
$aRouteParams['sBrickId'] = $oBrick->GetId();
}
$oController->setContainer($this->container);
/** @var Response $oResponse */
$oResponse = $oController->$sControllerAction($oRequest, $oBrick->GetId());
/** @var \Symfony\Component\HttpFoundation\Response $oResponse */
$oResponse = $this->forward($oBrick->GetTileControllerAction(), $aRouteParams, $oRequest->query->all());
$aData['aTilesRendering'][$oBrick->GetId()] = $oResponse->getContent();
}
}

View File

@@ -382,7 +382,7 @@ class ObjectController extends BrickController
'sObjectClass' => get_class($oTargetObject),
);
return $this->forward($this->GetControllerNameFromRoute('p_object_create'), $aRouteParams, $oRequest->query->all());
return $this->ForwardFromRoute('p_object_create', $aRouteParams, $oRequest->query->all());
}
/**
@@ -1390,21 +1390,4 @@ class ObjectController extends BrickController
return $aObjectData;
}
/**
* Returns a string containing the controller and action name of a specific route, typically used for request forwarding.
*
* Example: 'p_object_create' returns 'Combodo\iTop\Portal\Controller\ObjectController::CreateAction'
*
* @param string $sRouteName
*
* @return string
*/
private function GetControllerNameFromRoute($sRouteName)
{
$oRouteCollection = $this->get('router')->getRouteCollection();
$aRouteDefaults = $oRouteCollection->get($sRouteName)->getDefaults();
return $aRouteDefaults['_controller'];
}
}