chore: Bootstrap refactoring.

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2022-06-26 12:30:22 +02:00
parent 55d54a6518
commit f56bfc22f4
5 changed files with 31 additions and 45 deletions

View File

@@ -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

View File

@@ -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();

View File

@@ -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);

View File

@@ -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)
);
}

View File

@@ -41,15 +41,11 @@ use SP\Util\Filter;
*/
final class BootstrapWeb extends BootstrapBase
{
private const ROUTE_REGEX = /** @lang RegExp */
'#(?P<controller>[a-zA-Z]+)(?:/(?P<actions>[a-zA-Z]+))?(?P<params>(/[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<controller>[a-zA-Z]+)(?:/(?P<actions>[a-zA-Z]+))?(?P<params>(/[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();