Skip to content

Commit

Permalink
fix: fix dynamic properties deprecation notices, fixes teamtnt#359
Browse files Browse the repository at this point in the history
  • Loading branch information
tasinttttttt committed Apr 24, 2024
1 parent 56af782 commit 0176264
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 22 deletions.
10 changes: 5 additions & 5 deletions src/Console/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Contracts\Events\Dispatcher;
use TeamTNT\TNTSearch\TNTSearch;
use TeamTNT\Scout\ExtendedTNTSearch as TNTSearch;
use Illuminate\Support\Facades\Schema;

class ImportCommand extends Command
Expand Down Expand Up @@ -43,12 +43,12 @@ public function handle(Dispatcher $events)
$tnt->loadConfig($config);
$tnt->setDatabaseHandle($db->getPdo());

if(!$model->count()) {
if (!$model->count()) {
$this->info('Nothing to import.');
exit(0);
}
$indexer = $tnt->createIndex($model->searchableAs().'.index');

$indexer = $tnt->createIndex($model->searchableAs() . '.index');
$indexer->setPrimaryKey($model->getKeyName());

$availableColumns = Schema::connection($driver)->getColumnListing($model->getTable());
Expand All @@ -74,6 +74,6 @@ public function handle(Dispatcher $events)
$indexer->query($sql);

$indexer->run();
$this->info('All ['.$class.'] records have been imported.');
$this->info('All [' . $class . '] records have been imported.');
}
}
9 changes: 4 additions & 5 deletions src/Console/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Component\Finder\Finder;
use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException;
use TeamTNT\TNTSearch\TNTSearch;
use TeamTNT\Scout\ExtendedTNTSearch as TNTSearch;

class StatusCommand extends Command
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public function handle(Dispatcher $events)

$searchableModels = $this->getSearchableModels();

$this->output->text('🔎 Analysing information from: <info>['.implode(',', $searchableModels).']</info>');
$this->output->text('🔎 Analysing information from: <info>[' . implode(',', $searchableModels) . ']</info>');
$this->output->newLine();
$this->output->progressStart(count($searchableModels));

Expand All @@ -52,7 +52,7 @@ public function handle(Dispatcher $events)
$model = new $class();

$tnt = $this->loadTNTEngine($model);
$indexName = $model->searchableAs().'.index';
$indexName = $model->searchableAs() . '.index';

try {
$tnt->selectIndex($indexName);
Expand All @@ -66,14 +66,13 @@ public function handle(Dispatcher $events)

$indexedColumns = $rowsTotal ? implode(",", array_keys($model->first()->toSearchableArray())) : '';

if($recordsDifference == 0) {
if ($recordsDifference == 0) {
$recordsDifference = '<fg=green>Synchronized</>';
} else {
$recordsDifference = "<fg=red>$recordsDifference</>";
}

array_push($rows, [$class, $indexName, $indexedColumns, $rowsIndexed, $rowsTotal, $recordsDifference]);

}

$this->output->progressFinish();
Expand Down
10 changes: 10 additions & 0 deletions src/Engines/ExtendedScoutBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace TeamTNT\Scout\Engines;

use Laravel\Scout\Builder;

class ExtendedScoutBuilder extends Builder
{
public $constraints;
}
15 changes: 9 additions & 6 deletions src/Engines/TNTSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
use Illuminate\Support\LazyCollection;
use InvalidArgumentException;
use Laravel\Scout\Builder;
use TeamTNT\Scout\Engines\ExtendedScoutBuilder;
use Laravel\Scout\Engines\Engine;
use TeamTNT\Scout\Events\SearchPerformed;
use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException;
use TeamTNT\TNTSearch\TNTSearch;
use TeamTNT\Scout\ExtendedTNTSearch as TNTSearch;

class TNTSearchEngine extends Engine
{
Expand All @@ -24,7 +25,7 @@ class TNTSearchEngine extends Engine
protected $tnt;

/**
* @var Builder
* @var ExtendedScoutBuilder
*/
protected $builder;

Expand Down Expand Up @@ -176,7 +177,6 @@ protected function performSearch(Builder $builder, array $options = [])
$res = $this->tnt->searchBoolean($builder->query, $limit);
event(new SearchPerformed($builder, $res, true));
return $res;

} else {
$res = $this->tnt->search($builder->query, $limit);
event(new SearchPerformed($builder, $res));
Expand Down Expand Up @@ -207,7 +207,8 @@ public function map(Builder $builder, $results, $model)
}

$models = $builder->whereIn(
$model->getQualifiedKeyName(), $keys
$model->getQualifiedKeyName(),
$keys
)->get()->keyBy($model->getKeyName());

// sort models by user choice
Expand Down Expand Up @@ -250,7 +251,8 @@ public function lazyMap(Builder $builder, $results, $model)
}

$models = $builder->whereIn(
$model->getQualifiedKeyName(), $keys
$model->getQualifiedKeyName(),
$keys
)->get()->keyBy($model->getKeyName());

// sort models by user choice
Expand Down Expand Up @@ -351,7 +353,8 @@ private function discardIdsFromResultSetByConstraints($builder, $searchResults)
$subQualifiedKeyName = 'sub.' . $builder->model->getKeyName(); // sub.id

$sub = $this->getBuilder($builder->model)->whereIn(
$qualifiedKeyName, $searchResults
$qualifiedKeyName,
$searchResults
); // sub query for left join

$discardIds = $builder->model->newQuery()
Expand Down
9 changes: 9 additions & 0 deletions src/Events/SearchPerformed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

class SearchPerformed
{
public $query;
public $isBooleanSearch;
public $indexName;
public $model;
public $ids;
public $execution_time;
public $driver;
public $hits;

public function __construct($builder, $results, $isBooleanSearch = false)
{
$this->query = $builder->query;
Expand Down
12 changes: 12 additions & 0 deletions src/ExtendedTNTSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace TeamTNT\Scout;

use TeamTNT\TNTSearch\TNTSearch;

class ExtendedTNTSearch extends TNTSearch
{
public $engine;
public $maxDocs;
public $asYouType;
}
8 changes: 6 additions & 2 deletions src/TNTSearchScoutServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?php namespace TeamTNT\Scout;
<?php

namespace TeamTNT\Scout;

use Illuminate\Support\ServiceProvider;
use Laravel\Scout\Builder;
use Laravel\Scout\EngineManager;
use TeamTNT\Scout\Console\ImportCommand;
use TeamTNT\Scout\Console\StatusCommand;
use TeamTNT\Scout\Engines\TNTSearchEngine;
use TeamTNT\TNTSearch\TNTSearch;
use TeamTNT\Scout\ExtendedTNTSearch as TNTSearch;

class TNTSearchScoutServiceProvider extends ServiceProvider
{
public $constraints;

/**
* Bootstrap any application services.
*
Expand Down
7 changes: 3 additions & 4 deletions tests/TNTSearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Mockery as m;
use PHPUnit\Framework\TestCase;
use TeamTNT\Scout\Engines\TNTSearchEngine;
use TeamTNT\TNTSearch\TNTSearch;
use TeamTNT\Scout\ExtendedTNTSearch as TNTSearch;

class TNTSearchEngineTest extends TestCase
{
Expand All @@ -20,11 +20,10 @@ public function testApplyingFilters()
$engine = new TeamTNT\Scout\Engines\TNTSearchEngine($tnt);

$engine->addFilter("query_expansion", function ($query, $model) {
if ($query == "test" && $model == "TeamTNT\TNTSearch\TNTSearch") {
return "modified-".$query;
if ($query == "test" && $model == "TeamTNT\Scout\ExtendedTNTSearch") {
return "modified-" . $query;
}
return $query;

});

$query = $engine->applyFilters('query_expansion', "test", TNTSearch::class);
Expand Down

0 comments on commit 0176264

Please sign in to comment.