mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-23 23:51:25 +00:00
Working on improved configuration and automatically destroy cart on user logout
This commit is contained in:
@@ -11,6 +11,37 @@ return [
|
|||||||
| Taxable interface and use the HasTax trait.
|
| Taxable interface and use the HasTax trait.
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
'tax' => 21
|
|
||||||
|
'tax' => 21,
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Shoppingcart database settings
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Here you can set the connection that the shoppingcart should use when
|
||||||
|
| storing and restoring a cart.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'database' => [
|
||||||
|
|
||||||
|
'connection' => null,
|
||||||
|
|
||||||
|
'table' => 'shoppingcart',
|
||||||
|
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Destroy the cart on user logout
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| When this option is set to 'true' the cart will automatically
|
||||||
|
| destroy all cart instances when the user logs out.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'destroy_on_logout' => false,
|
||||||
|
|
||||||
];
|
];
|
||||||
16
src/Cart.php
16
src/Cart.php
@@ -493,7 +493,9 @@ class Cart
|
|||||||
*/
|
*/
|
||||||
private function getConnection()
|
private function getConnection()
|
||||||
{
|
{
|
||||||
return app(DatabaseManager::class)->connection(config('cart.database.connection', config('database.default')));
|
$connectionName = $this->getConnectionName();
|
||||||
|
|
||||||
|
return app(DatabaseManager::class)->connection($connectionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -505,4 +507,16 @@ class Cart
|
|||||||
{
|
{
|
||||||
return config('cart.database.table', 'shoppingcart');
|
return config('cart.database.table', 'shoppingcart');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the database connection name.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function getConnectionName()
|
||||||
|
{
|
||||||
|
$connection = config('cart.database.connection');
|
||||||
|
|
||||||
|
return is_null($connection) ? config('database.default') : $connection;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Gloudemans\Shoppingcart;
|
namespace Gloudemans\Shoppingcart;
|
||||||
|
|
||||||
|
use Illuminate\Auth\Events\Logout;
|
||||||
|
use Illuminate\Session\SessionManager;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class ShoppingcartServiceProvider extends ServiceProvider
|
class ShoppingcartServiceProvider extends ServiceProvider
|
||||||
@@ -19,6 +21,14 @@ class ShoppingcartServiceProvider extends ServiceProvider
|
|||||||
$config = __DIR__ . '/../config/cart.php';
|
$config = __DIR__ . '/../config/cart.php';
|
||||||
$this->mergeConfigFrom($config, 'cart');
|
$this->mergeConfigFrom($config, 'cart');
|
||||||
|
|
||||||
|
$this->publishes([__DIR__ . '/../config/cart.php' => config_path('cart.php')], 'config');
|
||||||
|
|
||||||
|
$this->app['events']->listen(Logout::class, function () {
|
||||||
|
if ($this->app['config']->get('cart.destroy_on_logout')) {
|
||||||
|
$this->app->make(SessionManager::class)->forget('cart');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if ( ! class_exists('CreateShoppingcartTable')) {
|
if ( ! class_exists('CreateShoppingcartTable')) {
|
||||||
// Publish the migration
|
// Publish the migration
|
||||||
$timestamp = date('Y_m_d_His', time());
|
$timestamp = date('Y_m_d_His', time());
|
||||||
|
|||||||
@@ -614,7 +614,7 @@ class CartTest extends Orchestra\Testbench\TestCase
|
|||||||
|
|
||||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||||
|
|
||||||
$this->assertEquals('Mockery_0_Gloudemans_Shoppingcart_Contracts_Buyable', PHPUnit_Framework_Assert::readAttribute($cartItem, 'associatedModel'));
|
$this->assertContains('Gloudemans_Shoppingcart_Contracts_Buyable', PHPUnit_Framework_Assert::readAttribute($cartItem, 'associatedModel'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@@ -909,6 +909,22 @@ class CartTest extends Orchestra\Testbench\TestCase
|
|||||||
$this->assertEquals(3.80, $cart->tax(2));
|
$this->assertEquals(3.80, $cart->tax(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_will_destroy_the_cart_when_the_user_logs_out_and_the_config_setting_was_set_to_true()
|
||||||
|
{
|
||||||
|
$this->app['config']->set('cart.destroy_on_logout', true);
|
||||||
|
|
||||||
|
$session = Mockery::mock(\Illuminate\Session\SessionManager::class);
|
||||||
|
|
||||||
|
$session->shouldReceive('forget')->once()->with('cart');
|
||||||
|
|
||||||
|
$this->app->instance(\Illuminate\Session\SessionManager::class, $session);
|
||||||
|
|
||||||
|
$user = Mockery::mock(\Illuminate\Contracts\Auth\Authenticatable::class);
|
||||||
|
|
||||||
|
event(new \Illuminate\Auth\Events\Logout($user));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an instance of the cart.
|
* Get an instance of the cart.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user