Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix | Replace Listener For Terminatable Middleware #31

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ testbench.yaml
vendor
node_modules
.php-cs-fixer.cache
.phpunit.cache/
26 changes: 0 additions & 26 deletions src/EventServiceProvider.php

This file was deleted.

95 changes: 0 additions & 95 deletions src/Listeners/LogRequest.php

This file was deleted.

83 changes: 83 additions & 0 deletions src/Middleware/LogRequestMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Bilfeldt\RequestLogger\Middleware;

use Bilfeldt\RequestLogger\RequestLoggerFacade;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Response;

class LogRequestMiddleware
{
Expand All @@ -20,4 +23,84 @@ public function handle(Request $request, Closure $next, string ...$drivers)

return $next($request);
}

/**
* Handle tasks after the response has been sent to the browser.
*/
public function terminate(Request $request, Response $response): void
{
$startTime = defined('LARAVEL_START') ? LARAVEL_START : $request->server('REQUEST_TIME_FLOAT');
$duration = $startTime ? floor((microtime(true) - $startTime) * 1000) : null;
$memory = memory_get_peak_usage(true);

if ($this->shouldLog($request, $response)) {
$request->enableLog();
}

foreach (array_unique($request->attributes->get('log', [])) as $driver) {
RequestLoggerFacade::driver($driver)->log(
$request,
$response,
$duration,
$memory
);
}
}

protected function shouldLog(Request $request, Response $response): bool
{
if ($this->ignoredPath($request)) {
return false;
}

if ($this->disabledRobot($request)) {
return false;
}

if ($this->enabledMethod($request)) {
return true;
}

if ($this->enabledStatus($response)) {
return true;
}

return false;
}

protected function ignoredPath(Request $request): bool
{
return $request->is(config('request-logger.ignore_paths', []));
}

protected function disabledRobot(Request $request): bool
{
if (! config('request-logger.disable_robots_tracking')) {
return false;
}

return (new CrawlerDetect())->isCrawler();
}

protected function enabledMethod(Request $request): bool
{
foreach (config('request-logger.log_methods') as $method) {
if ($request->isMethod($method)) {
return true;
}
}

return false;
}

protected function enabledStatus(Response $response): bool
{
foreach (config('request-logger.log_statuses') as $status) {
if (Str::is($status, $response->getStatusCode())) {
return true;
}
}

return false;
}
}
2 changes: 0 additions & 2 deletions src/RequestLoggerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class RequestLoggerServiceProvider extends ServiceProvider
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/request-logger.php', 'request-logger');

$this->app->register(EventServiceProvider::class);
}

/**
Expand Down
21 changes: 0 additions & 21 deletions tests/Feature/LogRequestListenerTest.php

This file was deleted.