mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
feat: database support
This commit is contained in:
+18
-13
@@ -15,11 +15,6 @@ class Cart
|
||||
|
||||
protected $instanceName;
|
||||
|
||||
protected $customStorage = false;
|
||||
protected $sessionKey;
|
||||
protected $sessionKeyCartItems;
|
||||
protected $sessionKeyCartConditions;
|
||||
|
||||
protected $config;
|
||||
|
||||
protected $currentItemId;
|
||||
@@ -168,7 +163,7 @@ public function add(
|
||||
*/
|
||||
public function update(int|string $id, array $data): bool
|
||||
{
|
||||
if ($this->fireEvent('Updating', $data) === false) {
|
||||
if ($this->fireEvent('Updating', 'item', $data) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -201,7 +196,7 @@ public function update(int|string $id, array $data): bool
|
||||
|
||||
$this->save($cart);
|
||||
|
||||
$this->fireEvent('Updated', $item);
|
||||
$this->fireEvent('Updated', 'item', $item);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -260,7 +255,7 @@ public function remove($id)
|
||||
{
|
||||
$cart = $this->getContent();
|
||||
|
||||
if ($this->fireEvent('Removing', $id) === false) {
|
||||
if ($this->fireEvent('Removing', 'item', $id) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -268,7 +263,7 @@ public function remove($id)
|
||||
|
||||
$this->save($cart);
|
||||
|
||||
$this->fireEvent('Removed', $id);
|
||||
$this->fireEvent('Removed', 'item', $id);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -714,7 +709,7 @@ protected function validate($item)
|
||||
*/
|
||||
protected function addItem($id, $item)
|
||||
{
|
||||
if ($this->fireEvent('Adding', $item) === false) {
|
||||
if ($this->fireEvent('Adding', 'item', $item) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -724,7 +719,7 @@ protected function addItem($id, $item)
|
||||
|
||||
$this->save($cart);
|
||||
|
||||
$this->fireEvent('Added', $item);
|
||||
$this->fireEvent('Added', 'item', $item);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -815,9 +810,19 @@ public function setDecimals($decimals)
|
||||
$this->decimals = $decimals;
|
||||
}
|
||||
|
||||
protected function fireEvent($name, $value = [])
|
||||
protected function fireEvent($name, $type = null, $data = null)
|
||||
{
|
||||
return $this->events->dispatch('LaravelCart.' . $name, array_values([$value, $this]), true);
|
||||
$eventData = [
|
||||
'session_key' => $this->session->sessionKey,
|
||||
'session_driver' => $this->session->driver,
|
||||
'session_model' => $this->session->getSessionModel(),
|
||||
];
|
||||
|
||||
if ($type) {
|
||||
$eventData[$type] = $data;
|
||||
}
|
||||
|
||||
return $this->events->dispatch('LaravelCart.' . $name, array_values([$eventData, $this]), true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -46,7 +46,7 @@ public function register()
|
||||
$storage,
|
||||
$events,
|
||||
'cart',
|
||||
'4yTlTDKu3oJOfzD',
|
||||
session()->getId(),
|
||||
$config
|
||||
);
|
||||
});
|
||||
|
||||
+16
-5
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user