1
|
|
|
<?php namespace Arcanedev\LaravelTracker; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelTracker\Contracts\Tracker as TrackerContract; |
4
|
|
|
use Illuminate\Contracts\Foundation\Application; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Tracker |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\LaravelTracker |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Tracker implements TrackerContract |
14
|
|
|
{ |
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
16
|
|
|
| Properties |
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
18
|
|
|
*/ |
19
|
|
|
/** |
20
|
|
|
* The application container. |
21
|
|
|
* |
22
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
23
|
|
|
*/ |
24
|
|
|
protected $app; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The request instance. |
28
|
|
|
* |
29
|
|
|
* @var \Illuminate\Http\Request |
30
|
|
|
*/ |
31
|
|
|
private $request; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $enabled = false; |
37
|
|
|
|
38
|
|
|
/* ------------------------------------------------------------------------------------------------ |
39
|
|
|
| Constructor |
40
|
|
|
| ------------------------------------------------------------------------------------------------ |
41
|
|
|
*/ |
42
|
|
|
/** |
43
|
|
|
* Tracker constructor. |
44
|
|
|
* |
45
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Application $app) |
48
|
|
|
{ |
49
|
|
|
$this->app = $app; |
50
|
|
|
$this->enabled = $this->getConfig('enabled', $this->enabled); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/* ------------------------------------------------------------------------------------------------ |
54
|
|
|
| Getters & Setters |
55
|
|
|
| ------------------------------------------------------------------------------------------------ |
56
|
|
|
*/ |
57
|
|
|
/** |
58
|
|
|
* Get the config repository. |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\Contracts\Config\Repository |
61
|
|
|
*/ |
62
|
|
|
private function config() |
63
|
|
|
{ |
64
|
|
|
return $this->app['config']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the tracker config. |
69
|
|
|
* |
70
|
|
|
* @param string $key |
71
|
|
|
* @param mixed|null $default |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
private function getConfig($key, $default = null) |
76
|
|
|
{ |
77
|
|
|
return $this->config()->get("laravel-tracker.$key", $default); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set the request. |
82
|
|
|
* |
83
|
|
|
* @param \Illuminate\Http\Request $request |
84
|
|
|
* |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
|
|
private function setRequest(Request $request) |
88
|
|
|
{ |
89
|
|
|
$this->request = $request; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/* ------------------------------------------------------------------------------------------------ |
95
|
|
|
| Main Functions |
96
|
|
|
| ------------------------------------------------------------------------------------------------ |
97
|
|
|
*/ |
98
|
|
|
/** |
99
|
|
|
* Start the tracking. |
100
|
|
|
* |
101
|
|
|
* @param \Illuminate\Http\Request $request |
102
|
|
|
*/ |
103
|
|
|
public function track(Request $request) |
104
|
|
|
{ |
105
|
|
|
if ($this->isEnabled()) { |
106
|
|
|
$this->setRequest($request); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Enable the tracker. |
112
|
|
|
*/ |
113
|
|
|
public function enable() |
114
|
|
|
{ |
115
|
|
|
if ( ! $this->isEnabled()) |
116
|
|
|
$this->enabled = true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Disable the tracker. |
121
|
|
|
*/ |
122
|
|
|
public function disable() |
123
|
|
|
{ |
124
|
|
|
if ($this->isEnabled()) |
125
|
|
|
$this->enabled = false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/* ------------------------------------------------------------------------------------------------ |
129
|
|
|
| Check Functions |
130
|
|
|
| ------------------------------------------------------------------------------------------------ |
131
|
|
|
*/ |
132
|
|
|
/** |
133
|
|
|
* Check if the tracker is enabled. |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function isEnabled() |
138
|
|
|
{ |
139
|
|
|
return $this->enabled; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/* ------------------------------------------------------------------------------------------------ |
143
|
|
|
| Other Functions |
144
|
|
|
| ------------------------------------------------------------------------------------------------ |
145
|
|
|
*/ |
146
|
|
|
} |
147
|
|
|
|