diff --git a/config/xhprof.php b/config/xhprof.php index a91fd1c..bff7a42 100644 --- a/config/xhprof.php +++ b/config/xhprof.php @@ -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'), ]; diff --git a/src/XhprofServiceProvider.php b/src/XhprofServiceProvider.php index 45513bb..1709fdc 100644 --- a/src/XhprofServiceProvider.php +++ b/src/XhprofServiceProvider.php @@ -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') ); }); } @@ -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; } @@ -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'), ]); } @@ -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; + } + } }