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