Skip to content

Commit 69d6708

Browse files
committed
initial commit of code
1 parent 0a9cdd4 commit 69d6708

26 files changed

+903
-66
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ composer require limenet/laravel-elastica-bridge
1919
You can publish and run the migrations with:
2020

2121
```bash
22-
php artisan vendor:publish --provider="Limenet\LaravelElasticaBridge\LaravelElasticaBridgeServiceProvider" --tag="laravel-elastica-bridge-migrations"
22+
php artisan vendor:publish --provider="Limenet\LaravelElasticaBridge\LaravelElasticaBridgeServiceProvider" --tag="elastica-bridge-migrations"
2323
php artisan migrate
2424
```
2525

2626
You can publish the config file with:
2727
```bash
28-
php artisan vendor:publish --provider="Limenet\LaravelElasticaBridge\LaravelElasticaBridgeServiceProvider" --tag="laravel-elastica-bridge-config"
28+
php artisan vendor:publish --provider="Limenet\LaravelElasticaBridge\LaravelElasticaBridgeServiceProvider" --tag="elastica-bridge-config"
2929
```
3030

3131
This is the contents of the published config file:

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
],
1818
"require": {
1919
"php": "^8.0",
20-
"spatie/laravel-package-tools": "^1.4.3",
21-
"illuminate/contracts": "^8.37"
20+
"illuminate/contracts": "^8.37",
21+
"ruflin/elastica": "^7.1",
22+
"spatie/laravel-package-tools": "^1.4.3"
2223
},
2324
"require-dev": {
2425
"brianium/paratest": "^6.2",

config/elastica-bridge.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
2-
// config for Limenet/ClassName
3-
return [
42

3+
return [
4+
'elasticsearch' => [
5+
'host' => env('ELASTICSEARCH_HOST', 'localhost'),
6+
'port' => env('ELASTICSEARCH_PORT', '9200'),
7+
],
8+
'indices' => [],
59
];

database/factories/.gitkeep

Whitespace-only changes.

database/factories/ModelFactory.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

database/migrations/.gitkeep

Whitespace-only changes.

database/migrations/create_elastica-bridge_table.php.stub

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Client/ElasticaClient.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Limenet\LaravelElasticaBridge\Client;
6+
7+
use Elastica\Client;
8+
use Elastica\Index;
9+
10+
class ElasticaClient
11+
{
12+
protected Client $client;
13+
14+
public function __construct()
15+
{
16+
$this->client = new Client([
17+
'host' => config('elastica-bridge.elasticsearch.host'),
18+
'port' => config('elastica-bridge.elasticsearch.port'),
19+
]);
20+
}
21+
22+
public function getClient(): Client
23+
{
24+
return $this->client;
25+
}
26+
27+
public function getIndex(string $name): Index
28+
{
29+
return $this->client->getIndex($name);
30+
}
31+
}

src/Commands/IndexCommand.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Limenet\LaravelElasticaBridge\Commands;
6+
7+
use Illuminate\Bus\Batch;
8+
use Illuminate\Console\Command;
9+
use Illuminate\Support\Facades\Bus;
10+
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
11+
use Limenet\LaravelElasticaBridge\Jobs\ActivateIndex;
12+
use Limenet\LaravelElasticaBridge\Jobs\PopulateIndex;
13+
use Limenet\LaravelElasticaBridge\Jobs\SetupIndex;
14+
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;
15+
16+
class IndexCommand extends Command
17+
{
18+
/**
19+
* The name and signature of the console command.
20+
*
21+
* @var string
22+
*/
23+
protected $signature = 'elasticsearch:index {index?*} {--delete}';
24+
25+
/**
26+
* The console command description.
27+
*
28+
* @var string
29+
*/
30+
protected $description = 'Re-create the ES index and populate with data';
31+
32+
/**
33+
* Create a new command instance.
34+
*
35+
* @return void
36+
*/
37+
public function __construct(protected ElasticaClient $elastica, protected IndexRepository $indexRepository)
38+
{
39+
parent::__construct();
40+
}
41+
42+
/**
43+
* Execute the console command.
44+
*
45+
* @return int
46+
*/
47+
public function handle()
48+
{
49+
foreach ($this->indexRepository->all() as $indexConfig) {
50+
if (
51+
!empty($this->argument('index'))
52+
&& !in_array($indexConfig->getName(), $this->argument('index'), true)
53+
) {
54+
continue;
55+
}
56+
57+
$this->info(sprintf('Indexing %s', $indexConfig->getName()));
58+
59+
dispatch(new SetupIndex($indexConfig, (bool) $this->option('delete')));
60+
61+
Bus::batch([
62+
[new PopulateIndex($indexConfig)],
63+
])
64+
->then(function (Batch $batch) use ($indexConfig): void {
65+
dispatch(new ActivateIndex($indexConfig));
66+
})
67+
->name('ES index: '.$indexConfig->getName())
68+
->dispatch();
69+
}
70+
71+
return 0;
72+
}
73+
}

src/Commands/LaravelElasticaBridgeCommand.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)