Skip to content

Commit

Permalink
Adding latest updates to admin bar to make sure that it doesn't mess …
Browse files Browse the repository at this point in the history
…with the logging system
  • Loading branch information
tnylea committed Oct 15, 2024
1 parent ebddc05 commit 7c5f176
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore all .DS_Store files
.DS_Store
**/.DS_Store
4 changes: 3 additions & 1 deletion AdminBarPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function register()

public function boot()
{
$this->app->router->pushMiddlewareToGroup('web', \Wave\Plugins\AdminBar\Http\Middleware\InjectBarMiddleware::class);
if (!app()->runningInConsole() && !app()->environment('testing')) {
$this->app->router->pushMiddlewareToGroup('web', \Wave\Plugins\AdminBar\Http\Middleware\InjectBarMiddleware::class);
}
$this->loadViewsFrom(__DIR__ . '/resources/views', 'admin-bar');
}

Expand Down
Binary file removed resources/.DS_Store
Binary file not shown.
41 changes: 22 additions & 19 deletions src/Http/Middleware/InjectBarMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@

class InjectBarMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (auth()->guest()) {
return $next($request);
} else {
$response = $next($request);

if (!$this->shouldInjectBar($request, $response)) {
return $response;
}

$content = $response->getContent();

if (str_contains($content, '<body')) {
$cornerTabHTML = view('admin-bar::index')->render();

$response = $next($request);
$content = $response->getContent();

// Inject $cornerTabHTML inside the <body> tag
$content = preg_replace('/(<body[^>]*>)(.*?)(<\/body>)/s', '$1$2' . $cornerTabHTML . '$3', $content);
// dd($content);
return $response->setContent($content);
$content = preg_replace('/(<body[^>]*>)(.*?)(<\/body>)/s', '$1$2' . $cornerTabHTML . '$3', $content, 1);
$response->setContent($content);
}

//return $next($request);

return $response;
}

private function shouldInjectBar(Request $request, Response $response): bool
{
return auth()->check()
&& !$request->ajax()
&& $response->headers->get('Content-Type') === 'text/html'
&& $response->getStatusCode() === 200;
}
}
}

0 comments on commit 7c5f176

Please sign in to comment.