From b1f44d9f081c3deef48f2b81d54aec58aaddda08 Mon Sep 17 00:00:00 2001 From: Artur Melikbekian Date: Thu, 27 Jun 2024 15:56:49 +0300 Subject: [PATCH 1/2] Add option to disable middleware. When we try to profile some part of code. --- config/xhprof.php | 1 + src/XhprofServiceProvider.php | 32 ++++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/config/xhprof.php b/config/xhprof.php index a91fd1c..5c607ba 100644 --- a/config/xhprof.php +++ b/config/xhprof.php @@ -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'), ]; diff --git a/src/XhprofServiceProvider.php b/src/XhprofServiceProvider.php index 45513bb..3ffb799 100644 --- a/src/XhprofServiceProvider.php +++ b/src/XhprofServiceProvider.php @@ -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') ); }); } @@ -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; } @@ -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'), ]); } @@ -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; + } + } } From 65b7264d25348bee0b813afbfb3bfa89d1d79afe Mon Sep 17 00:00:00 2001 From: Artur Melikbekian Date: Tue, 9 Jul 2024 10:43:11 +0300 Subject: [PATCH 2/2] Add option to disable middleware. When we try to profile some part of code. --- config/xhprof.php | 2 +- src/XhprofServiceProvider.php | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/config/xhprof.php b/config/xhprof.php index 5c607ba..bff7a42 100644 --- a/config/xhprof.php +++ b/config/xhprof.php @@ -2,6 +2,6 @@ return [ 'enabled' => (bool) env('XHPROF_ENABLED', false), - 'on_startup' => (bool) env('XHPROF_ON_STARTUP', true), + 'register_middleware' => (bool) env('XHPROF_REGISTER_MIDDLEWARE', true), 'endpoint' => (string) env('PROFILER_ENDPOINT', 'http://127.0.0.1:8000/api/profiler/store'), ]; diff --git a/src/XhprofServiceProvider.php b/src/XhprofServiceProvider.php index 3ffb799..1709fdc 100644 --- a/src/XhprofServiceProvider.php +++ b/src/XhprofServiceProvider.php @@ -21,11 +21,10 @@ public function register(): void return; } - if ($this->onStartup()) { + if ($this->canRegisterMiddleware()) { $this->registerMiddleware(); } - $this->app->bind(Profiler::class, function () { $storage = new WebStorage( new CurlHttpClient(), @@ -86,12 +85,12 @@ private function isEnabled(): bool } } - private function onStartup(): bool + private function canRegisterMiddleware(): bool { try { - return config()->get('xhprof.on_startup'); + return config()->get('xhprof.register_middleware'); } catch (Throwable) { - return false; + return true; } } }