1
|
|
|
<?php namespace Arcanedev\LaravelTracker; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Support\PackageServiceProvider; |
4
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract; |
5
|
|
|
use Illuminate\Contracts\Foundation\Application as AppContract; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class LaravelTrackerServiceProvider |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\LaravelTracker |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class LaravelTrackerServiceProvider extends PackageServiceProvider |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Properties |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Package name. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $package = 'laravel-tracker'; |
26
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
28
|
|
|
| Main Methods |
29
|
|
|
| ----------------------------------------------------------------- |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Register the service provider. |
34
|
|
|
*/ |
35
|
117 |
|
public function register() |
36
|
|
|
{ |
37
|
117 |
|
parent::register(); |
38
|
|
|
|
39
|
117 |
|
$this->registerConfig(); |
40
|
117 |
|
$this->registerProviders([ |
41
|
117 |
|
Providers\PackagesServiceProvider::class, |
42
|
|
|
Providers\EventServiceProvider::class, |
43
|
|
|
]); |
44
|
117 |
|
$this->registerConsoleServiceProvider(Providers\CommandServiceProvider::class); |
45
|
|
|
|
46
|
117 |
|
$this->registerDetectors(); |
47
|
117 |
|
$this->registerParsers(); |
48
|
117 |
|
$this->registerTrackers(); |
49
|
117 |
|
$this->registerMainTracker(); |
50
|
117 |
|
$this->registerExceptionHandler(); |
51
|
117 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Boot the service provider. |
55
|
|
|
*/ |
56
|
117 |
|
public function boot() |
57
|
|
|
{ |
58
|
117 |
|
parent::boot(); |
59
|
|
|
|
60
|
117 |
|
$this->registerModelsBindings(); |
61
|
|
|
|
62
|
|
|
// Publishes |
63
|
117 |
|
$this->publishConfig(); |
64
|
|
|
|
65
|
117 |
|
Tracker::$runsMigrations ? $this->loadMigrations() : $this->publishMigrations(); |
66
|
117 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the services provided by the provider. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
3 |
|
public function provides() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
3 |
|
Contracts\Tracker::class, |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* ----------------------------------------------------------------- |
81
|
|
|
| Other Methods |
82
|
|
|
| ----------------------------------------------------------------- |
83
|
|
|
*/ |
84
|
|
|
/** |
85
|
|
|
* Binding the models with the contracts. |
86
|
|
|
*/ |
87
|
117 |
|
private function registerModelsBindings() |
88
|
|
|
{ |
89
|
117 |
|
foreach (Support\BindingManager::getModelsBindings() as $key => $contract) { |
90
|
117 |
|
$this->bind($contract, $this->config()->get("laravel-tracker.models.$key")); |
91
|
|
|
} |
92
|
117 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Register the detectors. |
96
|
|
|
*/ |
97
|
|
|
private function registerDetectors() |
98
|
|
|
{ |
99
|
117 |
|
$this->singleton(Contracts\Detectors\CrawlerDetector::class, function (AppContract $app) { |
100
|
|
|
/** @var \Illuminate\Http\Request $request */ |
101
|
6 |
|
$request = $app['request']; |
102
|
|
|
|
103
|
6 |
|
return new Detectors\CrawlerDetector( |
104
|
6 |
|
new \Jaybizzle\CrawlerDetect\CrawlerDetect( |
105
|
6 |
|
$request->headers->all(), |
106
|
6 |
|
$request->server('HTTP_USER_AGENT') |
107
|
|
|
) |
108
|
|
|
); |
109
|
117 |
|
}); |
110
|
|
|
|
111
|
117 |
|
$this->singleton(Contracts\Detectors\DeviceDetector::class, Detectors\DeviceDetector::class); |
112
|
117 |
|
$this->singleton(Contracts\Detectors\GeoIpDetector::class, Detectors\GeoIpDetector::class); |
113
|
117 |
|
$this->singleton(Contracts\Detectors\LanguageDetector::class, Detectors\LanguageDetector::class); |
114
|
117 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Register the parsers. |
118
|
|
|
*/ |
119
|
|
|
private function registerParsers() |
120
|
|
|
{ |
121
|
117 |
|
$this->singleton(Contracts\Parsers\RefererParser::class, function () { |
122
|
9 |
|
return new Parsers\RefererParser( |
123
|
9 |
|
new \Snowplow\RefererParser\Parser |
124
|
|
|
); |
125
|
117 |
|
}); |
126
|
|
|
|
127
|
117 |
|
$this->singleton(Contracts\Parsers\UserAgentParser::class, function (AppContract $app) { |
128
|
15 |
|
return new Parsers\UserAgentParser( |
129
|
15 |
|
\UAParser\Parser::create(), $app->make('path.base') |
130
|
|
|
); |
131
|
117 |
|
}); |
132
|
117 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Register the trackers. |
136
|
|
|
*/ |
137
|
117 |
|
private function registerTrackers() |
138
|
|
|
{ |
139
|
117 |
|
foreach (Support\BindingManager::getTrackersBindings() as $abstract => $concrete) { |
140
|
117 |
|
$this->singleton($abstract, $concrete); |
141
|
|
|
} |
142
|
117 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Register the main tracker. |
146
|
|
|
*/ |
147
|
117 |
|
private function registerMainTracker() |
148
|
|
|
{ |
149
|
117 |
|
$this->singleton(Contracts\Tracker::class, Tracker::class); |
150
|
117 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Register the exception handler. |
154
|
|
|
*/ |
155
|
117 |
|
private function registerExceptionHandler() |
156
|
|
|
{ |
157
|
117 |
|
$handler = $this->app[ExceptionHandlerContract::class]; |
158
|
|
|
|
159
|
117 |
|
$this->singleton(ExceptionHandlerContract::class, function ($app) use ($handler) { |
160
|
117 |
|
return new Exceptions\Handler($app[Contracts\Tracker::class], $handler); |
161
|
117 |
|
}); |
162
|
117 |
|
} |
163
|
|
|
} |
164
|
|
|
|