From 8c6627cfb8ad8ec6d1505bdcd0eb7f1546204e58 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 2 Aug 2024 22:20:12 +0300 Subject: [PATCH] make database connection access protected --- src/Model/Database.php | 22 ++++++++++------------ src/Model/Database/Auth.php | 6 +++--- src/Model/Database/Bookmark.php | 16 ++++++++-------- src/Model/Database/Cache.php | 18 +++++++++--------- src/Model/Database/History.php | 18 +++++++++--------- src/Model/Database/Identity.php | 6 +++--- src/Model/Database/Session.php | 14 +++++++------- 7 files changed, 49 insertions(+), 51 deletions(-) diff --git a/src/Model/Database.php b/src/Model/Database.php index 1fd83da7..6d186801 100644 --- a/src/Model/Database.php +++ b/src/Model/Database.php @@ -8,10 +8,8 @@ use \Pdo; class Database { - // Dependencies - public Pdo $connection; + protected Pdo $_connection; - // Requirements public Database\Auth $auth; public Database\Bookmark $bookmark; public Database\Cache $cache; @@ -30,7 +28,7 @@ class Database ); // Init dependencies - $this->connection = new Pdo( + $this->_connection = new Pdo( sprintf( 'sqlite:%s', $filename @@ -39,39 +37,39 @@ class Database $password ); - $this->connection->setAttribute( + $this->_connection->setAttribute( Pdo::ATTR_ERRMODE, Pdo::ERRMODE_EXCEPTION ); - $this->connection->setAttribute( + $this->_connection->setAttribute( Pdo::ATTR_DEFAULT_FETCH_MODE, Pdo::FETCH_OBJ ); // Init requirements $this->auth = new Database\Auth( - $this->connection + $this->_connection ); $this->bookmark = new Database\Bookmark( - $this->connection + $this->_connection ); $this->cache = new Database\Cache( - $this->connection + $this->_connection ); $this->history = new Database\History( - $this->connection + $this->_connection ); $this->identity = new Database\Identity( - $this->connection + $this->_connection ); $this->session = new Database\Session( - $this->connection + $this->_connection ); // Init data diff --git a/src/Model/Database/Auth.php b/src/Model/Database/Auth.php index 94b91c11..99b153b3 100644 --- a/src/Model/Database/Auth.php +++ b/src/Model/Database/Auth.php @@ -8,16 +8,16 @@ use \Pdo; class Auth { - public Pdo $connection; + protected Pdo $_connection; public function __construct( Pdo $connection ) { // Init parent connection - $this->connection = $connection; + $this->_connection = $connection; // Init database structure - $this->connection->query(' + $this->_connection->query(' CREATE TABLE IF NOT EXISTS `auth` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, diff --git a/src/Model/Database/Bookmark.php b/src/Model/Database/Bookmark.php index 8e94cb3b..473e563d 100644 --- a/src/Model/Database/Bookmark.php +++ b/src/Model/Database/Bookmark.php @@ -8,16 +8,16 @@ use \Pdo; class Bookmark { - public Pdo $connection; + protected Pdo $_connection; public function __construct( Pdo $connection ) { // Init parent connection - $this->connection = $connection; + $this->_connection = $connection; // Init database structure - $this->connection->query(' + $this->_connection->query(' CREATE TABLE IF NOT EXISTS `bookmark` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, @@ -34,7 +34,7 @@ class Bookmark ?int $time = null ): int { - $query = $this->connection->prepare( + $query = $this->_connection->prepare( 'INSERT INTO `bookmark` ( `time`, `request`, @@ -55,7 +55,7 @@ class Bookmark ); return intval( - $this->connection->lastInsertId() + $this->_connection->lastInsertId() ); } @@ -63,7 +63,7 @@ class Bookmark ?string $request = null ): ?object { - $query = $this->connection->prepare( + $query = $this->_connection->prepare( 'SELECT * FROM `bookmark` WHERE `request` LIKE :request' ); @@ -87,7 +87,7 @@ class Bookmark int $limit = 1000 ): array { - $query = $this->connection->prepare( + $query = $this->_connection->prepare( sprintf( 'SELECT * FROM `bookmark` WHERE `request` LIKE :value OR `title` LIKE :value @@ -116,7 +116,7 @@ class Bookmark int $id ): int { - $query = $this->connection->query( + $query = $this->_connection->query( sprintf( 'DELETE FROM `bookmark` WHERE `id` = %d', $id diff --git a/src/Model/Database/Cache.php b/src/Model/Database/Cache.php index c180a5c9..b7c353e1 100644 --- a/src/Model/Database/Cache.php +++ b/src/Model/Database/Cache.php @@ -8,16 +8,16 @@ use \Pdo; class Cache { - public Pdo $connection; + protected Pdo $_connection; public function __construct( Pdo $connection ) { // Init parent connection - $this->connection = $connection; + $this->_connection = $connection; // Init database structure - $this->connection->query(' + $this->_connection->query(' CREATE TABLE IF NOT EXISTS `cache` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, @@ -42,7 +42,7 @@ class Cache ?int $time = null ): int { - $query = $this->connection->prepare( + $query = $this->_connection->prepare( 'INSERT INTO `cache` ( `time`, `request`, @@ -75,7 +75,7 @@ class Cache ); return intval( - $this->connection->lastInsertId() + $this->_connection->lastInsertId() ); } @@ -83,7 +83,7 @@ class Cache string $request = '' ): ?object { - $query = $this->connection->prepare( + $query = $this->_connection->prepare( 'SELECT * FROM `cache` WHERE `request` LIKE :request' ); @@ -105,7 +105,7 @@ class Cache int $id ): int { - $query = $this->connection->query( + $query = $this->_connection->query( sprintf( 'DELETE FROM `cache` WHERE `id` = %d', $id @@ -119,7 +119,7 @@ class Cache int $timeout = 0 ): int { - $query = $this->connection->query( + $query = $this->_connection->query( sprintf( 'DELETE FROM `cache` WHERE `time` + %d < %d', $timeout, @@ -141,7 +141,7 @@ class Cache ): void { // Find same records match URL - $query = $this->connection->prepare( + $query = $this->_connection->prepare( 'SELECT * FROM `cache` WHERE `request` LIKE :request' ); diff --git a/src/Model/Database/History.php b/src/Model/Database/History.php index 9a6ccd5d..806b553a 100644 --- a/src/Model/Database/History.php +++ b/src/Model/Database/History.php @@ -8,16 +8,16 @@ use \Pdo; class History { - public Pdo $connection; + protected Pdo $_connection; public function __construct( Pdo $connection ) { // Init parent connection - $this->connection = $connection; + $this->_connection = $connection; // Init database structure - $this->connection->query(' + $this->_connection->query(' CREATE TABLE IF NOT EXISTS `history` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, @@ -33,7 +33,7 @@ class History ?string $title = null ): int { - $query = $this->connection->prepare( + $query = $this->_connection->prepare( 'INSERT INTO `history` (`time`, `url`, `title`) VALUES (:time, :url, :title)' ); @@ -46,7 +46,7 @@ class History ); return intval( - $this->connection->lastInsertId() + $this->_connection->lastInsertId() ); } @@ -56,7 +56,7 @@ class History int $limit = 1000 ): array { - $query = $this->connection->prepare( + $query = $this->_connection->prepare( sprintf( 'SELECT * FROM `history` WHERE `url` LIKE :value OR `title` LIKE :value @@ -83,7 +83,7 @@ class History int $id ): int { - $query = $this->connection->query( + $query = $this->_connection->query( sprintf( 'DELETE FROM `history` WHERE `id` = %d', $id @@ -97,7 +97,7 @@ class History int $timeout = 0 ): int { - $query = $this->connection->query( + $query = $this->_connection->query( sprintf( 'DELETE FROM `history` WHERE `time` + %d < %d', $timeout, @@ -115,7 +115,7 @@ class History ): void { // Find same records match URL - $query = $this->connection->prepare( + $query = $this->_connection->prepare( 'SELECT * FROM `history` WHERE `url` LIKE :url' ); diff --git a/src/Model/Database/Identity.php b/src/Model/Database/Identity.php index aadd72e4..2bb154ca 100644 --- a/src/Model/Database/Identity.php +++ b/src/Model/Database/Identity.php @@ -8,16 +8,16 @@ use \Pdo; class Identity { - public Pdo $connection; + protected Pdo $_connection; public function __construct( Pdo $connection ) { // Init parent connection - $this->connection = $connection; + $this->_connection = $connection; // Init database structure - $this->connection->query(' + $this->_connection->query(' CREATE TABLE IF NOT EXISTS `identity` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, diff --git a/src/Model/Database/Session.php b/src/Model/Database/Session.php index 92e8c1c5..f1cd319c 100644 --- a/src/Model/Database/Session.php +++ b/src/Model/Database/Session.php @@ -8,16 +8,16 @@ use \Pdo; class Session { - public Pdo $connection; + protected Pdo $_connection; public function __construct( Pdo $connection ) { // Init parent connection - $this->connection = $connection; + $this->_connection = $connection; // Init database structure - $this->connection->query(' + $this->_connection->query(' CREATE TABLE IF NOT EXISTS `session` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, @@ -32,7 +32,7 @@ class Session ?int $time = null ): int { - $query = $this->connection->prepare( + $query = $this->_connection->prepare( 'INSERT INTO `session` (`time`, `request`) VALUES (:time, :request)' ); @@ -44,13 +44,13 @@ class Session ); return intval( - $this->connection->lastInsertId() + $this->_connection->lastInsertId() ); } public function get(): array { - $query = $this->connection->query( + $query = $this->_connection->query( 'SELECT * FROM `session`' ); @@ -64,7 +64,7 @@ class Session public function clean(): int { - $query = $this->connection->query( + $query = $this->_connection->query( 'DELETE FROM `session`' );