diff --git a/src/Console/StatusCommand.php b/src/Console/StatusCommand.php new file mode 100644 index 0000000..5a55a8a --- /dev/null +++ b/src/Console/StatusCommand.php @@ -0,0 +1,158 @@ +getSearchableModels(); + + $this->output->text('🔎 Analysing information from: ['.implode(',', $searchableModels).']'); + $this->output->newLine(); + $this->output->progressStart(count($searchableModels)); + + $headers = ['Searchable', 'Index', 'Indexed Columns', 'Index Records', 'DB Records', 'Records difference']; + $rows = []; + foreach ($searchableModels as $class) { + $model = new $class(); + + $tnt = $this->loadTNTEngine($model); + $indexName = $model->searchableAs().'.index'; + + try { + $tnt->selectIndex($indexName); + $rowsIndexed = $tnt->totalDocumentsInCollection(); + } catch (IndexNotFoundException $e) { + $rowsIndexed = 0; + } + + $indexedColumns = implode(",", array_keys($model->toSearchableArray())); + + $rowsTotal = $model->count(); + $recordsDifference = $rowsTotal - $rowsIndexed; + + if($recordsDifference == 0) { + $recordsDifference = 'Synchronized'; + } else { + $recordsDifference = "$recordsDifference"; + } + + array_push($rows, [$class, $indexName, $indexedColumns, $rowsIndexed, $rowsTotal, $recordsDifference]); + + } + + $this->output->progressFinish(); + $this->output->table($headers, $rows, $tableStyle = 'default', $columnStyles = []); + } + + /** + * @return array + */ + private function getProjectClasses(): array + { + + if (self::$declaredClasses === null) { + $configFiles = Finder::create()->files()->name('*.php')->notName('*.blade.php')->in(app()->path()); + + foreach ($configFiles->files() as $file) { + require_once $file; + } + + self::$declaredClasses = get_declared_classes(); + } + + return self::$declaredClasses; + } + + /** + * @return array|void + */ + private function getSearchableModelsFromClasses($trait = 'Laravel\Scout\Searchable') + { + $projectClasses = $this->getProjectClasses(); + $classes = array_filter( + $projectClasses, + $this->isSearchableModel($trait) + ); + + return $classes; + } + + /** + * @return array + */ + private function getSearchableModels() + { + $searchableModels = (array)$this->argument('model'); + if (empty($searchableModels)) { + $searchableModels = $this->getSearchableModelsFromClasses(); + } + + return $searchableModels; + } + + /** + * @param $trait + * @return \Closure + */ + private function isSearchableModel($trait) + { + return function ($className) use ($trait) { + $traits = class_uses($className); + + return isset($traits[$trait]); + }; + } + + /** + * @param $model + * @return TNTSearch + */ + private function loadTNTEngine($model) + { + $tnt = new TNTSearch(); + + $driver = $model->getConnectionName() ?: config('database.default'); + $config = config('scout.tntsearch') + config("database.connections.$driver"); + $tnt->loadConfig($config); + + return $tnt; + } +} diff --git a/src/TNTSearchScoutServiceProvider.php b/src/TNTSearchScoutServiceProvider.php index 3d24d04..c6f7378 100644 --- a/src/TNTSearchScoutServiceProvider.php +++ b/src/TNTSearchScoutServiceProvider.php @@ -1,5 +1,6 @@ app[EngineManager::class]->extend('tntsearch', function ($app) { $tnt = new TNTSearch(); - + $driver = config('database.default'); $config = config('scout.tntsearch') + config("database.connections.{$driver}"); $tnt->loadConfig($config); $tnt->setDatabaseHandle(app('db')->connection()->getPdo()); - + $this->setFuzziness($tnt); $this->setAsYouType($tnt); @@ -34,6 +35,7 @@ public function boot() if ($this->app->runningInConsole()) { $this->commands([ ImportCommand::class, + StatusCommand::class ]); }