forked from bimthebam/silverstripe-meilisearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RebuildAllIndexesTask.php
59 lines (50 loc) · 1.35 KB
/
RebuildAllIndexesTask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace BimTheBam\Meilisearch\Dev\Task;
use BimTheBam\Meilisearch\Index;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Versioned\Versioned;
use Throwable;
/**
* Class RebuildAllIndexesTask
* @package BimTheBam\Meilisearch\Dev\Task
*/
class RebuildAllIndexesTask extends BuildTask
{
/**
* @var string
*/
private static string $segment = 'meilisearch-rebuild-all-indexes';
/**
* @var string
*/
protected $title = 'Rebuild all meilisearch indexes';
/**
* @var string
*/
protected $description = '';
/**
* @param $request
* @return void
* @throws NotFoundExceptionInterface
* @throws ReflectionException
* @throws Throwable
*/
public function run($request): void
{
$stage = $request->getVar('stage');
Versioned::withVersionedMode(function () use ($stage) {
if ($stage) {
Versioned::set_stage($stage);
}
foreach (ClassInfo::subclassesFor(Index::class, false) as $indexClass) {
/** @var Index $index */
$index = Injector::inst()->create($indexClass);
$index->rebuild();
}
});
}
}