22
33namespace DamianTW \MySQLScout \Engines ;
44
5+ use DamianTW \MySQLScout \Engines \Modes \ModeContainer ;
56use Illuminate \Database \Eloquent \Collection ;
67use Laravel \Scout \Builder ;
78use Laravel \Scout \Engines \Engine ;
@@ -11,19 +12,12 @@ class MySQLEngine extends Engine
1112
1213 protected $ mode ;
1314
14- protected $ builder ;
15+ protected $ fallbackMode ;
1516
16- public function setup ( Builder $ builder )
17+ function __construct ( ModeContainer $ modeContainer )
1718 {
18- $ this ->builder = $ builder ;
19-
20- $ mode = __NAMESPACE__ . '\\Modes \\' . studly_case (strtolower (config ('scout.mysql.mode ' )));
21- $ this ->mode = new $ mode ($ this ->builder );
22-
23- if ($ this ->shouldUseFallback ()) {
24- $ mode = __NAMESPACE__ . '\\Modes \\' . studly_case (strtolower (config ('scout.mysql.min_fulltext_search_fallback ' )));
25- $ this ->mode = new $ mode ($ this ->builder );
26- }
19+ $ this ->mode = $ modeContainer ->mode ;
20+ $ this ->fallbackMode = $ modeContainer ->fallbackMode ;
2721 }
2822
2923 public function update ($ models )
@@ -48,30 +42,28 @@ public function search(Builder $builder)
4842
4943 $ result = [];
5044
51- $ this ->setup ($ builder );
52-
53- if ($ this ->shouldNotRun ()) {
45+ if ($ this ->shouldNotRun ($ builder )) {
5446 $ result ['results ' ] = Collection::make ();
5547 $ result ['count ' ] = 0 ;
5648 return $ result ;
5749 }
5850
51+ $ mode = $ this ->shouldUseFallback ($ builder ) ? $ this ->fallbackMode : $ this ->mode ;
5952
60- $ model = $ this ->builder ->model ;
61-
62- $ whereRawString = $ this ->mode ->buildWhereRawString ();
63- $ params = $ this ->mode ->buildParams ();
53+ $ whereRawString = $ mode ->buildWhereRawString ($ builder );
54+ $ params = $ mode ->buildParams ($ builder );
6455
56+ $ model = $ builder ->model ;
6557 $ query = $ model ::whereRaw ($ whereRawString , $ params );
6658
6759 $ result ['count ' ] = $ query ->count ();
6860
69- if ($ this -> builder ->limit ) {
70- $ query = $ query ->take ($ this -> builder ->limit );
61+ if ($ builder ->limit ) {
62+ $ query = $ query ->take ($ builder ->limit );
7163 }
7264
73- if (property_exists ($ this -> builder , 'offset ' ) && $ this -> builder ->offset ) {
74- $ query = $ query ->skip ($ this -> builder ->offset );
65+ if (property_exists ($ builder , 'offset ' ) && $ builder ->offset ) {
66+ $ query = $ query ->skip ($ builder ->offset );
7567 }
7668
7769 $ result ['results ' ] = $ query ->get ();
@@ -121,15 +113,15 @@ public function getTotalCount($results)
121113 return $ results ['count ' ];
122114 }
123115
124- protected function shouldNotRun ()
116+ protected function shouldNotRun ($ builder )
125117 {
126- return strlen ($ this -> builder ->query ) < config ('scout.mysql.min_search_length ' );
118+ return strlen ($ builder ->query ) < config ('scout.mysql.min_search_length ' );
127119 }
128120
129- protected function shouldUseFallback ()
121+ protected function shouldUseFallback ($ builder )
130122 {
131123 return $ this ->mode ->isFullText () &&
132- strlen ($ this -> builder ->query ) < config ('scout.mysql.min_fulltext_search_length ' );
124+ strlen ($ builder ->query ) < config ('scout.mysql.min_fulltext_search_length ' );
133125 }
134126
135127}
0 commit comments