|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of ibrand/laravel-shopping-cart. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) iBrand <https://www.ibrand.cc> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace iBrand\Shoppingcart; |
|
13
|
|
|
|
|
14
|
|
|
use iBrand\Shoppingcart\Storage\SessionStorage; |
|
15
|
|
|
use Illuminate\Support\ServiceProvider as LaravelServiceProvider; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Service provider for Laravel. |
|
19
|
|
|
*/ |
|
20
|
|
|
class ServiceProvider extends LaravelServiceProvider |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Indicates if loading of the provider is deferred. |
|
24
|
|
|
* |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $defer = true; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Boot the provider. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function boot() |
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->app->runningInConsole()) { |
|
35
|
|
|
$this->registerMigrations(); |
|
36
|
|
|
} |
|
37
|
|
|
// |
|
38
|
|
|
//publish a config file |
|
39
|
|
|
$this->publishes([ |
|
40
|
|
|
__DIR__ . '/config.php' => config_path('ibrand/cart.php'), |
|
41
|
|
|
]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Register the service provider. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function register() |
|
48
|
|
|
{ |
|
49
|
|
|
// merge configs |
|
50
|
|
|
$this->mergeConfigFrom( |
|
51
|
|
|
__DIR__ . '/config.php', 'ibrand.cart' |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$this->app->singleton(Cart::class, function ($app) { |
|
55
|
|
|
$storage = config('ibrand.cart.storage'); |
|
56
|
|
|
|
|
57
|
|
|
$cart = new Cart(new $storage(), $app['events']); |
|
58
|
|
|
|
|
59
|
|
|
if (SessionStorage::class == $storage) { |
|
60
|
|
|
return $cart; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
//The below code is used of database storage |
|
64
|
|
|
$currentGuard = 'default'; |
|
65
|
|
|
$user = null; |
|
66
|
|
|
|
|
67
|
|
|
if ($defaultGuard = $app['auth']->getDefaultDriver()) { |
|
68
|
|
|
$currentGuard = $defaultGuard; |
|
69
|
|
|
$user = auth($currentGuard)->user(); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($user) { |
|
73
|
|
|
//The cart name like `cart.{guard}.{user_id}`: cart.api.1 |
|
74
|
|
|
$aliases = config('ibrand.cart.aliases'); |
|
75
|
|
|
|
|
76
|
|
|
if (isset($aliases[$currentGuard])) { |
|
77
|
|
|
$currentGuard = $aliases[$currentGuard]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$cart->name($currentGuard . '.' . $user->id); |
|
81
|
|
|
|
|
82
|
|
|
} else { |
|
83
|
|
|
throw new Exception('Invalid auth.'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $cart; |
|
87
|
|
|
}); |
|
88
|
|
|
|
|
89
|
|
|
$this->app->alias(Cart::class, 'cart'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get the services provided by the provider. |
|
94
|
|
|
* |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
public function provides() |
|
98
|
|
|
{ |
|
99
|
|
|
return [Cart::class, 'cart']; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* load migration files. |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function registerMigrations() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->loadMigrationsFrom(__DIR__ . '/../migrations'); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: