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

Add option to disable middleware. When we try to profile some part of… #19

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 config/xhprof.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

return [
'enabled' => (bool) env('XHPROF_ENABLED', false),
'register_middleware' => (bool) env('XHPROF_REGISTER_MIDDLEWARE', true),
'endpoint' => (string) env('PROFILER_ENDPOINT', 'http://127.0.0.1:8000/api/profiler/store'),
];
31 changes: 21 additions & 10 deletions src/XhprofServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/xhprof.php', 'xhprof');

if (! $this->isEnabled()) {
if (!$this->isEnabled()) {
return;
}

$this->registerMiddleware();
if ($this->canRegisterMiddleware()) {
$this->registerMiddleware();
}

$this->app->bind(Profiler::class, function () {
$storage = new WebStorage(
new CurlHttpClient(),
config('xhprof.endpoint'),
new CurlHttpClient(),
config('xhprof.endpoint'),
);

return new Profiler(
$storage,
DriverFactory::createXhrofDriver(),
config('app.name')
$storage,
DriverFactory::createXhrofDriver(),
config('app.name')
);
});
}
Expand All @@ -48,8 +50,8 @@ protected function registerMiddleware(): void
$kernel = $this->app->get(Kernel::class);

if (
method_exists($kernel, 'hasMiddleware')
&& $kernel->hasMiddleware(XhprofProfiler::class)
method_exists($kernel, 'hasMiddleware')
&& $kernel->hasMiddleware(XhprofProfiler::class)
) {
return;
}
Expand All @@ -63,7 +65,7 @@ protected function registerMiddleware(): void
public function boot(): void
{
$this->publishes([
__DIR__.'/../config/xhprof.php' => config_path('xhprof.php'),
__DIR__.'/../config/xhprof.php' => config_path('xhprof.php'),
]);
}

Expand All @@ -82,4 +84,13 @@ private function isEnabled(): bool
return false;
}
}

private function canRegisterMiddleware(): bool
{
try {
return config()->get('xhprof.register_middleware');
} catch (Throwable) {
return true;
}
}
}
Loading