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 3 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.

33 changes: 16 additions & 17 deletions src/Listeners/LogRequest.php → src/LogHandler.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
<?php

namespace Bilfeldt\RequestLogger\Listeners;
namespace Bilfeldt\RequestLogger;

use Bilfeldt\RequestLogger\RequestLoggerFacade;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Jaybizzle\CrawlerDetect\CrawlerDetect;
use Symfony\Component\HttpFoundation\Response;

/**
* This class is highly inspired by the Laravel Telescope Request Watcher.
*
* @see https://github.com/laravel/telescope/blob/master/src/Watchers/RequestWatcher.php
*/
class LogRequest
class LogHandler
{
public function handle(RequestHandled $event)
/**
* Handle a request log
*
* @param \Illuminate\Http\Request $request
* @param \Symfony\Component\HttpFoundation\Response $response
* @return void
*/
public function __invoke(Request $request, Response $response)
{
$startTime = defined('LARAVEL_START') ? LARAVEL_START : $event->request->server('REQUEST_TIME_FLOAT');
$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($event->request, $event->response)) {
$event->request->enableLog();
if ($this->shouldLog($request, $response)) {
$request->enableLog();
}

foreach (array_unique($event->request->attributes->get('log', [])) as $driver) {
foreach (array_unique($request->attributes->get('log', [])) as $driver) {
RequestLoggerFacade::driver($driver)->log(
$event->request,
$event->response,
$request,
$response,
$duration,
$memory
);
Expand Down
4 changes: 2 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 All @@ -40,6 +38,8 @@ public function boot()
]);
}

$this->app->terminating(new LogHandler);

$this->registerMiddlewareAlias();
$this->bootMacros();

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

This file was deleted.