Merge pull request #19695 from MauricioFauth/sql-construct-di
Replace Sql class construction with DI when possible
This commit is contained in:
commit
5c939c0c46
@ -313,13 +313,10 @@ return [
|
||||
'class' => Database\Structure\EmptyTableController::class,
|
||||
'arguments' => [
|
||||
'$response' => '@response',
|
||||
'$template' => '@template',
|
||||
'$dbi' => '@dbi',
|
||||
'$relation' => '@relation',
|
||||
'$relationCleanup' => '@relation_cleanup',
|
||||
'$flashMessenger' => '@' . FlashMessenger::class,
|
||||
'$structureController' => '@' . Database\StructureController::class,
|
||||
'$config' => '@config',
|
||||
'$sql' => '@sql',
|
||||
],
|
||||
],
|
||||
Database\Structure\FavoriteTableController::class => [
|
||||
@ -999,12 +996,7 @@ return [
|
||||
],
|
||||
Table\DeleteRowsController::class => [
|
||||
'class' => Table\DeleteRowsController::class,
|
||||
'arguments' => [
|
||||
'$response' => '@response',
|
||||
'$template' => '@template',
|
||||
'$dbi' => '@dbi',
|
||||
'$config' => '@config',
|
||||
],
|
||||
'arguments' => ['$response' => '@response', '$dbi' => '@dbi', '$sql' => '@sql'],
|
||||
],
|
||||
Table\DropColumnConfirmationController::class => [
|
||||
'class' => Table\DropColumnConfirmationController::class,
|
||||
@ -1194,6 +1186,7 @@ return [
|
||||
'$dbi' => '@dbi',
|
||||
'$dbTableExists' => '@' . DbTableExists::class,
|
||||
'$config' => '@config',
|
||||
'$sql' => '@sql',
|
||||
],
|
||||
],
|
||||
Table\SqlController::class => [
|
||||
|
||||
@ -4,10 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Controllers\Database\Structure;
|
||||
|
||||
use PhpMyAdmin\Bookmarks\BookmarkRepository;
|
||||
use PhpMyAdmin\Config;
|
||||
use PhpMyAdmin\ConfigStorage\Relation;
|
||||
use PhpMyAdmin\ConfigStorage\RelationCleanup;
|
||||
use PhpMyAdmin\Controllers\Database\StructureController;
|
||||
use PhpMyAdmin\Controllers\InvocableController;
|
||||
use PhpMyAdmin\Current;
|
||||
@ -19,8 +15,6 @@ use PhpMyAdmin\Message;
|
||||
use PhpMyAdmin\ResponseRenderer;
|
||||
use PhpMyAdmin\Sql;
|
||||
use PhpMyAdmin\Table\Table;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Transformations;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
|
||||
@ -30,13 +24,10 @@ final readonly class EmptyTableController implements InvocableController
|
||||
{
|
||||
public function __construct(
|
||||
private ResponseRenderer $response,
|
||||
private Template $template,
|
||||
private DatabaseInterface $dbi,
|
||||
private Relation $relation,
|
||||
private RelationCleanup $relationCleanup,
|
||||
private FlashMessenger $flashMessenger,
|
||||
private StructureController $structureController,
|
||||
private Config $config,
|
||||
private Sql $sql,
|
||||
) {
|
||||
}
|
||||
|
||||
@ -72,17 +63,11 @@ final readonly class EmptyTableController implements InvocableController
|
||||
}
|
||||
|
||||
if (! empty($_REQUEST['pos'])) {
|
||||
$sql = new Sql(
|
||||
$this->dbi,
|
||||
$this->relation,
|
||||
$this->relationCleanup,
|
||||
new Transformations(),
|
||||
$this->template,
|
||||
new BookmarkRepository($this->dbi, $this->relation),
|
||||
$this->config,
|
||||
$_REQUEST['pos'] = $this->sql->calculatePosForLastPage(
|
||||
Current::$database,
|
||||
Current::$table,
|
||||
$_REQUEST['pos'],
|
||||
);
|
||||
|
||||
$_REQUEST['pos'] = $sql->calculatePosForLastPage(Current::$database, Current::$table, $_REQUEST['pos']);
|
||||
}
|
||||
|
||||
ForeignKey::handleDisableCheckCleanup($defaultFkCheckValue);
|
||||
|
||||
@ -4,10 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Controllers\Table;
|
||||
|
||||
use PhpMyAdmin\Bookmarks\BookmarkRepository;
|
||||
use PhpMyAdmin\Config;
|
||||
use PhpMyAdmin\ConfigStorage\Relation;
|
||||
use PhpMyAdmin\ConfigStorage\RelationCleanup;
|
||||
use PhpMyAdmin\Controllers\InvocableController;
|
||||
use PhpMyAdmin\Current;
|
||||
use PhpMyAdmin\Dbal\DatabaseInterface;
|
||||
@ -15,8 +11,6 @@ use PhpMyAdmin\Http\Response;
|
||||
use PhpMyAdmin\Http\ServerRequest;
|
||||
use PhpMyAdmin\ResponseRenderer;
|
||||
use PhpMyAdmin\Sql;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Transformations;
|
||||
use PhpMyAdmin\UrlParams;
|
||||
use PhpMyAdmin\Util;
|
||||
use PhpMyAdmin\Utils\ForeignKey;
|
||||
@ -26,12 +20,8 @@ use function sprintf;
|
||||
|
||||
final readonly class DeleteRowsController implements InvocableController
|
||||
{
|
||||
public function __construct(
|
||||
private ResponseRenderer $response,
|
||||
private Template $template,
|
||||
private DatabaseInterface $dbi,
|
||||
private Config $config,
|
||||
) {
|
||||
public function __construct(private ResponseRenderer $response, private DatabaseInterface $dbi, private Sql $sql)
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequest $request): Response
|
||||
@ -39,17 +29,6 @@ final readonly class DeleteRowsController implements InvocableController
|
||||
$multBtn = $_POST['mult_btn'] ?? '';
|
||||
$selected = $_POST['selected'] ?? [];
|
||||
|
||||
$relation = new Relation($this->dbi);
|
||||
$sql = new Sql(
|
||||
$this->dbi,
|
||||
$relation,
|
||||
new RelationCleanup($this->dbi, $relation),
|
||||
new Transformations(),
|
||||
$this->template,
|
||||
new BookmarkRepository($this->dbi, $relation),
|
||||
$this->config,
|
||||
);
|
||||
|
||||
if ($multBtn === __('Yes')) {
|
||||
$defaultFkCheckValue = ForeignKey::handleDisableCheckInit();
|
||||
Current::$sqlQuery = '';
|
||||
@ -67,7 +46,11 @@ final readonly class DeleteRowsController implements InvocableController
|
||||
}
|
||||
|
||||
if (! empty($_REQUEST['pos'])) {
|
||||
$_REQUEST['pos'] = $sql->calculatePosForLastPage(Current::$database, Current::$table, $_REQUEST['pos']);
|
||||
$_REQUEST['pos'] = $this->sql->calculatePosForLastPage(
|
||||
Current::$database,
|
||||
Current::$table,
|
||||
$_REQUEST['pos'],
|
||||
);
|
||||
}
|
||||
|
||||
ForeignKey::handleDisableCheckCleanup($defaultFkCheckValue);
|
||||
@ -80,7 +63,7 @@ final readonly class DeleteRowsController implements InvocableController
|
||||
Current::$sqlQuery = $request->getParsedBodyParamAsString('original_sql_query', '');
|
||||
}
|
||||
|
||||
$this->response->addHTML($sql->executeQueryAndSendQueryResponse(
|
||||
$this->response->addHTML($this->sql->executeQueryAndSendQueryResponse(
|
||||
null,
|
||||
false,
|
||||
Current::$database,
|
||||
|
||||
@ -4,10 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Controllers\Table;
|
||||
|
||||
use PhpMyAdmin\Bookmarks\BookmarkRepository;
|
||||
use PhpMyAdmin\Config;
|
||||
use PhpMyAdmin\ConfigStorage\Relation;
|
||||
use PhpMyAdmin\ConfigStorage\RelationCleanup;
|
||||
use PhpMyAdmin\Controllers\InvocableController;
|
||||
use PhpMyAdmin\Current;
|
||||
use PhpMyAdmin\Dbal\DatabaseInterface;
|
||||
@ -21,7 +19,6 @@ use PhpMyAdmin\ResponseRenderer;
|
||||
use PhpMyAdmin\Sql;
|
||||
use PhpMyAdmin\Table\Search;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Transformations;
|
||||
use PhpMyAdmin\Url;
|
||||
use PhpMyAdmin\UrlParams;
|
||||
use PhpMyAdmin\Util;
|
||||
@ -96,6 +93,7 @@ final class SearchController implements InvocableController
|
||||
private readonly DatabaseInterface $dbi,
|
||||
private readonly DbTableExists $dbTableExists,
|
||||
private readonly Config $config,
|
||||
private readonly Sql $sql,
|
||||
) {
|
||||
}
|
||||
|
||||
@ -237,17 +235,7 @@ final class SearchController implements InvocableController
|
||||
/**
|
||||
* Add this to ensure following procedures included running correctly.
|
||||
*/
|
||||
$sql = new Sql(
|
||||
$this->dbi,
|
||||
$this->relation,
|
||||
new RelationCleanup($this->dbi, $this->relation),
|
||||
new Transformations(),
|
||||
$this->template,
|
||||
new BookmarkRepository($this->dbi, $this->relation),
|
||||
$this->config,
|
||||
);
|
||||
|
||||
$this->response->addHTML($sql->executeQueryAndSendQueryResponse(
|
||||
$this->response->addHTML($this->sql->executeQueryAndSendQueryResponse(
|
||||
null,
|
||||
false, // is_gotofile
|
||||
Current::$database, // db
|
||||
|
||||
10
src/Sql.php
10
src/Sql.php
@ -57,11 +57,11 @@ class Sql
|
||||
public static Message|null $usingBookmarkMessage = null;
|
||||
|
||||
public function __construct(
|
||||
private DatabaseInterface $dbi,
|
||||
private Relation $relation,
|
||||
private RelationCleanup $relationCleanup,
|
||||
private Transformations $transformations,
|
||||
private Template $template,
|
||||
private readonly DatabaseInterface $dbi,
|
||||
private readonly Relation $relation,
|
||||
private readonly RelationCleanup $relationCleanup,
|
||||
private readonly Transformations $transformations,
|
||||
private readonly Template $template,
|
||||
private readonly BookmarkRepository $bookmarkRepository,
|
||||
private readonly Config $config,
|
||||
) {
|
||||
|
||||
@ -4,14 +4,19 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Tests\Controllers\Table;
|
||||
|
||||
use PhpMyAdmin\Bookmarks\BookmarkRepository;
|
||||
use PhpMyAdmin\Config;
|
||||
use PhpMyAdmin\ConfigStorage\Relation;
|
||||
use PhpMyAdmin\ConfigStorage\RelationCleanup;
|
||||
use PhpMyAdmin\Controllers\Table\DeleteRowsController;
|
||||
use PhpMyAdmin\Current;
|
||||
use PhpMyAdmin\Dbal\DatabaseInterface;
|
||||
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
|
||||
use PhpMyAdmin\Sql;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Tests\AbstractTestCase;
|
||||
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
|
||||
use PhpMyAdmin\Transformations;
|
||||
use PhpMyAdmin\UrlParams;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
|
||||
@ -56,8 +61,19 @@ class DeleteRowsControllerTest extends AbstractTestCase
|
||||
$request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/')
|
||||
->withParsedBody(['original_sql_query' => 'SELECT * FROM `test_db`.`test_table`']);
|
||||
|
||||
$relation = new Relation($dbi, $config);
|
||||
$sql = new Sql(
|
||||
$dbi,
|
||||
$relation,
|
||||
new RelationCleanup($dbi, $relation),
|
||||
new Transformations(),
|
||||
new Template($config),
|
||||
new BookmarkRepository($dbi, $relation),
|
||||
$config,
|
||||
);
|
||||
|
||||
$response = new ResponseRenderer();
|
||||
(new DeleteRowsController($response, new Template(), $dbi, $config))($request);
|
||||
(new DeleteRowsController($response, $dbi, $sql))($request);
|
||||
$actual = $response->getHTMLResult();
|
||||
self::assertStringContainsString(
|
||||
'<div class="alert alert-success border-top-0 border-start-0 border-end-0 rounded-bottom-0 mb-0"'
|
||||
|
||||
@ -4,17 +4,21 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Tests\Controllers\Table;
|
||||
|
||||
use PhpMyAdmin\Bookmarks\BookmarkRepository;
|
||||
use PhpMyAdmin\Config;
|
||||
use PhpMyAdmin\ConfigStorage\Relation;
|
||||
use PhpMyAdmin\ConfigStorage\RelationCleanup;
|
||||
use PhpMyAdmin\Controllers\Table\SearchController;
|
||||
use PhpMyAdmin\Current;
|
||||
use PhpMyAdmin\Dbal\DatabaseInterface;
|
||||
use PhpMyAdmin\DbTableExists;
|
||||
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
|
||||
use PhpMyAdmin\Sql;
|
||||
use PhpMyAdmin\Table\Search;
|
||||
use PhpMyAdmin\Template;
|
||||
use PhpMyAdmin\Tests\AbstractTestCase;
|
||||
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
|
||||
use PhpMyAdmin\Transformations;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
|
||||
#[CoversClass(SearchController::class)]
|
||||
@ -40,15 +44,29 @@ final class SearchControllerTest extends AbstractTestCase
|
||||
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com')
|
||||
->withParsedBody(['db' => 'test_db', 'table' => 'test_table']);
|
||||
|
||||
$config = new Config();
|
||||
$relation = new Relation($dbi, $config);
|
||||
$template = new Template($config);
|
||||
$sql = new Sql(
|
||||
$dbi,
|
||||
$relation,
|
||||
new RelationCleanup($dbi, $relation),
|
||||
new Transformations(),
|
||||
$template,
|
||||
new BookmarkRepository($dbi, $relation),
|
||||
$config,
|
||||
);
|
||||
|
||||
$responseRenderer = new ResponseRenderer();
|
||||
$controller = new SearchController(
|
||||
$responseRenderer,
|
||||
new Template(),
|
||||
$template,
|
||||
new Search($dbi),
|
||||
new Relation($dbi),
|
||||
$relation,
|
||||
$dbi,
|
||||
new DbTableExists($dbi),
|
||||
new Config(),
|
||||
$config,
|
||||
$sql,
|
||||
);
|
||||
$controller($request);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user