Skip to content

Commit

Permalink
Move check if setup is enabled to each setup controller
Browse files Browse the repository at this point in the history
Removes it from the Setup\MainController as it will be removed in the
future.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed May 11, 2024
1 parent 0a681ea commit 126ac3a
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 42 deletions.
6 changes: 3 additions & 3 deletions app/services_controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -869,15 +869,15 @@
],
Setup\MainController::class => [
'class' => Setup\MainController::class,
'arguments' => ['@' . ResponseFactory::class, '@response', '@template'],
'arguments' => ['@' . ResponseFactory::class, '@response', '@template', '@config'],
],
Setup\ShowConfigController::class => [
'class' => Setup\ShowConfigController::class,
'arguments' => ['@response'],
'arguments' => ['@' . ResponseFactory::class, '@response', '@template', '@config'],
],
Setup\ValidateController::class => [
'class' => Setup\ValidateController::class,
'arguments' => ['@' . ResponseFactory::class],
'arguments' => ['@' . ResponseFactory::class, '@template', '@config'],
],
Sql\ColumnPreferencesController::class => [
'class' => Sql\ColumnPreferencesController::class,
Expand Down
32 changes: 31 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3655,11 +3655,26 @@ parameters:
count: 1
path: src/Controllers/Server/Variables/SetVariableController.php

-
message: "#^Parameter \\#1 \\$filename of function file_exists expects string, mixed given\\.$#"
count: 1
path: src/Controllers/Setup/ConfigController.php

-
message: "#^Parameter \\#1 \\$filename of function file_exists expects string, mixed given\\.$#"
count: 1
path: src/Controllers/Setup/FormController.php

-
message: "#^Only booleans are allowed in a ternary operator condition, mixed given\\.$#"
count: 1
path: src/Controllers/Setup/HomeController.php

-
message: "#^Parameter \\#1 \\$filename of function file_exists expects string, mixed given\\.$#"
count: 1
path: src/Controllers/Setup/HomeController.php

-
message: "#^Parameter \\#1 \\$id of method PhpMyAdmin\\\\Config\\\\ConfigFile\\:\\:getServerName\\(\\) expects int, \\(int\\|string\\) given\\.$#"
count: 1
Expand All @@ -3673,7 +3688,22 @@ parameters:
-
message: "#^Parameter \\#1 \\$filename of function file_exists expects string, mixed given\\.$#"
count: 1
path: src/Controllers/Setup/MainController.php
path: src/Controllers/Setup/ServerDestroyController.php

-
message: "#^Parameter \\#1 \\$filename of function file_exists expects string, mixed given\\.$#"
count: 1
path: src/Controllers/Setup/ServersController.php

-
message: "#^Parameter \\#1 \\$filename of function file_exists expects string, mixed given\\.$#"
count: 1
path: src/Controllers/Setup/ShowConfigController.php

-
message: "#^Parameter \\#1 \\$filename of function file_exists expects string, mixed given\\.$#"
count: 1
path: src/Controllers/Setup/ValidateController.php

-
message: "#^Cannot cast mixed to string\\.$#"
Expand Down
8 changes: 1 addition & 7 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3000,21 +3000,15 @@
</PossiblyUnusedReturnValue>
</file>
<file src="src/Controllers/Setup/HomeController.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<MixedArgumentTypeCoercion>
<code><![CDATA[$id]]></code>
<code><![CDATA[$id]]></code>
</MixedArgumentTypeCoercion>
<RiskyTruthyFalsyComparison>
<code><![CDATA[Config::getInstance()->get('PMA_IS_WINDOWS')]]></code>
<code><![CDATA[$this->config->get('PMA_IS_WINDOWS')]]></code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/Controllers/Setup/MainController.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<PossiblyUnusedMethod>
<code><![CDATA[__construct]]></code>
</PossiblyUnusedMethod>
Expand Down
18 changes: 18 additions & 0 deletions src/Controllers/Setup/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,46 @@

namespace PhpMyAdmin\Controllers\Setup;

use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\Controllers\InvocableController;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\LanguageManager;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Setup\ConfigGenerator;
use PhpMyAdmin\Setup\SetupHelper;
use PhpMyAdmin\Template;

use function __;
use function file_exists;
use function is_string;

use const CONFIG_FILE;

final class ConfigController implements InvocableController
{
public function __construct(
private readonly ResponseFactory $responseFactory,
private readonly ResponseRenderer $responseRenderer,
private readonly Template $template,
private readonly Config $config,
) {
}

public function __invoke(ServerRequest $request): Response
{
if (@file_exists(CONFIG_FILE) && ! $this->config->config->debug->demo) {
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_NOT_FOUND);

return $response->write($this->template->render('error/generic', [
'lang' => $GLOBALS['lang'] ?? 'en',
'dir' => LanguageManager::$textDir,
'error_message' => __('Configuration already exists, setup is disabled!'),
]));
}

$response = $this->responseFactory->createResponse();
foreach ($this->responseRenderer->getHeader()->getHttpHeaders() as $name => $value) {
$response = $response->withHeader($name, $value);
Expand Down
16 changes: 16 additions & 0 deletions src/Controllers/Setup/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PhpMyAdmin\Controllers\Setup;

use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\Config\Forms\Setup\SetupFormList;
use PhpMyAdmin\Controllers\InvocableController;
use PhpMyAdmin\Http\Factory\ResponseFactory;
Expand All @@ -16,21 +18,35 @@
use PhpMyAdmin\Template;

use function __;
use function file_exists;
use function is_string;
use function ob_get_clean;
use function ob_start;

use const CONFIG_FILE;

final class FormController implements InvocableController
{
public function __construct(
private readonly ResponseFactory $responseFactory,
private readonly ResponseRenderer $responseRenderer,
private readonly Template $template,
private readonly Config $config,
) {
}

public function __invoke(ServerRequest $request): Response
{
if (@file_exists(CONFIG_FILE) && ! $this->config->config->debug->demo) {
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_NOT_FOUND);

return $response->write($this->template->render('error/generic', [
'lang' => $GLOBALS['lang'] ?? 'en',
'dir' => LanguageManager::$textDir,
'error_message' => __('Configuration already exists, setup is disabled!'),
]));
}

$response = $this->responseFactory->createResponse();
foreach ($this->responseRenderer->getHeader()->getHttpHeaders() as $name => $value) {
$response = $response->withHeader($name, $value);
Expand Down
17 changes: 16 additions & 1 deletion src/Controllers/Setup/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpMyAdmin\Controllers\Setup;

use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\Config\ServerConfigChecks;
use PhpMyAdmin\Controllers\InvocableController;
Expand All @@ -18,20 +19,34 @@

use function __;
use function array_keys;
use function file_exists;
use function is_scalar;
use function is_string;

use const CONFIG_FILE;

final class HomeController implements InvocableController
{
public function __construct(
private readonly ResponseFactory $responseFactory,
private readonly ResponseRenderer $responseRenderer,
private readonly Template $template,
private readonly Config $config,
) {
}

public function __invoke(ServerRequest $request): Response
{
if (@file_exists(CONFIG_FILE) && ! $this->config->config->debug->demo) {
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_NOT_FOUND);

return $response->write($this->template->render('error/generic', [
'lang' => $GLOBALS['lang'] ?? 'en',
'dir' => LanguageManager::$textDir,
'error_message' => __('Configuration already exists, setup is disabled!'),
]));
}

$response = $this->responseFactory->createResponse();
foreach ($this->responseRenderer->getHeader()->getHttpHeaders() as $name => $value) {
$response = $response->withHeader($name, $value);
Expand Down Expand Up @@ -109,7 +124,7 @@ public function __invoke(ServerRequest $request): Response
'has_check_page_refresh' => $hasCheckPageRefresh,
'eol' => isset($_SESSION['eol']) && is_scalar($_SESSION['eol'])
? $_SESSION['eol']
: (Config::getInstance()->get('PMA_IS_WINDOWS') ? 'win' : 'unix'),
: ($this->config->get('PMA_IS_WINDOWS') ? 'win' : 'unix'),
]));
}

Expand Down
64 changes: 38 additions & 26 deletions src/Controllers/Setup/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,76 @@

namespace PhpMyAdmin\Controllers\Setup;

use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\Controllers\InvocableController;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\LanguageManager;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;

use function __;
use function file_exists;
use function in_array;

use const CONFIG_FILE;

final class MainController implements InvocableController
{
public function __construct(
private readonly ResponseFactory $responseFactory,
private readonly ResponseRenderer $responseRenderer,
private readonly Template $template,
private readonly Config $config,
) {
}

public function __invoke(ServerRequest $request): Response
{
$config = Config::getInstance();
if (@file_exists(CONFIG_FILE) && ! $config->config->debug->demo) {
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_NOT_FOUND);

return $response->write($this->template->render('error/generic', [
'lang' => $GLOBALS['lang'] ?? 'en',
'dir' => LanguageManager::$textDir,
'error_message' => __('Configuration already exists, setup is disabled!'),
]));
}

/** @var mixed $pageParam */
$pageParam = $request->getQueryParam('page');
$page = in_array($pageParam, ['form', 'config', 'servers'], true) ? $pageParam : 'index';

$page = $this->getPageParam($request->getQueryParam('page'));
if ($page === 'form') {
return (new FormController($this->responseFactory, $this->responseRenderer, $this->template))($request);
return (new FormController(
$this->responseFactory,
$this->responseRenderer,
$this->template,
$this->config,
))($request);
}

if ($page === 'config') {
return (new ConfigController($this->responseFactory, $this->responseRenderer, $this->template))($request);
return (new ConfigController(
$this->responseFactory,
$this->responseRenderer,
$this->template,
$this->config,
))($request);
}

if ($page === 'servers' && $request->getQueryParam('mode') === 'remove' && $request->isPost()) {
return (new ServerDestroyController($this->responseFactory, $this->responseRenderer))($request);
return (new ServerDestroyController(
$this->responseFactory,
$this->responseRenderer,
$this->template,
$this->config,
))($request);
}

if ($page === 'servers') {
return (new ServersController($this->responseFactory, $this->responseRenderer, $this->template))($request);
return (new ServersController(
$this->responseFactory,
$this->responseRenderer,
$this->template,
$this->config,
))($request);
}

return (new HomeController($this->responseFactory, $this->responseRenderer, $this->template))($request);
return (new HomeController(
$this->responseFactory,
$this->responseRenderer,
$this->template,
$this->config,
))($request);
}

/** @psalm-return 'form'|'config'|'servers'|'index' */
private function getPageParam(mixed $pageParam): string
{
return in_array($pageParam, ['form', 'config', 'servers'], true) ? $pageParam : 'index';
}
}
19 changes: 19 additions & 0 deletions src/Controllers/Setup/ServerDestroyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,45 @@
namespace PhpMyAdmin\Controllers\Setup;

use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\Controllers\InvocableController;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\LanguageManager;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Setup\SetupHelper;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;

use function __;
use function file_exists;
use function is_numeric;

use const CONFIG_FILE;

final class ServerDestroyController implements InvocableController
{
public function __construct(
private readonly ResponseFactory $responseFactory,
private readonly ResponseRenderer $responseRenderer,
private readonly Template $template,
private readonly Config $config,
) {
}

public function __invoke(ServerRequest $request): Response
{
if (@file_exists(CONFIG_FILE) && ! $this->config->config->debug->demo) {
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_NOT_FOUND);

return $response->write($this->template->render('error/generic', [
'lang' => $GLOBALS['lang'] ?? 'en',
'dir' => LanguageManager::$textDir,
'error_message' => __('Configuration already exists, setup is disabled!'),
]));
}

$response = $this->responseFactory->createResponse();
foreach ($this->responseRenderer->getHeader()->getHttpHeaders() as $name => $value) {
$response = $response->withHeader($name, $value);
Expand Down

0 comments on commit 126ac3a

Please sign in to comment.