Skip to content

Commit 2134f22

Browse files
committed
fix commands for lumen (without facades)
1 parent f7aa5a4 commit 2134f22

File tree

5 files changed

+186
-51
lines changed

5 files changed

+186
-51
lines changed

src/Commands/CreateIndexCommand.php

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

33
namespace Basemkhirat\Elasticsearch\Commands;
44

5-
use Basemkhirat\Elasticsearch\Facades\ES;
65
use Illuminate\Console\Command;
76

87
class CreateIndexCommand extends Command
@@ -21,6 +20,21 @@ class CreateIndexCommand extends Command
2120
*/
2221
protected $description = 'Create a new index using defined setting and mapping in config file';
2322

23+
/**
24+
* ES object
25+
* @var object
26+
*/
27+
protected $es;
28+
29+
/**
30+
* CreateIndexCommand constructor.
31+
*/
32+
function __construct()
33+
{
34+
parent::__construct();
35+
$this->es = app("es");
36+
}
37+
2438
/**
2539
* Execute the console command.
2640
*
@@ -31,7 +45,7 @@ public function handle()
3145

3246
$connection = $this->option("connection") ? $this->option("connection") : config("es.default");
3347

34-
$client = ES::connection($connection)->raw();
48+
$client = $this->es->connection($connection)->raw();
3549

3650
$indices = !is_null($this->argument('index')) ?
3751
[$this->argument('index')] :
@@ -68,7 +82,7 @@ public function handle()
6882

6983
if (isset($config['aliases'])) {
7084

71-
foreach($config['aliases'] as $alias) {
85+
foreach ($config['aliases'] as $alias) {
7286

7387
$this->info("Creating alias: {$alias} for index: {$index}");
7488

@@ -112,8 +126,6 @@ public function handle()
112126
}
113127

114128

115-
116-
117129
}
118130

119131
}

src/Commands/DropIndexCommand.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Basemkhirat\Elasticsearch\Commands;
44

5-
use Basemkhirat\Elasticsearch\Facades\ES;
65
use Illuminate\Console\Command;
76

87
class DropIndexCommand extends Command
@@ -23,6 +22,21 @@ class DropIndexCommand extends Command
2322
*/
2423
protected $description = 'Drop an index';
2524

25+
/**
26+
* ES object
27+
* @var object
28+
*/
29+
protected $es;
30+
31+
/**
32+
* DropIndexCommand constructor.
33+
*/
34+
function __construct()
35+
{
36+
parent::__construct();
37+
$this->es = app("es");
38+
}
39+
2640
/**
2741
* Execute the console command.
2842
*
@@ -34,7 +48,7 @@ public function handle()
3448
$connection = $this->option("connection") ? $this->option("connection") : config("es.default");
3549
$force = $this->option("force") ? $this->option("force") : 0;
3650

37-
$client = ES::connection($connection)->raw();
51+
$client = $this->es->connection($connection)->raw();
3852

3953
$indices = !is_null($this->argument('index')) ?
4054
[$this->argument('index')] :
@@ -47,7 +61,7 @@ public function handle()
4761
continue;
4862
}
4963

50-
if($force or $this->confirm("Are you sure to drop \"$index\" index")) {
64+
if ($force or $this->confirm("Are you sure to drop \"$index\" index")) {
5165

5266
$this->info("Dropping index: {$index}");
5367

src/Commands/ListIndicesCommand.php

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Illuminate\Console\Command;
66

7-
use Basemkhirat\Elasticsearch\Facades\ES;
8-
7+
/**
8+
* Class ListIndicesCommand
9+
* @package Basemkhirat\Elasticsearch\Commands
10+
*/
911
class ListIndicesCommand extends Command
1012
{
1113
/**
@@ -28,6 +30,20 @@ class ListIndicesCommand extends Command
2830
*/
2931
protected $headers = ["configured (es.php)", "health", "status", "index", "uuid", "pri", "rep", "docs.count", "docs.deleted", "store.size", "pri.store.size"];
3032

33+
/**
34+
* ES object
35+
* @var object
36+
*/
37+
protected $es;
38+
39+
/**
40+
* ListIndicesCommand constructor.
41+
*/
42+
function __construct()
43+
{
44+
parent::__construct();
45+
$this->es = app("es");
46+
}
3147

3248
/**
3349
* Execute the console command.
@@ -39,48 +55,78 @@ public function handle()
3955

4056
$connection = $this->option("connection") ? $this->option("connection") : config("es.default");
4157

42-
$indices = ES::connection($connection)->raw()->cat()->indices();
43-
44-
if($indices != "") {
45-
46-
$this->table($this->headers, $this->getIndices($indices));
58+
$indices = $this->es->connection($connection)->raw()->cat()->indices();
4759

60+
if (is_array($indices)) {
61+
$indices = $this->getIndicesFromArrayResponse($indices);
4862
} else {
63+
$indices = $this->getIndicesFromStringResponse($indices);
64+
}
4965

66+
if (count($indices)) {
67+
$this->table($this->headers, $indices);
68+
} else {
5069
$this->warn('No indices found.');
70+
}
71+
72+
}
73+
74+
75+
/**
76+
* Get a list of indices data
77+
* Match newer versions of elasticsearch/elasticsearch package (5.1.1 or higher)
78+
* @param $indices
79+
* @return array
80+
*/
81+
function getIndicesFromArrayResponse($indices)
82+
{
83+
84+
foreach ($indices as $row) {
85+
86+
if (in_array($row['index'], array_keys(config("es.indices")))) {
87+
$row = array_prepend($row, "yes");
88+
} else {
89+
$row = array_prepend($row, "no");
90+
}
91+
92+
$data[] = $row;
5193

5294
}
5395

96+
return $data;
97+
5498
}
5599

56100
/**
57-
* Convert lines into array
101+
* Get list of indices data
102+
* Match older versions of elasticsearch/elasticsearch package.
58103
* @param $indices
59104
* @return array
60105
*/
61-
function getIndices($indices){
106+
function getIndicesFromStringResponse($indices)
107+
{
62108

63109
$lines = explode("\n", trim($indices));
64110

65111
$data = [];
66112

67-
foreach ($lines as $line){
113+
foreach ($lines as $line) {
68114

69115
$row = [];
70116

71117
$line_array = explode(" ", trim($line));
72118

73-
foreach ($line_array as $item){
119+
foreach ($line_array as $item) {
74120

75-
if(trim($item) != ""){
121+
if (trim($item) != "") {
76122
$row[] = $item;
77123
}
78124

79125
}
80126

81-
if(in_array($row[2], array_keys(config("es.indices")))){
127+
if (in_array($row[2], array_keys(config("es.indices")))) {
82128
$row = array_prepend($row, "yes");
83-
}else{
129+
} else {
84130
$row = array_prepend($row, "no");
85131
}
86132

0 commit comments

Comments
 (0)