diff --git a/Clockwork/DataSource/LaravelScoutDataSource.php b/Clockwork/DataSource/LaravelScoutDataSource.php new file mode 100644 index 00000000..b3894fbb --- /dev/null +++ b/Clockwork/DataSource/LaravelScoutDataSource.php @@ -0,0 +1,58 @@ +dispatcher = $dispatcher; + + $this->searches = new Timeline; + } + + // Adds rendered searches to the request + public function resolve(Request $request) + { + //$request->searchesData = array_merge($request->searchesData, $this->searches->finalize()); + + return $request; + } + + // Reset the data source to an empty state, clearing any collected data + public function reset() + { + $this->searches = new Timeline; + } + + // Listen to the search events + public function listenToEvents() + { + $this->dispatcher->listen(\Laravel\Scout\Events\ModelsFlushed::class, function ($event) { + $this->searches->event('Flush models from search', [ + 'name' => 'model ' . get_class($event->models[0]), + 'start' => $time = microtime(true), + 'end' => $time, + ]); + }); + + $this->dispatcher->listen(\Laravel\Scout\Events\ModelsImported::class, function ($event) { + $this->searches->event('Import models into search', [ + 'name' => 'model ' . get_class($event->models[0]), + 'start' => $time = microtime(true), + 'end' => $time, + ]); + }); + } +} diff --git a/Clockwork/Support/Laravel/ClockworkServiceProvider.php b/Clockwork/Support/Laravel/ClockworkServiceProvider.php index b35974b6..a2a401fb 100644 --- a/Clockwork/Support/Laravel/ClockworkServiceProvider.php +++ b/Clockwork/Support/Laravel/ClockworkServiceProvider.php @@ -2,11 +2,19 @@ use Clockwork\Clockwork; use Clockwork\Authentication\AuthenticatorInterface; -use Clockwork\DataSource\{ - EloquentDataSource, LaravelCacheDataSource, LaravelDataSource, LaravelEventsDataSource, LaravelHttpClientDataSource, - LaravelNotificationsDataSource, LaravelQueueDataSource, LaravelRedisDataSource, LaravelViewsDataSource, SwiftDataSource, - TwigDataSource, XdebugDataSource -}; +use Clockwork\DataSource\{EloquentDataSource, + LaravelCacheDataSource, + LaravelDataSource, + LaravelEventsDataSource, + LaravelHttpClientDataSource, + LaravelNotificationsDataSource, + LaravelQueueDataSource, + LaravelRedisDataSource, + LaravelScoutDataSource, + LaravelViewsDataSource, + SwiftDataSource, + TwigDataSource, + XdebugDataSource}; use Clockwork\Helpers\StackFilter; use Clockwork\Request\Request; use Clockwork\Storage\StorageInterface; @@ -194,6 +202,10 @@ protected function registerDataSources() return $dataSource; }); + $this->app->singleton('clockwork.scout', function ($app) { + return (new LaravelScoutDataSource($app['events'])); + }); + $this->app->singleton('clockwork.swift', function ($app) { return new SwiftDataSource($app['mailer']->getSwiftMailer()); }); @@ -230,6 +242,7 @@ protected function registerAliases() $this->app->alias('clockwork.notifications', LaravelNotificationsDataSource::class); $this->app->alias('clockwork.queue', LaravelQueueDataSource::class); $this->app->alias('clockwork.redis', LaravelRedisDataSource::class); + $this->app->alias('clockwork.scout', LaravelScoutDataSource::class); $this->app->alias('clockwork.swift', SwiftDataSource::class); $this->app->alias('clockwork.xdebug', XdebugDataSource::class); } diff --git a/Clockwork/Support/Laravel/ClockworkSupport.php b/Clockwork/Support/Laravel/ClockworkSupport.php index 45ceeea9..4dd13990 100644 --- a/Clockwork/Support/Laravel/ClockworkSupport.php +++ b/Clockwork/Support/Laravel/ClockworkSupport.php @@ -142,6 +142,7 @@ public function addDataSources() if ($this->isFeatureEnabled('database')) $clockwork->addDataSource($this->app['clockwork.eloquent']); if ($this->isFeatureEnabled('cache')) $clockwork->addDataSource($this->app['clockwork.cache']); if ($this->isFeatureEnabled('redis')) $clockwork->addDataSource($this->app['clockwork.redis']); + if ($this->isFeatureEnabled('scout')) $clockwork->addDataSource($this->app['clockwork.scout']); if ($this->isFeatureEnabled('queue')) $clockwork->addDataSource($this->app['clockwork.queue']); if ($this->isFeatureEnabled('events')) $clockwork->addDataSource($this->app['clockwork.events']); if ($this->isFeatureEnabled('notifications')) { @@ -170,6 +171,7 @@ public function listenToEvents() if ($this->isFeatureEnabled('cache')) $this->app['clockwork.cache']->listenToEvents(); if ($this->isFeatureEnabled('database')) $this->app['clockwork.eloquent']->listenToEvents(); if ($this->isFeatureEnabled('events')) $this->app['clockwork.events']->listenToEvents(); + if ($this->isFeatureEnabled('scout')) $this->app['clockwork.scout']->listenToEvents(); if ($this->isFeatureEnabled('http_requests')) $this->app['clockwork.http-requests']->listenToEvents(); if ($this->isFeatureEnabled('notifications')) { $this->isFeatureAvailable('notifications-events') diff --git a/Clockwork/Support/Laravel/config/clockwork.php b/Clockwork/Support/Laravel/config/clockwork.php index dc86dc47..0f646877 100644 --- a/Clockwork/Support/Laravel/config/clockwork.php +++ b/Clockwork/Support/Laravel/config/clockwork.php @@ -115,6 +115,11 @@ 'enabled' => env('CLOCKWORK_REDIS_ENABLED', true) ], + // Scout searches + 'scout' => [ + 'enabled' => env('CLOCKWORK_SCOUT_ENABLED', true) + ], + // Routes list 'routes' => [ 'enabled' => env('CLOCKWORK_ROUTES_ENABLED', false),