Skip to content

Commit

Permalink
Add option to disable middleware. When we try to profile some part of…
Browse files Browse the repository at this point in the history
… code.
  • Loading branch information
Artur Melikbekian authored and Artur Melikbekian committed Jun 27, 2024
1 parent 97bd9a3 commit b1f44d9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
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),
'on_startup' => (bool) env('XHPROF_ON_STARTUP', true),
'endpoint' => (string) env('PROFILER_ENDPOINT', 'http://127.0.0.1:8000/api/profiler/store'),
];
32 changes: 22 additions & 10 deletions src/XhprofServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@ public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/xhprof.php', 'xhprof');

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

$this->registerMiddleware();
if ($this->onStartup()) {
$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 +51,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 +66,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 +85,13 @@ private function isEnabled(): bool
return false;
}
}

private function onStartup(): bool
{
try {
return config()->get('xhprof.on_startup');
} catch (Throwable) {
return false;
}
}
}

0 comments on commit b1f44d9

Please sign in to comment.