From a982be66455bca1c5b39bb3ba38257fb3335d255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Tue, 2 May 2023 10:36:04 +0200 Subject: [PATCH 1/4] remove unnecessary "pass by reference" variables from Request --- src/Request/Request.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Request/Request.php b/src/Request/Request.php index 0887e2e..535f4f0 100644 --- a/src/Request/Request.php +++ b/src/Request/Request.php @@ -23,15 +23,15 @@ class Request implements IRequest public function __construct( string $base, - array &$get, - array &$post, + array $get, + array $post, array $headers, array &$session, IUserRepository $userRepository) { $this->base = $base; - $this->get = &$get; - $this->post = &$post; + $this->get = $get; + $this->post = $post; $this->headers = $headers; $this->session = new Session($session); @@ -41,9 +41,9 @@ class Request implements IRequest } } - public function setParsedRouteParams(array &$routeParams): void + public function setParsedRouteParams(array $routeParams): void { - $this->routeParams = &$routeParams; + $this->routeParams = $routeParams; } public function getBase(): string -- 2.45.2 From 9ade08d8bd9d9889b71cfcad650669c58fb8c5ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Tue, 2 May 2023 10:52:22 +0200 Subject: [PATCH 2/4] session handler should receive table name in the constructor --- src/Session/DatabaseSessionHandler.php | 13 ++++++++----- templates/web.php.tpl | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Session/DatabaseSessionHandler.php b/src/Session/DatabaseSessionHandler.php index 45cab2d..45f0e45 100644 --- a/src/Session/DatabaseSessionHandler.php +++ b/src/Session/DatabaseSessionHandler.php @@ -11,13 +11,16 @@ class DatabaseSessionHandler implements ISessionHandler { private IConnection $dbConnection; + private string $table; + private bool $exists = false; private bool $written = false; - public function __construct(IConnection $dbConnection) + public function __construct(IConnection $dbConnection, string $table) { $this->dbConnection = $dbConnection; + $this->table = $table; } public function open($savePath, $sessionName): bool @@ -32,7 +35,7 @@ class DatabaseSessionHandler implements ISessionHandler public function read($id): string { - $select = new Select($this->dbConnection, 'sessions'); + $select = new Select($this->dbConnection, $this->table); $select->columns(['data']); $select->whereId(substr($id, 0, 32)); @@ -49,7 +52,7 @@ class DatabaseSessionHandler implements ISessionHandler public function write($id, $data): bool { - $modify = new Modify($this->dbConnection, 'sessions'); + $modify = new Modify($this->dbConnection, $this->table); if ($this->exists) { $modify->setId(substr($id, 0, 32)); @@ -68,7 +71,7 @@ class DatabaseSessionHandler implements ISessionHandler public function destroy($id): bool { - $modify = new Modify($this->dbConnection, 'sessions'); + $modify = new Modify($this->dbConnection, $this->table); $modify->setId(substr($id, 0, 32)); $modify->delete(); @@ -101,7 +104,7 @@ class DatabaseSessionHandler implements ISessionHandler return true; } - $modify = new Modify($this->dbConnection, 'sessions'); + $modify = new Modify($this->dbConnection, $this->table); $modify->setId(substr($id, 0, 32)); $modify->set('updated', (new DateTime())->format('Y-m-d H:i:s')); diff --git a/templates/web.php.tpl b/templates/web.php.tpl index 47fe1f7..4b503a6 100644 --- a/templates/web.php.tpl +++ b/templates/web.php.tpl @@ -14,7 +14,7 @@ Container::$routeCollection = new SokoWeb\Routing\RouteCollection(); Container::$routeCollection->get('index', '', [{app}\Controller\HomeController::class, 'getIndex']); if (isset($_COOKIE['COOKIES_CONSENT'])) { - Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler(Container::$dbConnection); + Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler(Container::$dbConnection, 'sessions'); session_set_save_handler(Container::$sessionHandler, true); session_start([ -- 2.45.2 From 6a35344210160702f055591270e7f0e9bed1d42e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Tue, 2 May 2023 11:16:20 +0200 Subject: [PATCH 3/4] pass Session object to Request --- src/Request/Request.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Request/Request.php b/src/Request/Request.php index 535f4f0..77cd711 100644 --- a/src/Request/Request.php +++ b/src/Request/Request.php @@ -17,7 +17,7 @@ class Request implements IRequest private array $headers; - private Session $session; + private ISession $session; private ?IUser $user = null; @@ -26,14 +26,14 @@ class Request implements IRequest array $get, array $post, array $headers, - array &$session, + ISession $session, IUserRepository $userRepository) { $this->base = $base; $this->get = $get; $this->post = $post; $this->headers = $headers; - $this->session = new Session($session); + $this->session = $session; $userId = $this->session->get('userId'); if ($userId !== null) { -- 2.45.2 From c57d1d40d433bc60983ced3b6a0c58e26131990f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Tue, 2 May 2023 12:02:30 +0200 Subject: [PATCH 4/4] fixup! remove unnecessary "pass by reference" variables from Request --- src/Interfaces/Request/IRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Interfaces/Request/IRequest.php b/src/Interfaces/Request/IRequest.php index 0df9507..a31978c 100644 --- a/src/Interfaces/Request/IRequest.php +++ b/src/Interfaces/Request/IRequest.php @@ -4,7 +4,7 @@ use SokoWeb\Interfaces\Authentication\IUser; interface IRequest { - public function setParsedRouteParams(array &$routeParams): void; + public function setParsedRouteParams(array $routeParams): void; public function getBase(): string; -- 2.45.2