* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ abstract class BaseController extends BaseFrameworkSystem implements Registerable { // Load traits use ResolverTrait; // Exception constants const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10; // Names of controller's own filter chains const FILTER_CHAIN_PRE_COMMAND = 'controller_pre_command'; const FILTER_CHAIN_POST_COMMAND = 'controller_post_command'; const FILTER_CHAIN_SHUTDOWN = 'shutdown'; /** * Generic filter chains */ private $filterChains = []; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct (string $className) { // Call parent constructor //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: className=%s - CONSTRUCTED!', $className)); parent::__construct($className); // Initialize both filter chains //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('BASE-CONTROLLER: Initializing filter chains ...'); foreach([self::FILTER_CHAIN_PRE_COMMAND, self::FILTER_CHAIN_POST_COMMAND] as $filterChain) { // Init it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: Invoking this->initFilterChain(=%s) ...', $filterChain)); $this->initFilterChain($filterChain); } // Add this controller to the registry //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-CONTROLLER: Registering this=%s ...', $this->__toString())); ObjectRegistry::getRegistry('generic')->addInstance('controller', $this); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Executes a command with pre and post filters * * @param $requestInstance A Requestable class * @param $responseInstance A Responseable class * @return void */ public function executeGenericPrePostCommand (Requestable $requestInstance, Responseable $responseInstance) { // Get the command instance from the resolver by sending a request instance to the resolver //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: requestInstance=%s,responseInstance=%s - CALLED!', $requestInstance->__toString(), $responseInstance->__toString())); $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance); // Add more filters by the command $commandInstance->addExtraFilters($this, $requestInstance); // Run the pre filters $this->executePreFilters($requestInstance, $responseInstance); // This request was valid! :-D $requestInstance->setIsRequestValid(TRUE); // Execute the command $commandInstance->execute($requestInstance, $responseInstance); // Run the post filters $this->executePostFilters($requestInstance, $responseInstance); // Flush the response out $responseInstance->flushBuffer(); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Handles the given request and response, redirects to login_failed if * UserAuthorizationException is thrown. * * @param $requestInstance An instance of a Requestable class * @param $responseInstance An instance of a Responseable class * @return void */ public function genericHanleRequestLoginFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) { // Get the "form action" //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: requestInstance=%s,responseInstance=%s - CALLED!', $requestInstance->__toString(), $responseInstance->__toString())); $formAction = $requestInstance->getRequestElement('form'); // Get command instance from resolver $commandInstance = $this->getResolverInstance()->resolveCommand('Org\Mxchange\CoreFramework\Command\Failed', $formAction); // Add more filters by the command $commandInstance->addExtraFilters($this, $requestInstance); // Try to run the pre filters, if auth exceptions come through redirect here try { // Run the pre filters $this->executePreFilters($requestInstance, $responseInstance); } catch (UserAuthorizationException $e) { // Redirect to main page $responseInstance->redirectToConfiguredUrl('login_failed'); // Exit here exit; } /* * Is the request still valid? Post filters shall only be executed of * the request is valid */ if ($requestInstance->isRequestValid()) { // Execute the command $commandInstance->execute($requestInstance, $responseInstance); // Execute *very* generic post filters $this->executePostFilters($requestInstance, $responseInstance); } // Flush the buffer out $responseInstance->flushBuffer(); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Generic execute of the command: pre and post filters with redirect * but request becomes valid after pre-filters run. * * @param $requestInstance An instance of a Requestable class * @param $responseInstance An instance of a Responseable class * @return void */ public function genericHanleRequestLoginAreaFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) { // Get the command instance from the resolver by sending a request instance to the resolver //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: requestInstance=%s,responseInstance=%s - CALLED!', $requestInstance->__toString(), $responseInstance->__toString())); $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance); // Add more filters by the command $commandInstance->addExtraFilters($this, $requestInstance); // Try to run the pre filters, if auth exceptions come through redirect here try { // Run the pre filters $this->executePreFilters($requestInstance, $responseInstance); } catch (UserAuthorizationException $e) { // Redirect to main page $responseInstance->redirectToConfiguredUrl('login_failed'); // Exit here exit; } // This request was valid! :-D $requestInstance->setIsRequestValid(TRUE); // Execute the command $commandInstance->execute($requestInstance, $responseInstance); // Run the post filters $this->executePostFilters($requestInstance, $responseInstance); // Flush the response out $responseInstance->flushBuffer(); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Private method to initialize a given filter chain * * @param $filterChain Name of the filter chain * @return void * @throws InvalidArgumentException If a parameter has an invalid value * @throws BadMethodCallException If the given filter chain is already initialized */ protected function initFilterChain (string $filterChain) { // Check parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: filterChain=%s - CALLED!', $filterChain)); if (empty($filterChain)) { // Throw IAE throw new InvalidArgumentException('Parameter "filterChain" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (isset($this->filterChains[$filterChain])) { // Throw BMCE throw new BadMethodCallException(sprintf('filterChain=%s is already initialized', $filterChain), FrameworkInterface::EXCEPTION_BAD_METHOD_CALL); } // Initialize filter chain //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-CONTROLLER: Initializing filterChain=%s ...', $filterChain)); $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class'); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Adds a filter to a given filter chain * * @param $filterChain Chain of the filter * @param $filterInstance An instance of a filter * @return void * @throws InvalidArgumentException If a parameter has an invalid value * @throws BadMethodCallException If the given filter chain is not yet initialized */ protected function addFilter (string $filterChain, Filterable $filterInstance) { // Check parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: filterChain=%s,filterInstance=%s - CALLED!', $filterChain, $filterInstance->__toString())); if (empty($filterChain)) { // Throw IAE throw new InvalidArgumentException('Parameter "filterChain" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (!isset($this->filterChains[$filterChain])) { // Throw IAE throw new BadMethodCallException(sprintf('filterChain=%s is not a valid chain', $filterChain), FrameworkInterface::EXCEPTION_BAD_METHOD_CALL); } // Add the filter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-CONTROLLER: Adding filterInstance=%s to filterChain=%s ...', $filterInstance->__toString(), $filterChain)); $this->filterChains[$filterChain]->addFilter($filterInstance); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Adds a filter to the pre filter chain * * @param $filterInstance An instance of a filter * @return void */ public function addPreFilter (Filterable $filterInstance) { // Add the pre filter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: filterInstance=%s - CALLED!', $filterInstance->__toString())); $this->addFilter(self::FILTER_CHAIN_PRE_COMMAND, $filterInstance); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Adds a filter to the post filter chain * * @param $filterInstance An instance of a filter * @return void */ public function addPostFilter (Filterable $filterInstance) { // Add post filter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: filterInstance=%s - CALLED!', $filterInstance->__toString())); $this->addFilter(self::FILTER_CHAIN_POST_COMMAND, $filterInstance); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Add a shutdown filter * * @param $filterInstance A Filterable class * @return void */ public function addShutdownFilter (Filterable $filterInstance) { // Add shutdown filter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: filterInstance=%s - CALLED!', $filterInstance->__toString())); $this->addFilter(self::FILTER_CHAIN_SHUTDOWN, $filterInstance); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Executes given filter chain chain * * @param $filterChain Chain of the filter to execute * @param $requestInstance An instance of a Requestable class * @param $responseInstance An instance of a Responseable class * @return void * @throws InvalidArgumentException If the filter chain is invalid */ protected function executeFilters (string $filterChain, Requestable $requestInstance, Responseable $responseInstance) { // Check parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: filterChain=%s,requestInstance=%s,responseInstance=%s - CALLED!', $filterChain, $requestInstance->__toString(), $responseInstance->__toString())); if (empty($filterChain)) { // Throw IAE throw new InvalidArgumentException('Parameter "filterChain" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (!isset($this->filterChains[$filterChain])) { // Throw an exception here throw new BadMethodCallException(sprintf('filterChain=%s is not a valid chain', $filterChain), FrameworkInterface::EXCEPTION_BAD_METHOD_CALL); } // Run all filters //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-CONTROLLER: Processing filterChain=%s...', $filterChain)); $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Executes all pre filters * * @param $requestInstance An instance of a Requestable class * @param $responseInstance An instance of a Responseable class * @return void */ protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) { // Execute all pre filters //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: requestInstance=%s,responseInstance=%s - CALLED!', $requestInstance->__toString(), $responseInstance->__toString())); $this->executeFilters(self::FILTER_CHAIN_PRE_COMMAND, $requestInstance, $responseInstance); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Executes all post filters * * @param $requestInstance An instance of a Requestable class * @param $responseInstance An instance of a Responseable class * @return void */ protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) { // Execute all post filters //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: requestInstance=%s,responseInstance=%s - CALLED!', $requestInstance->__toString(), $responseInstance->__toString())); $this->executeFilters(self::FILTER_CHAIN_POST_COMMAND, $requestInstance, $responseInstance); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } /** * Executes all shutdown filters * * @param $requestInstance A Requestable class * @param $responseInstance A Responseable class * @return void */ public function executeShutdownFilters (Requestable $requestInstance, Responseable $responseInstance) { // Execute all shutdown filter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-CONTROLLER: requestInstance=%s,responseInstance=%s - CALLED!', $requestInstance->__toString(), $responseInstance->__toString())); $this->executeFilters(self::FILTER_CHAIN_SHUTDOWN, $requestInstance, $responseInstance); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-CONTROLLER: EXIT!'); } }