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