-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from macbookandrew/feature/create-index
Add index:create and index:delete commands
- Loading branch information
Showing
17 changed files
with
599 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dwarf\MeiliTools\Actions; | ||
|
||
use Dwarf\MeiliTools\Contracts\Actions\CreatesIndex; | ||
use Dwarf\MeiliTools\Helpers; | ||
use Laravel\Scout\EngineManager; | ||
|
||
/** | ||
* Create index. | ||
*/ | ||
class CreateIndex implements CreatesIndex | ||
{ | ||
use Concerns\ExtractsIndexInformation; | ||
|
||
/** | ||
* Scout engine manager. | ||
* | ||
* @var \Laravel\Scout\EngineManager | ||
*/ | ||
protected EngineManager $manager; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Laravel\Scout\EngineManager $manager Scout engine manager. | ||
*/ | ||
public function __construct(EngineManager $manager) | ||
{ | ||
$this->manager = $manager; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @throws \Dwarf\MeiliTools\Exceptions\MeiliToolsException When not using the MeiliSearch Scout driver. | ||
* @throws \MeiliSearch\Exceptions\CommunicationException When connection to MeiliSearch fails. | ||
*/ | ||
public function __invoke(string $index, array $options = []): array | ||
{ | ||
Helpers::throwUnlessMeiliSearch(); | ||
$engine = $this->manager->engine(); | ||
$task = $engine->createIndex($index, $options); | ||
$engine->waitForTask($task['taskUid']); | ||
|
||
return $this->getIndexData($engine->getIndex($index)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dwarf\MeiliTools\Actions; | ||
|
||
use Dwarf\MeiliTools\Contracts\Actions\DeletesIndex; | ||
use Dwarf\MeiliTools\Helpers; | ||
use Laravel\Scout\EngineManager; | ||
|
||
/** | ||
* Delete index. | ||
*/ | ||
class DeleteIndex implements DeletesIndex | ||
{ | ||
/** | ||
* Scout engine manager. | ||
* | ||
* @var \Laravel\Scout\EngineManager | ||
*/ | ||
protected EngineManager $manager; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Laravel\Scout\EngineManager $manager Scout engine manager. | ||
*/ | ||
public function __construct(EngineManager $manager) | ||
{ | ||
$this->manager = $manager; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @throws \Dwarf\MeiliTools\Exceptions\MeiliToolsException When not using the MeiliSearch Scout driver. | ||
* @throws \MeiliSearch\Exceptions\CommunicationException When connection to MeiliSearch fails. | ||
* @throws \MeiliSearch\Exceptions\ApiException When index is not found. | ||
*/ | ||
public function __invoke(string $index): void | ||
{ | ||
Helpers::throwUnlessMeiliSearch(); | ||
|
||
$engine = $this->manager->engine(); | ||
$task = $engine->deleteIndex($index); | ||
$engine->waitForTask($task['taskUid']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dwarf\MeiliTools\Console\Commands; | ||
|
||
use Dwarf\MeiliTools\Contracts\Actions\CreatesIndex; | ||
use Illuminate\Console\Command; | ||
|
||
class IndexCreate extends Command | ||
{ | ||
use Concerns\RequiresIndex; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'meili:index:create {index? : Index name}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create a MeiliSearch index'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @param \Dwarf\MeiliTools\Contracts\Actions\CreatesIndex $createIndex | ||
* | ||
* @return int | ||
*/ | ||
public function handle(CreatesIndex $createIndex) | ||
{ | ||
$createIndex($this->getIndex()); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dwarf\MeiliTools\Console\Commands; | ||
|
||
use Dwarf\MeiliTools\Contracts\Actions\DeletesIndex; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Console\ConfirmableTrait; | ||
|
||
class IndexDelete extends Command | ||
{ | ||
use Concerns\RequiresIndex; | ||
use ConfirmableTrait; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'meili:index:delete | ||
{index? : Index name} | ||
{--force : Force the operation to run}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Delete a MeiliSearch index'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @param \Dwarf\MeiliTools\Contracts\Actions\DeletesIndex $deleteIndex | ||
* | ||
* @return int | ||
*/ | ||
public function handle(DeletesIndex $deleteIndex) | ||
{ | ||
// Confirm execution. | ||
$index = $this->getIndex(); | ||
if (!$this->confirmToProceed("Index '{$index}' is about to be deleted", fn () => true)) { | ||
return Command::FAILURE; | ||
} | ||
|
||
$deleteIndex($index); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dwarf\MeiliTools\Contracts\Actions; | ||
|
||
/** | ||
* Creates index. | ||
*/ | ||
interface CreatesIndex | ||
{ | ||
/** | ||
* Create index. | ||
* | ||
* @param string $index Index name. | ||
* @param array $options Index options. | ||
* | ||
* @return array | ||
*/ | ||
public function __invoke(string $index, array $options = []): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dwarf\MeiliTools\Contracts\Actions; | ||
|
||
/** | ||
* Deletes index. | ||
*/ | ||
interface DeletesIndex | ||
{ | ||
/** | ||
* Delete index. | ||
* | ||
* @param string $index Index name. | ||
* | ||
* @return void | ||
*/ | ||
public function __invoke(string $index): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.