| 199 |
lars |
1 |
<?php namespace Clockwork\DataSource;
|
|
|
2 |
|
|
|
3 |
use Clockwork\DataSource\DataSource;
|
|
|
4 |
use Clockwork\Helpers\Serializer;
|
|
|
5 |
use Clockwork\Request\Log;
|
|
|
6 |
use Clockwork\Request\Request;
|
|
|
7 |
|
|
|
8 |
use Illuminate\Contracts\Foundation\Application;
|
|
|
9 |
use Symfony\Component\HttpFoundation\Response;
|
|
|
10 |
|
|
|
11 |
// Data source for Laravel framework, provides application log, request and response information
|
|
|
12 |
class LaravelDataSource extends DataSource
|
|
|
13 |
{
|
|
|
14 |
// Laravel application instance
|
|
|
15 |
protected $app;
|
|
|
16 |
|
|
|
17 |
// Laravel response instance
|
|
|
18 |
protected $response;
|
|
|
19 |
|
|
|
20 |
// Whether we should collect log messages
|
|
|
21 |
protected $collectLog = true;
|
|
|
22 |
|
|
|
23 |
// Whether we should collect routes
|
|
|
24 |
protected $collectRoutes = false;
|
|
|
25 |
|
|
|
26 |
// Only collect routes from following list of namespaces (collect all if empty)
|
|
|
27 |
protected $routesOnlyNamespaces = [];
|
|
|
28 |
|
|
|
29 |
// Clockwork log instance
|
|
|
30 |
protected $log;
|
|
|
31 |
|
|
|
32 |
// Create a new data source, takes Laravel application instance and additional options as an arguments
|
|
|
33 |
public function __construct(Application $app, $collectLog = true, $collectRoutes = false, $routesOnlyNamespaces = true)
|
|
|
34 |
{
|
|
|
35 |
$this->app = $app;
|
|
|
36 |
|
|
|
37 |
$this->collectLog = $collectLog;
|
|
|
38 |
$this->collectRoutes = $collectRoutes;
|
|
|
39 |
$this->routesOnlyNamespaces = $routesOnlyNamespaces;
|
|
|
40 |
|
|
|
41 |
$this->log = new Log;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
// Adds request, response information, middleware, routes, session data, user and log entries to the request
|
|
|
45 |
public function resolve(Request $request)
|
|
|
46 |
{
|
|
|
47 |
$request->method = $this->getRequestMethod();
|
|
|
48 |
$request->url = $this->getRequestUrl();
|
|
|
49 |
$request->uri = $this->getRequestUri();
|
|
|
50 |
$request->controller = $this->getController();
|
|
|
51 |
$request->headers = $this->getRequestHeaders();
|
|
|
52 |
$request->responseStatus = $this->getResponseStatus();
|
|
|
53 |
$request->middleware = $this->getMiddleware();
|
|
|
54 |
$request->routes = $this->getRoutes();
|
|
|
55 |
$request->sessionData = $this->getSessionData();
|
|
|
56 |
|
|
|
57 |
$this->resolveAuthenticatedUser($request);
|
|
|
58 |
|
|
|
59 |
$request->log()->merge($this->log);
|
|
|
60 |
|
|
|
61 |
return $request;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// Reset the data source to an empty state, clearing any collected data
|
|
|
65 |
public function reset()
|
|
|
66 |
{
|
|
|
67 |
$this->log = new Log;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
// Set Laravel application instance for the current request
|
|
|
71 |
public function setApplication(Application $app)
|
|
|
72 |
{
|
|
|
73 |
$this->app = $app;
|
|
|
74 |
return $this;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
// Set Laravel response instance for the current request
|
|
|
78 |
public function setResponse(Response $response)
|
|
|
79 |
{
|
|
|
80 |
$this->response = $response;
|
|
|
81 |
return $this;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
// Listen for the log events
|
|
|
85 |
public function listenToEvents()
|
|
|
86 |
{
|
|
|
87 |
if (! $this->collectLog) return;
|
|
|
88 |
|
|
|
89 |
if (class_exists(\Illuminate\Log\Events\MessageLogged::class)) {
|
|
|
90 |
// Laravel 5.4
|
|
|
91 |
$this->app['events']->listen(\Illuminate\Log\Events\MessageLogged::class, function ($event) {
|
|
|
92 |
$this->log->log($event->level, $event->message, $event->context);
|
|
|
93 |
});
|
|
|
94 |
} else {
|
|
|
95 |
// Laravel 5.0 to 5.3
|
|
|
96 |
$this->app['events']->listen('illuminate.log', function ($level, $message, $context) {
|
|
|
97 |
$this->log->log($level, $message, $context);
|
|
|
98 |
});
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
// Get a textual representation of the current route's controller
|
|
|
103 |
protected function getController()
|
|
|
104 |
{
|
|
|
105 |
$router = $this->app['router'];
|
|
|
106 |
|
|
|
107 |
$route = $router->current();
|
|
|
108 |
$controller = $route ? $route->getActionName() : null;
|
|
|
109 |
|
|
|
110 |
if ($controller instanceof \Closure) {
|
|
|
111 |
$controller = 'anonymous function';
|
|
|
112 |
} elseif (is_object($controller)) {
|
|
|
113 |
$controller = 'instance of ' . get_class($controller);
|
|
|
114 |
} elseif (is_array($controller) && count($controller) == 2) {
|
|
|
115 |
if (is_object($controller[0])) {
|
|
|
116 |
$controller = get_class($controller[0]) . '->' . $controller[1];
|
|
|
117 |
} else {
|
|
|
118 |
$controller = $controller[0] . '::' . $controller[1];
|
|
|
119 |
}
|
|
|
120 |
} elseif (! is_string($controller)) {
|
|
|
121 |
$controller = null;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
return $controller;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
// Get the request headers
|
|
|
128 |
protected function getRequestHeaders()
|
|
|
129 |
{
|
|
|
130 |
return $this->app['request']->headers->all();
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
// Get the request method
|
|
|
134 |
protected function getRequestMethod()
|
|
|
135 |
{
|
|
|
136 |
return $this->app['request']->getMethod();
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
// Get the request URL
|
|
|
140 |
protected function getRequestUrl()
|
|
|
141 |
{
|
|
|
142 |
return $this->app['request']->fullUrl();
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
// Get the request URI
|
|
|
146 |
protected function getRequestUri()
|
|
|
147 |
{
|
|
|
148 |
return $this->app['request']->getRequestUri();
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
// Get the response status code
|
|
|
152 |
protected function getResponseStatus()
|
|
|
153 |
{
|
|
|
154 |
return $this->response ? $this->response->getStatusCode() : null;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
// Get an array of middleware for the matched route
|
|
|
158 |
protected function getMiddleware()
|
|
|
159 |
{
|
|
|
160 |
$route = $this->app['router']->current();
|
|
|
161 |
|
|
|
162 |
if (! $route) return;
|
|
|
163 |
|
|
|
164 |
return method_exists($route, 'gatherMiddleware') ? $route->gatherMiddleware() : $route->middleware();
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
// Get an array of application routes
|
|
|
168 |
protected function getRoutes()
|
|
|
169 |
{
|
|
|
170 |
if (! $this->collectRoutes) return [];
|
|
|
171 |
|
|
|
172 |
return array_values(array_filter(array_map(function ($route) {
|
|
|
173 |
$action = $route->getActionName() ?: 'anonymous function';
|
|
|
174 |
$namespace = strpos($action, '\\') !== false ? explode('\\', $action)[0] : null;
|
|
|
175 |
|
|
|
176 |
if (count($this->routesOnlyNamespaces) && ! in_array($namespace, $this->routesOnlyNamespaces)) return;
|
|
|
177 |
|
|
|
178 |
return [
|
|
|
179 |
'method' => implode(', ', $route->methods()),
|
|
|
180 |
'uri' => $route->uri(),
|
|
|
181 |
'name' => $route->getName(),
|
|
|
182 |
'action' => $action,
|
|
|
183 |
'middleware' => $route->middleware(),
|
|
|
184 |
'before' => method_exists($route, 'beforeFilters') ? implode(', ', array_keys($route->beforeFilters())) : '',
|
|
|
185 |
'after' => method_exists($route, 'afterFilters') ? implode(', ', array_keys($route->afterFilters())) : ''
|
|
|
186 |
];
|
|
|
187 |
}, $this->app['router']->getRoutes()->getRoutes())));
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
// Get the session data (normalized with removed passwords)
|
|
|
191 |
protected function getSessionData()
|
|
|
192 |
{
|
|
|
193 |
if (! isset($this->app['session'])) return [];
|
|
|
194 |
|
|
|
195 |
return $this->removePasswords((new Serializer)->normalizeEach($this->app['session']->all()));
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
// Add authenticated user data to the request
|
|
|
199 |
protected function resolveAuthenticatedUser(Request $request)
|
|
|
200 |
{
|
|
|
201 |
if (! isset($this->app['auth'])) return;
|
|
|
202 |
if (! ($user = $this->app['auth']->user())) return;
|
|
|
203 |
|
|
|
204 |
if ($user instanceof \Illuminate\Database\Eloquent\Model) {
|
|
|
205 |
// retrieve attributes in this awkward way to make sure we don't trigger exceptions with Eloquent strict mode on
|
|
|
206 |
$keyName = method_exists($user, 'getAuthIdentifierName') ? $user->getAuthIdentifierName() : $user->getKeyName();
|
|
|
207 |
$user = $user->getAttributes();
|
|
|
208 |
|
|
|
209 |
$userId = isset($user[$keyName]) ? $user[$keyName] : null;
|
|
|
210 |
$userEmail = isset($user['email']) ? $user['email'] : $userId;
|
|
|
211 |
$userName = isset($user['name']) ? $user['name'] : null;
|
|
|
212 |
} else {
|
|
|
213 |
$userId = $user->getAuthIdentifier();
|
|
|
214 |
$userEmail = isset($user->email) ? $user->email : $userId;
|
|
|
215 |
$userName = isset($user->name) ? $user->name : null;
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
$request->setAuthenticatedUser($userEmail, $userId, [
|
|
|
219 |
'email' => $userEmail,
|
|
|
220 |
'name' => $userName
|
|
|
221 |
]);
|
|
|
222 |
}
|
|
|
223 |
}
|