feat: database support

This commit is contained in:
Joel Male
2025-02-25 16:03:17 +10:00
parent ec99aa0563
commit 0f07b9ae8a
12 changed files with 4882 additions and 882 deletions
+16 -5
View File
@@ -6,6 +6,9 @@ class CartSession
{
private $customStorage;
public $sessionKey;
public $driver;
private $session;
private $sessionId = null;
@@ -14,12 +17,15 @@ class CartSession
public function __construct($session, $sessionKey, $config)
{
if ($config['driver'] === 'session') {
$this->sessionKey = $sessionKey;
$this->driver = $config['driver'];
if ($this->driver === 'session') {
$this->customStorage = false;
$this->session = $session;
$this->itemsKey = $sessionKey . '_cart_items';
$this->conditionsKey = $sessionKey . '_cart_conditions';
} elseif ($config['driver'] === 'database') {
$this->itemsKey = $this->sessionKey . '_cart_items';
$this->conditionsKey = $this->sessionKey . '_cart_conditions';
} elseif ($this->driver === 'database') {
$this->customStorage = true;
$this->sessionId = $config['storage']['database']['id'];
@@ -27,7 +33,7 @@ public function __construct($session, $sessionKey, $config)
$this->conditionsKey = $config['storage']['database']['conditions'];
// find or create the session in the database
$this->session = $session->firstOrNew([$this->sessionId => $sessionKey]);
$this->session = $session->firstOrNew([$this->sessionId => $this->sessionKey]);
// initialize the items and conditions
$this->session[$this->itemsKey] = $this->session[$this->itemsKey] ?? [];
@@ -99,4 +105,9 @@ public function clear()
return true;
}
public function getSessionModel()
{
return $this->session;
}
}