Skip to content

Commit

Permalink
Feature/detect module name and better exception handling (#326)
Browse files Browse the repository at this point in the history
* Get module name from request
* Improve exception handling in frontend controller
  • Loading branch information
digedag authored Oct 22, 2023
1 parent 977beb1 commit 2faf7bc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Changelog
---------

v1.18.0 (??.10.2023)

* Auto-detect module identifier to avoid boilerblade-code
* BC: Parameter type of exceptions changed in ExceptionHandlerInterface to \Throwable

v1.17.4 (22.09.2023)

* fix autoloading for T3 8.x non-composer-mode
Expand Down
16 changes: 16 additions & 0 deletions Classes/Backend/Module/BaseScriptClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ abstract class BaseScriptClass
*/
protected $pageRenderer;

protected $moduleName;

/**
* Constructor deprecates the class.
*/
Expand All @@ -225,6 +227,16 @@ public function init()

$this->id = (int) GeneralUtility::_GP('id');
$this->CMD = GeneralUtility::_GP('CMD');
$this->moduleName = GeneralUtility::_GP('M');
// Das sollte zur Initialisierung des Modules ausreichend sein.
// Das access Level sollte nicht vorgegeben werden.
if (!isset($this->MCONF['name']) && $this->moduleName) {
$this->MCONF = array_merge((array) $GLOBALS['MCONF'], [
'name' => $this->moduleName,
// 'access' => 'user,group',
]);
}

$this->perms_clause = $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW);
$this->menuConfig();
$this->handleExternalFunctionValue();
Expand All @@ -248,6 +260,10 @@ public function menuConfig()
unset($this->MOD_MENU['function'][$key]);
}
}
if (empty($this->MOD_MENU)) {
exit('Backend module is not initialized. No module functions found in MOD_MENU.');
}

$this->MOD_SETTINGS = BackendUtility::getModuleData($this->MOD_MENU, GeneralUtility::_GP('SET'), $this->MCONF['name'], $this->modMenu_type, $this->modMenu_dontValidateList, $this->modMenu_setDefaultList);
}

Expand Down
7 changes: 4 additions & 3 deletions Classes/Exception/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sys25\RnBase\Configuration\Processor;
use Sys25\RnBase\Utility\Logger;
use Sys25\RnBase\Utility\Misc;
use Throwable;

/***************************************************************
* Copyright notice
Expand Down Expand Up @@ -38,13 +39,13 @@ class ExceptionHandler implements ExceptionHandlerInterface
/**
* Interne Verarbeitung der Exception.
*
* @param string $actionName
* @param Exception $e
* @param string $actionName
* @param Throwable $e
* @param ConfigurationInterface $configurations
*
* @return string error message
*/
public function handleException($actionName, Exception $e, ConfigurationInterface $configurations)
public function handleException($actionName, Throwable $e, ConfigurationInterface $configurations)
{
// wir prüfen erst mal, ob die exception gefangen werden soll
$catch = $this->catchException($actionName, $e, $configurations);
Expand Down
6 changes: 3 additions & 3 deletions Classes/Exception/ExceptionHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Sys25\RnBase\Exception;

use Exception;
use Sys25\RnBase\Configuration\ConfigurationInterface;
use Throwable;

/***************************************************************
* Copyright notice
Expand Down Expand Up @@ -34,10 +34,10 @@ interface ExceptionHandlerInterface
* Interne Verarbeitung der Exception.
*
* @param string $actionName
* @param Exception $e
* @param Throwable $e
* @param ConfigurationInterface $configurations
*
* @return string error message
*/
public function handleException($actionName, Exception $e, ConfigurationInterface $configurations);
public function handleException($actionName, Throwable $e, ConfigurationInterface $configurations);
}
52 changes: 28 additions & 24 deletions Legacy/class.tx_rnbase_controller.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

use Sys25\RnBase\Configuration\ConfigurationInterface;
use Sys25\RnBase\Configuration\Processor;
use Sys25\RnBase\Exception\ExceptionHandler;
use Sys25\RnBase\Exception\ExceptionHandlerInterface;
use Sys25\RnBase\Exception\PageNotFound404;
use Sys25\RnBase\Exception\SkipActionException;
use Sys25\RnBase\Frontend\Request\Parameters;
use Sys25\RnBase\Utility\Arrays;
use Sys25\RnBase\Utility\Logger;
use Sys25\RnBase\Utility\Misc;
use Sys25\RnBase\Utility\Strings;
use Sys25\RnBase\Utility\TYPO3;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand Down Expand Up @@ -105,13 +108,13 @@
* Used by: none
*
* @author René Nitzsche ([email protected])
* @package TYPO3
* @subpackage rn_base
*
* FIXME: move to PSR-4
*/

class tx_rnbase_controller
{
public $configurationsClassName = 'Tx_Rnbase_Configuration_Processor'; // You may overwrite this in your subclass with an own configurations class.
public $configurationsClassName = Processor::class; // You may overwrite this in your subclass with an own configurations class.

public $parameters;

Expand Down Expand Up @@ -205,12 +208,13 @@ public function setContentObjectRenderer(ContentObjectRenderer $cObj): void

public function main($out, $configurationArray)
{
tx_rnbase_util_Misc::pushTT('tx_rnbase_controller', 'start');
Misc::pushTT('tx_rnbase_controller', 'start');

// Making the configurations object
tx_rnbase_util_Misc::pushTT('init configuration', '');
Misc::pushTT('init configuration', '');
/** @var $configurations Processor */
$configurations = $this->_makeConfigurationsObject($configurationArray);
tx_rnbase_util_Misc::pullTT();
Misc::pullTT();

try {
// check for doConvertToUserIntObject
Expand All @@ -223,13 +227,13 @@ public function main($out, $configurationArray)
return '';
}

tx_rnbase_util_Misc::enableTimeTrack($configurations->get('_enableTT') ? true : false);
Misc::enableTimeTrack($configurations->get('_enableTT') ? true : false);
// Making the parameters object
tx_rnbase_util_Misc::pushTT('init parameters', '');
Misc::pushTT('init parameters', '');
$parameters = $this->_makeParameterObject($configurations);
// Make sure to keep all parameters
$configurations->setParameters($parameters);
tx_rnbase_util_Misc::pullTT();
Misc::pullTT();

// Finding the action:
$actions = $this->_findAction($parameters, $configurations);
Expand All @@ -243,15 +247,15 @@ public function main($out, $configurationArray)

try {
foreach ($actions as $actionName) {
tx_rnbase_util_Misc::pushTT('call action', $actionName);
Misc::pushTT('call action', $actionName);
$out .= $this->doAction($actionName, $parameters, $configurations);
tx_rnbase_util_Misc::pullTT();
Misc::pullTT();
}
} catch (SkipActionException $e) {
// Bei USER_INT im ersten Aufruf die Ausgabe unterdrücken
$out = '';
}
tx_rnbase_util_Misc::pullTT();
Misc::pullTT();

return $out;
}
Expand Down Expand Up @@ -289,10 +293,10 @@ public function doAction($actionName, &$parameters, &$configurations)
count($message) > 1 ? $message[0] : $e->getMessage(),
count($message) > 1 ? $message[1] : ''
);
} // Nice to have, aber weder aufwärts noch abwärtskompatibel...
catch (\TYPO3\CMS\Core\Error\Http\PageNotFoundException $e) {
} catch (\TYPO3\CMS\Core\Error\Http\PageNotFoundException $e) {
// Nice to have, aber weder aufwärts noch abwärtskompatibel...
$this->handlePageNotFound('TYPO3\\CMS\\Core\\Error\\Http\\PageNotFoundException was thrown');
} catch (Exception $e) {
} catch (Throwable $e) {
$ret = $this->handleException($actionName, $e, $configurations);
$this->errors[] = $e;
}
Expand All @@ -302,11 +306,11 @@ public function doAction($actionName, &$parameters, &$configurations)

protected function handlePageNotFound(string $reason, string $header = ''): void
{
if (!\Sys25\RnBase\Utility\TYPO3::isTYPO104OrHigher()) {
\Sys25\RnBase\Utility\TYPO3::getTSFE()->pageNotFoundAndExit($reason, $header);
if (!TYPO3::isTYPO104OrHigher()) {
TYPO3::getTSFE()->pageNotFoundAndExit($reason, $header);
}

$response = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Controller\ErrorController::class)
$response = tx_rnbase::makeInstance(\TYPO3\CMS\Frontend\Controller\ErrorController::class)
->pageNotFoundAction(
$GLOBALS['TYPO3_REQUEST'],
$reason
Expand All @@ -327,12 +331,12 @@ public function getErrors()
/**
* Interne Verarbeitung der Exception.
*
* @param Exception $e
* @param Tx_Rnbase_Configuration_ProcessorInterface $configurations
* @param Exception $e
* @param ConfigurationInterface $configurations
*/
private function handleException($actionName, Exception $e, $configurations)
private function handleException($actionName, Throwable $e, ConfigurationInterface $configurations)
{
$exceptionHandlerClass = \Sys25\RnBase\Configuration\Processor::getExtensionCfgValue(
$exceptionHandlerClass = Processor::getExtensionCfgValue(
'rn_base',
'exceptionHandler'
);
Expand Down Expand Up @@ -403,10 +407,10 @@ protected function _findAction($parameters, $configurations)

// Falls es mehrere Actions sind den String splitten
if ($action) {
$action = tx_rnbase_util_Strings::trimExplode(',', $action);
$action = Strings::trimExplode(',', $action);
}
if (is_array($action) && 1 == count($action)) {
$action = tx_rnbase_util_Strings::trimExplode('|', $action[0]); // Nochmal mit Pipe versuchen
$action = Strings::trimExplode('|', $action[0]); // Nochmal mit Pipe versuchen
}
// If there is still no action we use defined defaultAction
$action = !$action ? $configurations->get('defaultAction') : $action;
Expand Down
2 changes: 1 addition & 1 deletion tests/class.tx_rnbase_tests_controller_testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public function handleException($actionName, Exception $e, Tx_Rnbase_Configurati

class tx_rnbase_exception_CustomHandler implements \Sys25\RnBase\Exception\ExceptionHandlerInterface
{
public function handleException($actionName, Exception $e, Sys25\RnBase\Configuration\ConfigurationInterface $configurations)
public function handleException($actionName, Throwable $e, Sys25\RnBase\Configuration\ConfigurationInterface $configurations)
{
return 'custom handler';
}
Expand Down

0 comments on commit 2faf7bc

Please sign in to comment.