Skip to content

Commit b2bfe8d

Browse files
committed
Add multilingual importing support
1 parent 4ec0644 commit b2bfe8d

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/Console/AbstractCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ protected function getModelArgument()
6060
}, $models);
6161
}
6262

63+
/**
64+
* Get locale option.
65+
*
66+
* @return array
67+
*/
68+
protected function getLocaleOption()
69+
{
70+
return array_filter(explode(',', preg_replace('/\s+/', '', $this->option('locales'))));
71+
}
72+
6373
/**
6474
* Validate model.
6575
*

src/Console/ImportCommand.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace LaravelHunt\Console;
44

5-
use Illuminate\Database\Eloquent\Model;
6-
75
class ImportCommand extends AbstractCommand
86
{
97
/**
@@ -12,7 +10,8 @@ class ImportCommand extends AbstractCommand
1210
* @var string
1311
*/
1412
protected $signature = 'hunt:import
15-
{model : Name or comma separated names of the model(s) to index}';
13+
{model : Name or comma separated names of the model(s) to index}
14+
{--l|locales= : Single or comma separated locales to index}';
1615

1716
/**
1817
* The console command description.
@@ -28,9 +27,18 @@ class ImportCommand extends AbstractCommand
2827
*/
2928
public function handle()
3029
{
30+
$locales = $this->getLocaleOption();
31+
3132
foreach ($this->getModelArgument() as $model) {
3233
if ($model = $this->validateModel($model)) {
33-
$this->index($model);
34+
if (empty($locales) === false) {
35+
foreach ($locales as $locale) {
36+
$this->index($model, $locale);
37+
}
38+
}
39+
else {
40+
$this->index($model);
41+
}
3442
}
3543
}
3644
}
@@ -39,10 +47,11 @@ public function handle()
3947
* Index all model entries to ElasticSearch.
4048
*
4149
* @param string $model
50+
* @param string $locale
4251
*
4352
* @return bool
4453
*/
45-
protected function index($model)
54+
protected function index($model, $locale = '')
4655
{
4756
$this->comment("Processing [{$model}]");
4857

@@ -55,6 +64,11 @@ protected function index($model)
5564
$this->hunter->putMapping($instance);
5665
}
5766

67+
// Get entries by a specific locale
68+
if ($locale && ($field = $this->hunter->config('locale_field'))) {
69+
$instance->where($field, $locale);
70+
}
71+
5872
// Index model
5973
$this->line(' - Importing');
6074

0 commit comments

Comments
 (0)