Skip to content

Commit

Permalink
Merge pull request #19159 from MauricioFauth/response-renderer-disabl…
Browse files Browse the repository at this point in the history
…e-removal

Remove the ResponseRenderer::disable() method
  • Loading branch information
MauricioFauth committed May 14, 2024
2 parents 797fe78 + 578ed90 commit 4c98f9b
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 205 deletions.
2 changes: 1 addition & 1 deletion app/services_controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@
],
Export\ExportController::class => [
'class' => Export\ExportController::class,
'arguments' => ['$response' => '@response', '$export' => '@export'],
'arguments' => ['@response', '@export', '@' . ResponseFactory::class],
],
Export\TablesController::class => [
'class' => Export\TablesController::class,
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7215,11 +7215,6 @@ parameters:
count: 1
path: src/Export/Export.php

-
message: "#^Method PhpMyAdmin\\\\Export\\\\Export\\:\\:getMemoryLimit\\(\\) should return int but returns \\(float\\|int\\)\\.$#"
count: 1
path: src/Export/Export.php

-
message: "#^Only booleans are allowed in a negated boolean, int\\|false given\\.$#"
count: 1
Expand Down
15 changes: 1 addition & 14 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,7 @@
<code><![CDATA[$whatStrucOrData]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidCast>
<code><![CDATA[$this->export->dumpBuffer]]></code>
<code><![CDATA[$this->export->dumpBuffer]]></code>
<code><![CDATA[$whatStrucOrData]]></code>
<code><![CDATA[$whatStrucOrData]]></code>
Expand Down Expand Up @@ -1844,13 +1845,6 @@
<code><![CDATA[$GLOBALS['export_type']]]></code>
<code><![CDATA[$GLOBALS['export_type']]]></code>
</PossiblyNullArgument>
<PossiblyUnusedReturnValue>
<code><![CDATA[Response|null]]></code>
</PossiblyUnusedReturnValue>
<RedundantCondition>
<code><![CDATA[! $GLOBALS['save_on_server']]]></code>
<code><![CDATA[$outputFormat === 'sendit' && ! $GLOBALS['save_on_server']]]></code>
</RedundantCondition>
<RiskyTruthyFalsyComparison>
<code><![CDATA[$asSeparateFiles]]></code>
<code><![CDATA[$onServerParam]]></code>
Expand Down Expand Up @@ -5737,12 +5731,6 @@
<code><![CDATA[$GLOBALS['time_start']]]></code>
<code><![CDATA[$GLOBALS['xkana']]]></code>
</InvalidArrayOffset>
<InvalidReturnStatement>
<code><![CDATA[$memoryLimit]]></code>
</InvalidReturnStatement>
<InvalidReturnType>
<code><![CDATA[int]]></code>
</InvalidReturnType>
<MixedArgument>
<code><![CDATA[$GLOBALS['xkana'] ?? '']]></code>
<code><![CDATA[$dbAlias]]></code>
Expand Down Expand Up @@ -13673,7 +13661,6 @@
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<InaccessibleProperty>
<code><![CDATA[$config->config->debug->sql]]></code>
Expand Down
20 changes: 7 additions & 13 deletions src/Controllers/Export/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpMyAdmin\Encoding;
use PhpMyAdmin\Exceptions\ExportException;
use PhpMyAdmin\Export\Export;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Identifiers\DatabaseName;
Expand All @@ -39,8 +40,11 @@

final class ExportController implements InvocableController
{
public function __construct(private readonly ResponseRenderer $response, private readonly Export $export)
{
public function __construct(
private readonly ResponseRenderer $response,
private readonly Export $export,
private readonly ResponseFactory $responseFactory,
) {
}

public function __invoke(ServerRequest $request): Response|null
Expand Down Expand Up @@ -164,14 +168,6 @@ public function __invoke(ServerRequest $request): Response|null
}
}

/**
* If we are sending the export file (as opposed to just displaying it
* as text), we have to bypass the usual PhpMyAdmin\Response mechanism
*/
if ($outputFormat === 'sendit' && ! $GLOBALS['save_on_server']) {
$this->response->disable();
}

$tableNames = [];
// Generate error url and check for needed variables
if ($GLOBALS['export_type'] === 'server') {
Expand Down Expand Up @@ -546,9 +542,7 @@ public function __invoke(ServerRequest $request): Response|null
return null;
}

echo $this->export->dumpBuffer;

return null;
return $this->responseFactory->createResponse()->write($this->export->dumpBuffer);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Export/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function getMemoryLimit(): int
// Some memory is needed for compression, assume 1/3
$memoryLimit /= 8;

return $memoryLimit;
return (int) $memoryLimit;
}

/**
Expand Down
16 changes: 0 additions & 16 deletions src/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ class Footer
* or also include scripts, errors and links
*/
private bool $isMinimal = false;
/**
* Whether to display anything
*/
private bool $isEnabled = true;

public function __construct(private readonly Template $template, private readonly Config $config)
{
Expand Down Expand Up @@ -155,14 +151,6 @@ public function getErrorMessages(): string
return $retval;
}

/**
* Disables the rendering of the footer
*/
public function disable(): void
{
$this->isEnabled = false;
}

/**
* Set the ajax flag to indicate whether
* we are servicing an ajax request
Expand Down Expand Up @@ -197,10 +185,6 @@ public function getScripts(): Scripts
*/
public function getDisplay(): string
{
if (! $this->isEnabled) {
return '';
}

if (! $this->isAjax && ! $this->isMinimal) {
if (Core::getEnv('SCRIPT_NAME') !== '') {
$url = $this->getSelfUrl();
Expand Down
14 changes: 1 addition & 13 deletions src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ class Header
* Whether we are servicing an ajax request.
*/
private bool $isAjax = false;
/**
* Whether to display anything
*/
private bool $isEnabled = true;
/**
* Whether the HTTP headers (and possibly some HTML)
* have already been sent to the browser
Expand Down Expand Up @@ -163,14 +159,6 @@ public function getJsParamsCode(): string
return 'window.Navigation.update(window.CommonParams.setAll(' . json_encode($params, JSON_HEX_TAG) . '));';
}

/**
* Disables the rendering of the header
*/
public function disable(): void
{
$this->isEnabled = false;
}

/**
* Set the ajax flag to indicate whether
* we are servicing an ajax request
Expand Down Expand Up @@ -247,7 +235,7 @@ public function disableWarnings(): void
*/
public function getDisplay(): string
{
if ($this->headerIsSent || ! $this->isEnabled || $this->isAjax) {
if ($this->headerIsSent || $this->isAjax) {

Check warning on line 238 in src/Header.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ */ public function getDisplay(): string { - if ($this->headerIsSent || $this->isAjax) { + if ($this->headerIsSent && $this->isAjax) { return ''; } $this->sendHttpHeaders();
return '';
}

Expand Down
9 changes: 6 additions & 3 deletions src/Plugins/Auth/AuthenticationCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

namespace PhpMyAdmin\Plugins\Auth;

use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\Core;
use PhpMyAdmin\Current;
use PhpMyAdmin\Error\ErrorHandler;
use PhpMyAdmin\Exceptions\AuthenticationFailure;
use PhpMyAdmin\Exceptions\SessionHandlerException;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\LanguageManager;
use PhpMyAdmin\Message;
Expand Down Expand Up @@ -478,10 +480,11 @@ public function rememberCredentials(): Response|null
Util::clearUserCache();

$responseRenderer = ResponseRenderer::getInstance();
$responseRenderer->disable();
$responseRenderer->redirect('./index.php?route=/' . Url::getCommonRaw($urlParams, '&'));

return $responseRenderer->response();
return ResponseFactory::create()->createResponse(StatusCodeInterface::STATUS_FOUND)->withHeader(
'Location',
$responseRenderer->fixRelativeUrlForRedirect('./index.php?route=/' . Url::getCommonRaw($urlParams, '&')),
);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Plugins/Auth/AuthenticationSignon.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

namespace PhpMyAdmin\Plugins\Auth;

use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\Exceptions\AuthenticationFailure;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\LanguageManager;
use PhpMyAdmin\Plugins\AuthenticationPlugin;
Expand Down Expand Up @@ -40,7 +42,6 @@ class AuthenticationSignon extends AuthenticationPlugin
public function showLoginForm(): Response
{
$responseRenderer = ResponseRenderer::getInstance();
$responseRenderer->disable();
unset($_SESSION['LAST_SIGNON_URL']);
$config = Config::getInstance();
if (empty($config->selectedServer['SignonURL'])) {
Expand All @@ -53,9 +54,10 @@ public function showLoginForm(): Response
return $responseRenderer->response();
}

$responseRenderer->redirect($config->selectedServer['SignonURL']);

return $responseRenderer->response();
return ResponseFactory::create()->createResponse(StatusCodeInterface::STATUS_FOUND)->withHeader(
'Location',
$responseRenderer->fixRelativeUrlForRedirect($config->selectedServer['SignonURL']),
);
}

/**
Expand Down
20 changes: 0 additions & 20 deletions src/ResponseRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ class ResponseRenderer
* Whether we are servicing an ajax request.
*/
protected bool $isAjax = false;
/**
* Whether response object is disabled
*/
protected bool $isDisabled = false;
/**
* Whether there were any errors during the processing of the request
* Only used for ajax responses
Expand Down Expand Up @@ -205,17 +201,6 @@ public function isAjax(): bool
return $this->isAjax;
}

/**
* Disables the rendering of the header
* and the footer in responses
*/
public function disable(): void
{
$this->header->disable();
$this->footer->disable();
$this->isDisabled = true;
}

/**
* Returns a PhpMyAdmin\Header object
*/
Expand Down Expand Up @@ -273,11 +258,6 @@ private function getDisplay(): string
*/
private function ajaxResponse(): string
{
/* Avoid wrapping in case we're disabled */
if ($this->isDisabled) {
return $this->getDisplay();
}

if (! isset($this->JSON['message'])) {
$this->JSON['message'] = $this->getDisplay();
} elseif ($this->JSON['message'] instanceof Message) {
Expand Down

0 comments on commit 4c98f9b

Please sign in to comment.