Browse Source

implement separated models

PHP-GTK3
yggverse 4 months ago
parent
commit
10534df069
  1. 2
      src/Abstract/Model/Connection.php
  2. 6
      src/Entity/Browser.php
  3. 2
      src/Entity/Browser/Bookmark/Container/Content.php
  4. 2
      src/Entity/Browser/Bookmark/Container/Navbar/Delete.php
  5. 2
      src/Entity/Browser/Container/Page.php
  6. 4
      src/Entity/Browser/Container/Page/Navbar/Bookmark.php
  7. 4
      src/Entity/Browser/Container/Page/Navbar/Request/Completion.php
  8. 2
      src/Entity/Browser/Container/Tab.php
  9. 2
      src/Entity/Browser/History/Container/Content.php
  10. 2
      src/Entity/Browser/History/Container/Navbar/Delete.php
  11. 538
      src/Model/Database.php
  12. 31
      src/Model/Database/Auth.php
  13. 155
      src/Model/Database/Bookmark.php
  14. 173
      src/Model/Database/Cache.php
  15. 142
      src/Model/Database/History.php
  16. 32
      src/Model/Database/Identity.php
  17. 73
      src/Model/Database/Session.php

2
src/Abstract/Model/Connection.php

@ -239,7 +239,7 @@ abstract class Connection implements \Yggverse\Yoda\Interface\Model\Connection @@ -239,7 +239,7 @@ abstract class Connection implements \Yggverse\Yoda\Interface\Model\Connection
string $request
): ?object
{
return $this->_database->getCache(
return $this->_database->cache->get(
$request
);
}

6
src/Entity/Browser.php

@ -88,19 +88,19 @@ class Browser @@ -88,19 +88,19 @@ class Browser
if ($pid === 0)
{
// Reset previous records
$this->database->cleanSession();
$this->database->session->clean();
foreach ($this->container->tab->pages as $page)
{
// Save page session data
$this->database->addSession(
$this->database->session->add(
$page->navbar->request->getValue()
);
// Cache connection pool data
if ($page->connection)
{
$this->database->renewCache(
$this->database->cache->renew(
$page->navbar->request->getValue(),
$page->connection->getMime(),
$page->connection->getTitle(),

2
src/Entity/Browser/Bookmark/Container/Content.php

@ -76,7 +76,7 @@ class Content @@ -76,7 +76,7 @@ class Content
{
$this->table->data->clear();
if ($records = $this->container->bookmark->browser->database->findBookmark($filter))
if ($records = $this->container->bookmark->browser->database->bookmark->find($filter))
{
foreach ($records as $record)
{

2
src/Entity/Browser/Bookmark/Container/Navbar/Delete.php

@ -20,7 +20,7 @@ class Delete extends Button @@ -20,7 +20,7 @@ class Delete extends Button
{
if ($id = $this->navbar->container->content->table->getSelectedId())
{
$this->navbar->container->bookmark->browser->database->deleteBookmark(
$this->navbar->container->bookmark->browser->database->bookmark->delete(
$id
);
}

2
src/Entity/Browser/Container/Page.php

@ -279,7 +279,7 @@ class Page @@ -279,7 +279,7 @@ class Page
if ($pid === 0)
{
$this->container->browser->database->renewHistory(
$this->container->browser->database->history->renew(
$this->navbar->request->getValue(),
$this->title->getValue()
);

4
src/Entity/Browser/Container/Page/Navbar/Bookmark.php

@ -25,7 +25,7 @@ class Bookmark extends Button @@ -25,7 +25,7 @@ class Bookmark extends Button
): void
{
$this->setImage(
$this->navbar->page->container->browser->database->toggleBookmark(
$this->navbar->page->container->browser->database->bookmark->toggle(
$this->navbar->request->getValue(),
$this->navbar->page->title->getValue()
) ? self::_IMAGE_STARRED_YES : self::_IMAGE_STARRED_NON
@ -35,7 +35,7 @@ class Bookmark extends Button @@ -35,7 +35,7 @@ class Bookmark extends Button
public function refresh(): void
{
$this->setImage(
$this->navbar->page->container->browser->database->getBookmark(
$this->navbar->page->container->browser->database->bookmark->get(
$this->navbar->request->getValue()
) ? self::_IMAGE_STARRED_YES : self::_IMAGE_STARRED_NON
);

4
src/Entity/Browser/Container/Page/Navbar/Request/Completion.php

@ -68,7 +68,7 @@ class Completion @@ -68,7 +68,7 @@ class Completion
{
$this->suggestion->clear();
foreach ($this->request->navbar->page->container->browser->database->findBookmark(
foreach ($this->request->navbar->page->container->browser->database->bookmark->find(
$this->request->getValue(),
$offset,
$limit
@ -77,7 +77,7 @@ class Completion @@ -77,7 +77,7 @@ class Completion
$suggestions[] = $history->request;
}
foreach ($this->request->navbar->page->container->browser->database->findHistory(
foreach ($this->request->navbar->page->container->browser->database->history->find(
$this->request->getValue(),
$offset,
$limit

2
src/Entity/Browser/Container/Tab.php

@ -42,7 +42,7 @@ class Tab @@ -42,7 +42,7 @@ class Tab
);
// Restore previous session
foreach ($this->container->browser->database->getSession() as $session)
foreach ($this->container->browser->database->session->get() as $session)
{
$this->append(
$session->request,

2
src/Entity/Browser/History/Container/Content.php

@ -76,7 +76,7 @@ class Content @@ -76,7 +76,7 @@ class Content
{
$this->table->data->clear();
if ($records = $this->container->history->browser->database->findHistory($filter))
if ($records = $this->container->history->browser->database->history->find($filter))
{
foreach ($records as $record)
{

2
src/Entity/Browser/History/Container/Navbar/Delete.php

@ -20,7 +20,7 @@ class Delete extends Button @@ -20,7 +20,7 @@ class Delete extends Button
{
if ($id = $this->navbar->container->content->table->getSelectedId())
{
$this->navbar->container->history->browser->database->deleteHistory(
$this->navbar->container->history->browser->database->history->delete(
$id
);
}

538
src/Model/Database.php

@ -4,13 +4,20 @@ declare(strict_types=1); @@ -4,13 +4,20 @@ declare(strict_types=1);
namespace Yggverse\Yoda\Model;
use \PDO;
use \Pdo;
class Database
{
private PDO $_connection;
// Dependencies
public Pdo $connection;
private bool $_exists;
// Requirements
public Database\Auth $auth;
public Database\Bookmark $bookmark;
public Database\Cache $cache;
public Database\History $history;
public Database\Identity $identity;
public Database\Session $session;
public function __construct(
string $filename,
@ -18,12 +25,12 @@ class Database @@ -18,12 +25,12 @@ class Database
?string $password = null
) {
// Status
$this->_exists = file_exists(
$exists = file_exists(
$filename
);
// Init database connection
$this->_connection = new PDO(
// Init dependencies
$this->connection = new Pdo(
sprintf(
'sqlite:%s',
$filename
@ -32,519 +39,48 @@ class Database @@ -32,519 +39,48 @@ class Database
$password
);
$this->_connection->setAttribute(
PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION
$this->connection->setAttribute(
Pdo::ATTR_ERRMODE,
Pdo::ERRMODE_EXCEPTION
);
$this->_connection->setAttribute(
PDO::ATTR_DEFAULT_FETCH_MODE,
PDO::FETCH_OBJ
$this->connection->setAttribute(
Pdo::ATTR_DEFAULT_FETCH_MODE,
Pdo::FETCH_OBJ
);
// Init tables
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `auth`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`active` INTEGER NOT NULL,
`identity` INTEGER NOT NULL,
`request` VARCHAR(1024) NOT NULL
)
');
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `bookmark`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`request` VARCHAR(1024) UNIQUE,
`title` VARCHAR(255)
)
');
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `cache`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`request` VARCHAR(1024) UNIQUE,
`mime` VARCHAR(255),
`title` VARCHAR(255),
`subtitle` VARCHAR(255),
`tooltip` VARCHAR(255),
`data` BLOB
);
');
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `history`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`url` VARCHAR(1024) NOT NULL,
`title` VARCHAR(255)
)
');
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `identity`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`active` INTEGER NOT NULL,
`name` VARCHAR(255),
`crt` TEXT NOT NULL,
`key` TEXT NOT NULL
)
');
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `session`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`request` VARCHAR(1024)
);
');
// Initial setup
if (!$this->_exists)
{
// Add gemini protocol homepage
$this->addSession(
'gemini://geminiprotocol.net/'
);
// Add yggverse homepage
$this->addSession(
'gemini://yggverse.cities.yesterweb.org/'
);
}
}
// Bookmark
public function addBookmark(
?string $request = null,
?string $title = null,
?int $time = null
): int
{
$query = $this->_connection->prepare(
'INSERT INTO `bookmark` (
`time`,
`request`,
`title`
) VALUES (
:time,
:request,
:title
)'
);
$query->execute(
[
':time' => $time ? $time : time(),
':request' => $request,
':title' => $title
]
);
return intval(
$this->_connection->lastInsertId()
);
}
public function getBookmark(
?string $request = null
): ?object
{
$query = $this->_connection->prepare(
'SELECT * FROM `bookmark` WHERE `request` LIKE :request'
);
$query->execute(
[
':request' => $request
]
);
if ($record = $query->fetch())
{
return $record;
}
return null;
}
public function findBookmark(
?string $value = null,
int $start = 0,
int $limit = 1000
): array
{
$query = $this->_connection->prepare(
sprintf(
'SELECT * FROM `bookmark`
WHERE `request` LIKE :value OR `title` LIKE :value
ORDER BY `id` DESC
LIMIT %d,%d',
$start,
$limit
)
);
$query->execute(
[
':value' => sprintf(
'%%%s%%',
strval(
$value
)
)
]
);
return $query->fetchAll();
}
public function deleteBookmark(
int $id
): int
{
$query = $this->_connection->query(
sprintf(
'DELETE FROM `bookmark` WHERE `id` = %d',
$id
)
);
return $query->rowCount();
}
public function toggleBookmark(
?string $request = null,
?string $title = null,
?int $time = null
): bool
{
if ($record = $this->getBookmark($request))
{
$this->deleteBookmark(
$record->id
);
return false;
}
else
{
$this->addBookmark(
$request,
$title,
$time
);
return true;
}
}
// Cache
public function addCache(
?string $request = null,
?string $mime = null,
?string $title = null,
?string $subtitle = null,
?string $tooltip = null,
?string $data = null,
?int $time = null
): int
{
$query = $this->_connection->prepare(
'INSERT INTO `cache` (
`time`,
`request`,
`mime`,
`title`,
`subtitle`,
`tooltip`,
`data`
) VALUES (
:time,
:request,
:mime,
:title,
:subtitle,
:tooltip,
:data
)'
);
$query->execute(
[
':time' => $time ? $time : time(),
':request' => $request,
':mime' => $mime,
':title' => $title,
':subtitle' => $subtitle,
':tooltip' => $tooltip,
':data' => $data
]
);
return intval(
$this->_connection->lastInsertId()
);
}
public function getCache(
string $request = ''
): ?object
{
$query = $this->_connection->prepare(
'SELECT * FROM `cache` WHERE `request` LIKE :request'
);
$query->execute(
[
':request' => $request
]
);
if ($cache = $query->fetch())
{
return $cache;
}
return null;
}
public function deleteCache(
int $id
): int
{
$query = $this->_connection->query(
sprintf(
'DELETE FROM `cache` WHERE `id` = %d',
$id
)
);
return $query->rowCount();
}
public function cleanCache(
int $timeout = 0
): int
{
$query = $this->_connection->query(
sprintf(
'DELETE FROM `cache` WHERE `time` + %d < %d',
$timeout,
time()
)
);
return $query->rowCount();
}
public function renewCache(
string $request,
?string $mime = null,
?string $title = null,
?string $subtitle = null,
?string $tooltip = null,
?string $data = null,
?int $time = null
): void
{
// Find same records match URL
$query = $this->_connection->prepare(
'SELECT * FROM `cache` WHERE `request` LIKE :request'
);
$query->execute(
[
':request' => $request
]
);
// Drop previous records
foreach ($query->fetchAll() as $record)
{
$this->deleteCache(
$record->id
);
}
// Add new record
$this->addCache(
$request,
$mime,
$title,
$subtitle,
$tooltip,
$data,
$time
);
}
// History
public function addHistory(
string $url,
?string $title = null
): int
{
$query = $this->_connection->prepare(
'INSERT INTO `history` (`time`, `url`, `title`) VALUES (:time, :url, :title)'
);
$query->execute(
[
':time' => time(),
':url' => $url,
':title' => $title
]
);
return intval(
$this->_connection->lastInsertId()
);
}
public function findHistory(
string $value = '',
int $start = 0,
int $limit = 1000
): array
{
$query = $this->_connection->prepare(
sprintf(
'SELECT * FROM `history`
WHERE `url` LIKE :value OR `title` LIKE :value
ORDER BY `id` DESC
LIMIT %d,%d',
$start,
$limit
)
// Init requirements
$this->auth = new Database\Auth(
$this->connection
);
$query->execute(
[
':value' => sprintf(
'%%%s%%',
$value
)
]
$this->bookmark = new Database\Bookmark(
$this->connection
);
return $query->fetchAll();
}
public function deleteHistory(
int $id
): int
{
$query = $this->_connection->query(
sprintf(
'DELETE FROM `history` WHERE `id` = %d',
$id
)
$this->cache = new Database\Cache(
$this->connection
);
return $query->rowCount();
}
public function cleanHistory(
int $timeout = 0
): int
{
$query = $this->_connection->query(
sprintf(
'DELETE FROM `history` WHERE `time` + %d < %d',
$timeout,
time()
)
$this->history = new Database\History(
$this->connection
);
return $query->rowCount();
}
public function renewHistory(
string $url,
?string $title = null
): void
{
// Find same records match URL
$query = $this->_connection->prepare(
'SELECT * FROM `history` WHERE `url` LIKE :url'
$this->identity = new Database\Identity(
$this->connection
);
$query->execute(
[
':url' => $url
]
$this->session = new Database\Session(
$this->connection
);
// Drop previous records
foreach ($query->fetchAll() as $record)
// Init data
if (!$exists)
{
$this->deleteHistory(
$record->id
);
}
// Add new record
$this->addHistory(
$url,
$title
);
}
// Session
public function addSession(
?string $request = null,
?int $time = null
): int
{
$query = $this->_connection->prepare(
'INSERT INTO `session` (`time`, `request`) VALUES (:time, :request)'
);
$query->execute(
[
':time' => $time ? $time : time(),
':request' => $request
]
);
return intval(
$this->_connection->lastInsertId()
);
}
public function getSession(): array
{
$query = $this->_connection->query(
'SELECT * FROM `session`'
// Open yggverse homepage
$this->session->add(
'gemini://yggverse.cities.yesterweb.org/'
);
if ($session = $query->fetchAll())
{
return $session;
}
return [];
}
public function cleanSession(): int
{
$query = $this->_connection->query(
'DELETE FROM `session`'
);
return $query->rowCount();
}
}

31
src/Model/Database/Auth.php

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Model\Database;
use \Pdo;
class Auth
{
public Pdo $connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
// Init database structure
$this->connection->query('
CREATE TABLE IF NOT EXISTS `auth`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`active` INTEGER NOT NULL,
`identity` INTEGER NOT NULL,
`request` VARCHAR(1024) NOT NULL
)
');
}
}

155
src/Model/Database/Bookmark.php

@ -0,0 +1,155 @@ @@ -0,0 +1,155 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Model\Database;
use \Pdo;
class Bookmark
{
public Pdo $connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
// Init database structure
$this->connection->query('
CREATE TABLE IF NOT EXISTS `bookmark`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`request` VARCHAR(1024) UNIQUE,
`title` VARCHAR(255)
)
');
}
public function add(
?string $request = null,
?string $title = null,
?int $time = null
): int
{
$query = $this->connection->prepare(
'INSERT INTO `bookmark` (
`time`,
`request`,
`title`
) VALUES (
:time,
:request,
:title
)'
);
$query->execute(
[
':time' => $time ? $time : time(),
':request' => $request,
':title' => $title
]
);
return intval(
$this->connection->lastInsertId()
);
}
public function get(
?string $request = null
): ?object
{
$query = $this->connection->prepare(
'SELECT * FROM `bookmark` WHERE `request` LIKE :request'
);
$query->execute(
[
':request' => $request
]
);
if ($record = $query->fetch())
{
return $record;
}
return null;
}
public function find(
?string $value = null,
int $start = 0,
int $limit = 1000
): array
{
$query = $this->connection->prepare(
sprintf(
'SELECT * FROM `bookmark`
WHERE `request` LIKE :value OR `title` LIKE :value
ORDER BY `id` DESC
LIMIT %d,%d',
$start,
$limit
)
);
$query->execute(
[
':value' => sprintf(
'%%%s%%',
strval(
$value
)
)
]
);
return $query->fetchAll();
}
public function delete(
int $id
): int
{
$query = $this->connection->query(
sprintf(
'DELETE FROM `bookmark` WHERE `id` = %d',
$id
)
);
return $query->rowCount();
}
public function toggle(
?string $request = null,
?string $title = null,
?int $time = null
): bool
{
if ($record = $this->get($request))
{
$this->delete(
$record->id
);
return false;
}
else
{
$this->add(
$request,
$title,
$time
);
return true;
}
}
}

173
src/Model/Database/Cache.php

@ -0,0 +1,173 @@ @@ -0,0 +1,173 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Model\Database;
use \Pdo;
class Cache
{
public Pdo $connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
// Init database structure
$this->connection->query('
CREATE TABLE IF NOT EXISTS `cache`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`request` VARCHAR(1024) UNIQUE,
`mime` VARCHAR(255),
`title` VARCHAR(255),
`subtitle` VARCHAR(255),
`tooltip` VARCHAR(255),
`data` BLOB
)
');
}
public function add(
?string $request = null,
?string $mime = null,
?string $title = null,
?string $subtitle = null,
?string $tooltip = null,
?string $data = null,
?int $time = null
): int
{
$query = $this->connection->prepare(
'INSERT INTO `cache` (
`time`,
`request`,
`mime`,
`title`,
`subtitle`,
`tooltip`,
`data`
) VALUES (
:time,
:request,
:mime,
:title,
:subtitle,
:tooltip,
:data
)'
);
$query->execute(
[
':time' => $time ? $time : time(),
':request' => $request,
':mime' => $mime,
':title' => $title,
':subtitle' => $subtitle,
':tooltip' => $tooltip,
':data' => $data
]
);
return intval(
$this->connection->lastInsertId()
);
}
public function get(
string $request = ''
): ?object
{
$query = $this->connection->prepare(
'SELECT * FROM `cache` WHERE `request` LIKE :request'
);
$query->execute(
[
':request' => $request
]
);
if ($cache = $query->fetch())
{
return $cache;
}
return null;
}
public function delete(
int $id
): int
{
$query = $this->connection->query(
sprintf(
'DELETE FROM `cache` WHERE `id` = %d',
$id
)
);
return $query->rowCount();
}
public function clean(
int $timeout = 0
): int
{
$query = $this->connection->query(
sprintf(
'DELETE FROM `cache` WHERE `time` + %d < %d',
$timeout,
time()
)
);
return $query->rowCount();
}
public function renew(
string $request,
?string $mime = null,
?string $title = null,
?string $subtitle = null,
?string $tooltip = null,
?string $data = null,
?int $time = null
): void
{
// Find same records match URL
$query = $this->connection->prepare(
'SELECT * FROM `cache` WHERE `request` LIKE :request'
);
$query->execute(
[
':request' => $request
]
);
// Drop previous records
foreach ($query->fetchAll() as $record)
{
$this->delete(
$record->id
);
}
// Add new record
$this->add(
$request,
$mime,
$title,
$subtitle,
$tooltip,
$data,
$time
);
}
}

142
src/Model/Database/History.php

@ -0,0 +1,142 @@ @@ -0,0 +1,142 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Model\Database;
use \Pdo;
class History
{
public Pdo $connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
// Init database structure
$this->connection->query('
CREATE TABLE IF NOT EXISTS `history`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`url` VARCHAR(1024) NOT NULL,
`title` VARCHAR(255)
)
');
}
public function add(
string $url,
?string $title = null
): int
{
$query = $this->connection->prepare(
'INSERT INTO `history` (`time`, `url`, `title`) VALUES (:time, :url, :title)'
);
$query->execute(
[
':time' => time(),
':url' => $url,
':title' => $title
]
);
return intval(
$this->connection->lastInsertId()
);
}
public function find(
string $value = '',
int $start = 0,
int $limit = 1000
): array
{
$query = $this->connection->prepare(
sprintf(
'SELECT * FROM `history`
WHERE `url` LIKE :value OR `title` LIKE :value
ORDER BY `id` DESC
LIMIT %d,%d',
$start,
$limit
)
);
$query->execute(
[
':value' => sprintf(
'%%%s%%',
$value
)
]
);
return $query->fetchAll();
}
public function delete(
int $id
): int
{
$query = $this->connection->query(
sprintf(
'DELETE FROM `history` WHERE `id` = %d',
$id
)
);
return $query->rowCount();
}
public function clean(
int $timeout = 0
): int
{
$query = $this->connection->query(
sprintf(
'DELETE FROM `history` WHERE `time` + %d < %d',
$timeout,
time()
)
);
return $query->rowCount();
}
public function renew(
string $url,
?string $title = null
): void
{
// Find same records match URL
$query = $this->connection->prepare(
'SELECT * FROM `history` WHERE `url` LIKE :url'
);
$query->execute(
[
':url' => $url
]
);
// Drop previous records
foreach ($query->fetchAll() as $record)
{
$this->delete(
$record->id
);
}
// Add new record
$this->add(
$url,
$title
);
}
}

32
src/Model/Database/Identity.php

@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Model\Database;
use \Pdo;
class Identity
{
public Pdo $connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
// Init database structure
$this->connection->query('
CREATE TABLE IF NOT EXISTS `identity`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`active` INTEGER NOT NULL,
`name` VARCHAR(255),
`crt` TEXT NOT NULL,
`key` TEXT NOT NULL
)
');
}
}

73
src/Model/Database/Session.php

@ -0,0 +1,73 @@ @@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Model\Database;
use \Pdo;
class Session
{
public Pdo $connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
// Init database structure
$this->connection->query('
CREATE TABLE IF NOT EXISTS `session`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`request` VARCHAR(1024)
)
');
}
public function add(
?string $request = null,
?int $time = null
): int
{
$query = $this->connection->prepare(
'INSERT INTO `session` (`time`, `request`) VALUES (:time, :request)'
);
$query->execute(
[
':time' => $time ? $time : time(),
':request' => $request
]
);
return intval(
$this->connection->lastInsertId()
);
}
public function get(): array
{
$query = $this->connection->query(
'SELECT * FROM `session`'
);
if ($session = $query->fetchAll())
{
return $session;
}
return [];
}
public function clean(): int
{
$query = $this->connection->query(
'DELETE FROM `session`'
);
return $query->rowCount();
}
}
Loading…
Cancel
Save