File tree Expand file tree Collapse file tree 3 files changed +38
-8
lines changed Expand file tree Collapse file tree 3 files changed +38
-8
lines changed Original file line number Diff line number Diff line change 5
5
use AlgoliaSearch \Client ;
6
6
use Illuminate \Console \Command ;
7
7
use Illuminate \Support \Facades \File ;
8
+ use Laravel \Scout \Searchable ;
8
9
9
10
10
11
class AlgoliaCommand extends Command
11
12
{
13
+ protected $ path ;
14
+
12
15
public function __construct ()
13
16
{
14
17
parent ::__construct ();
15
18
19
+ $ this ->path = resource_path (env ('ALGOLIA_SETTINGS_FOLDER ' , 'algolia-settings/ ' ));
20
+
16
21
// Ensure settings directory exists
17
- if (! File::exists (resource_path ( ' settings/ ' ) )) {
22
+ if (! File::exists ($ this -> path )) {
18
23
File::makeDirectory ();
19
24
}
20
25
}
@@ -26,4 +31,14 @@ protected function getIndex($indexName)
26
31
config ('scout.algolia.secret ' ))
27
32
)->initIndex ($ indexName );
28
33
}
34
+
35
+ protected function isClassSearchable ($ class )
36
+ {
37
+ if (!in_array (Searchable::class, class_uses ($ class ))) {
38
+ $ this ->warn ('The class [ ' . $ class . '] does not use the [ ' . Searchable::class . '] trait ' );
39
+ return false ;
40
+ }
41
+
42
+ return true ;
43
+ }
29
44
}
Original file line number Diff line number Diff line change @@ -29,15 +29,23 @@ public function handle()
29
29
{
30
30
$ class = $ this ->argument ('model ' );
31
31
32
+ if (! $ this ->isClassSearchable ($ class )) {
33
+ return ;
34
+ }
35
+
32
36
$ indexName = (new $ class )->searchableAs ();
33
37
34
38
$ index = $ this ->getIndex ($ indexName );
35
39
36
- File::put (
37
- resource_path ( ' settings/ ' .$ indexName .'json ' ) ,
40
+ $ success = File::put (
41
+ $ this -> path .$ indexName .'. json ' ,
38
42
json_encode ($ index ->getSettings (), JSON_PRETTY_PRINT )
39
43
);
40
44
41
- $ this ->info ('All settings for [ ' .$ class .'] index have been backed up. ' );
45
+ if ($ success ) {
46
+ $ this ->info ('All settings for [ ' .$ class .'] index have been backed up. ' );
47
+ } else {
48
+ $ this ->warn ('The settings could not be saved ' );
49
+ }
42
50
}
43
51
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace Algolia \Settings \Console ;
4
4
5
+ use AlgoliaSearch \Json ;
5
6
use Illuminate \Support \Facades \File ;
6
7
7
8
class PushCommand extends AlgoliaCommand
@@ -29,15 +30,21 @@ public function handle()
29
30
{
30
31
$ class = $ this ->argument ('model ' );
31
32
33
+ if (! $ this ->isClassSearchable ($ class )) {
34
+ return ;
35
+ }
36
+
32
37
$ indexName = (new $ class )->searchableAs ();
33
38
34
39
$ index = $ this ->getIndex ($ indexName );
35
40
36
- $ settings = json_decode (File::get (
37
- resource_path ('settings/ ' .$ indexName .'json ' )
38
- ));
41
+ $ jsonSettings = File::get ($ this ->path .$ indexName .'.json ' );
42
+
43
+ if (! $ jsonSettings ) {
44
+ $ this ->warn ("It seems that you don't have any settings to push for [ $ class]. " );
45
+ }
39
46
40
- $ index ->setSettings ($ settings );
47
+ $ index ->setSettings (Json:: decode ( $ jsonSettings ) );
41
48
42
49
$ this ->info ('All settings for [ ' .$ class .'] index have been pushed. ' );
43
50
}
You can’t perform that action at this time.
0 commit comments