From f56bfc22f4741bead33e622db8f62eff7e9eacc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sun, 26 Jun 2022 12:30:22 +0200 Subject: [PATCH] chore: Bootstrap refactoring. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../api/Controllers/ControllerBase.php | 3 +- .../web/Controllers/ControllerBase.php | 3 ++ lib/SP/Core/Bootstrap/BootstrapApi.php | 29 +++++++------------ lib/SP/Core/Bootstrap/BootstrapBase.php | 22 +++++++------- lib/SP/Core/Bootstrap/BootstrapWeb.php | 19 ++++-------- 5 files changed, 31 insertions(+), 45 deletions(-) diff --git a/app/modules/api/Controllers/ControllerBase.php b/app/modules/api/Controllers/ControllerBase.php index 7965b5ea..e743b662 100644 --- a/app/modules/api/Controllers/ControllerBase.php +++ b/app/modules/api/Controllers/ControllerBase.php @@ -29,6 +29,7 @@ use Klein\Klein; use League\Fractal\Manager; use SP\Core\Acl\Acl; use SP\Core\Application; +use SP\Core\Bootstrap\BootstrapBase; use SP\Core\Context\ContextInterface; use SP\Core\Events\EventDispatcher; use SP\Core\Exceptions\SPException; @@ -73,7 +74,7 @@ abstract class ControllerBase $this->fractal = new Manager(); $this->controllerName = $this->getControllerName(); - $this->actionName = $this->context->getTrasientKey('_actionName'); + $this->actionName = $this->context->getTrasientKey(BootstrapBase::CONTEXT_ACTION_NAME); } final protected function getControllerName(): string diff --git a/app/modules/web/Controllers/ControllerBase.php b/app/modules/web/Controllers/ControllerBase.php index c2ab374e..386bbb26 100644 --- a/app/modules/web/Controllers/ControllerBase.php +++ b/app/modules/web/Controllers/ControllerBase.php @@ -29,6 +29,7 @@ defined('APP_ROOT') || die(); use Exception; use SP\Core\Acl\Acl; use SP\Core\Application; +use SP\Core\Bootstrap\BootstrapBase; use SP\Core\Context\ContextInterface; use SP\Core\Crypt\Hash; use SP\Core\Events\EventDispatcher; @@ -72,6 +73,7 @@ abstract class ControllerBase protected bool $isAjax; protected LayoutHelper $layoutHelper; private BrowserAuthInterface $browser; + protected string $actionName; public function __construct( Application $application, @@ -93,6 +95,7 @@ abstract class ControllerBase $this->view->setBase($this->getViewBaseName()); $this->isAjax = $this->request->isAjax(); + $this->actionName = $this->session->getTrasientKey(BootstrapBase::CONTEXT_ACTION_NAME); $loggedIn = $this->session->isLoggedIn(); diff --git a/lib/SP/Core/Bootstrap/BootstrapApi.php b/lib/SP/Core/Bootstrap/BootstrapApi.php index 32adc84f..07f43e99 100644 --- a/lib/SP/Core/Bootstrap/BootstrapApi.php +++ b/lib/SP/Core/Bootstrap/BootstrapApi.php @@ -43,11 +43,6 @@ final class BootstrapApi extends BootstrapBase protected HttpModuleBase $module; - /** - * @param \Psr\Container\ContainerInterface $container - * - * @return \SP\Core\Bootstrap\BootstrapApi - */ public static function run(ContainerInterface $container): BootstrapApi { logger('------------'); @@ -69,37 +64,31 @@ final class BootstrapApi extends BootstrapBase protected function configureRouter(): void { - // Manage requests for api module - $this->router->respond( - 'POST', - '@/api\.php', - $this->manageApiRequest() - ); + $this->router->respond('POST', '@/api\.php', $this->manageApiRequest()); } private function manageApiRequest(): Closure { return function ($request, $response, $service) { + /** @var \Klein\Request $request */ + /** @var \Klein\Response $response */ + try { logger('API route'); $apiRequest = $this->createObjectFor(ApiRequest::class); - [$controllerName, $action] = explode('/', $apiRequest->getMethod()); + [$controllerName, $actionName] = explode('/', $apiRequest->getMethod()); - $controllerClass = self::getClassFor($controllerName, $action); + $controllerClass = self::getClassFor($controllerName, $actionName); - $method = $action.'Action'; + $method = $actionName.'Action'; if (!method_exists($controllerClass, $method)) { logger($controllerClass.'::'.$method); /** @var Response $response */ - $response->headers() - ->set( - 'Content-type', - 'application/json; charset=utf-8' - ); + $response->headers()->set('Content-type', 'application/json; charset=utf-8'); return $response->body( JsonRpcResponse::getResponseError( @@ -110,6 +99,8 @@ final class BootstrapApi extends BootstrapBase ); } + $this->context->setTrasientKey(self::CONTEXT_ACTION_NAME, $actionName); + $this->initializeCommon(); $this->module->initialize($controllerName); diff --git a/lib/SP/Core/Bootstrap/BootstrapBase.php b/lib/SP/Core/Bootstrap/BootstrapBase.php index 3812cf00..6b647572 100644 --- a/lib/SP/Core/Bootstrap/BootstrapBase.php +++ b/lib/SP/Core/Bootstrap/BootstrapBase.php @@ -32,6 +32,7 @@ use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; use RuntimeException; +use SP\Core\Context\ContextInterface; use SP\Core\Exceptions\InitializationException; use SP\Core\Exceptions\SPException; use SP\Core\Language; @@ -53,6 +54,8 @@ defined('APP_ROOT') || die(); */ abstract class BootstrapBase { + public const CONTEXT_ACTION_NAME = "_actionName"; + protected const OOPS_MESSAGE = "Oops, it looks like this content does not exist..."; /** * @var string The current request path relative to the sysPass root (e.g. files/index.php) @@ -71,6 +74,7 @@ abstract class BootstrapBase protected Klein $router; protected RequestInterface $request; protected ConfigDataInterface $configData; + protected ContextInterface $context; private ContainerInterface $container; private UpgradeConfigChecker $upgradeConfigChecker; private PhpExtensionChecker $phpExtensionChecker; @@ -84,6 +88,7 @@ abstract class BootstrapBase RequestInterface $request, UpgradeConfigChecker $upgradeConfigChecker, PhpExtensionChecker $extensionChecker, + ContextInterface $context, ContainerInterface $container ) { // Set the default language @@ -94,6 +99,7 @@ abstract class BootstrapBase $this->request = $request; $this->upgradeConfigChecker = $upgradeConfigChecker; $this->phpExtensionChecker = $extensionChecker; + $this->context = $context; $this->container = $container; $this->initRouter(); @@ -152,21 +158,13 @@ abstract class BootstrapBase return null; } - final protected static function getClassFor(string $controllerName, ?string $actionName): string + final protected static function getClassFor(string $controllerName, string $actionName): string { - if ($actionName !== null) { - return sprintf( - 'SP\Modules\%s\Controllers\%s\%sController', - ucfirst(APP_MODULE), - ucfirst($controllerName), - ucfirst($actionName) - ); - } - return sprintf( - 'SP\Modules\%s\Controllers\%sController', + 'SP\Modules\%s\Controllers\%s\%sController', ucfirst(APP_MODULE), - ucfirst($controllerName) + ucfirst($controllerName), + ucfirst($actionName) ); } diff --git a/lib/SP/Core/Bootstrap/BootstrapWeb.php b/lib/SP/Core/Bootstrap/BootstrapWeb.php index 4d2596d1..aa1d5afb 100644 --- a/lib/SP/Core/Bootstrap/BootstrapWeb.php +++ b/lib/SP/Core/Bootstrap/BootstrapWeb.php @@ -41,15 +41,11 @@ use SP\Util\Filter; */ final class BootstrapWeb extends BootstrapBase { + private const ROUTE_REGEX = /** @lang RegExp */ + '#(?P[a-zA-Z]+)(?:/(?P[a-zA-Z]+))?(?P(/[a-zA-Z\d.]+)+)?#'; + protected HttpModuleBase $module; - /** - * @param \Psr\Container\ContainerInterface $container - * - * @return \SP\Core\Bootstrap\BootstrapWeb - * - * TODO: Inject needed classes - */ public static function run(ContainerInterface $container): BootstrapWeb { logger('------------'); @@ -72,7 +68,6 @@ final class BootstrapWeb extends BootstrapBase protected function configureRouter(): void { - // Manage requests for web module $this->router->respond(['GET', 'POST'], '@(?!/api\.php)', $this->manageWebRequest()); } @@ -88,11 +83,7 @@ final class BootstrapWeb extends BootstrapBase /** @var \Klein\Request $request */ $route = Filter::getString($request->param('r', 'index/index')); - if (!preg_match_all( - '#(?P[a-zA-Z]+)(?:/(?P[a-zA-Z]+))?(?P(/[a-zA-Z\d.]+)+)?#', - $route, - $matches - )) { + if (!preg_match_all(self::ROUTE_REGEX, $route, $matches)) { throw new RuntimeException(self::OOPS_MESSAGE); } @@ -115,6 +106,8 @@ final class BootstrapWeb extends BootstrapBase throw new RuntimeException(self::OOPS_MESSAGE); } + $this->context->setTrasientKey(self::CONTEXT_ACTION_NAME, $actionName); + $this->setCors($response); $this->initializeCommon();