Repository navigation
Expand file tree
/
Copy pathModelObserver.php
More file actions
129 lines (113 loc) · 3.27 KB
/
Copy pathModelObserver.php
File metadata and controls
129 lines (113 loc) · 3.27 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Scout;
use Hyperf\Context\Context;
use Hyperf\Database\Model\Events\Deleted;
use Hyperf\Database\Model\Events\ForceDeleted;
use Hyperf\Database\Model\Events\Restored;
use Hyperf\Database\Model\Events\Saved;
use Hyperf\Database\Model\Model;
use Hyperf\Database\Model\SoftDeletes;
use function Hyperf\Config\config;
use function Hyperf\Support\class_uses_recursive;
class ModelObserver
{
/**
* Enable syncing for the given class.
*/
public static function enableSyncingFor(string $class): void
{
Context::override('syncing_disabled', function ($syncingDisabled) use ($class) {
unset($syncingDisabled[$class]);
return $syncingDisabled;
});
}
/**
* Disable syncing for the given class.
*/
public static function disableSyncingFor(string $class): void
{
Context::override('syncing_disabled', function ($syncingDisabled) use ($class) {
$syncingDisabled[$class] = true;
return $syncingDisabled;
});
}
/**
* Determine if syncing is disabled for the given class or model.
*
* @param object|string $class
*/
public static function syncingDisabledFor($class): bool
{
$class = is_object($class) ? get_class($class) : $class;
$syncingDisabled = (array) Context::get('syncing_disabled', []);
return array_key_exists($class, $syncingDisabled);
}
/**
* Handle the saved event for the model.
*/
public function saved(Saved $event): void
{
/** @var SearchableInterface $model */
$model = $event->getModel();
if (static::syncingDisabledFor($model)) {
return;
}
if (! $model->shouldBeSearchable()) {
$model->unsearchable();
return;
}
$model->searchable();
}
/**
* Handle the deleted event for the model.
*/
public function deleted(Deleted $event)
{
/** @var Model|SearchableInterface $model */
$model = $event->getModel();
if (static::syncingDisabledFor($model)) {
return;
}
if ($this->usesSoftDelete($model) && config('scout.soft_delete', false)) {
$this->saved(new Saved($model));
} else {
$model->unsearchable();
}
}
/**
* Handle the force deleted event for the model.
*/
public function forceDeleted(ForceDeleted $event)
{
/** @var Model|SearchableInterface $model */
$model = $event->getModel();
if (static::syncingDisabledFor($model)) {
return;
}
$model->unsearchable();
}
/**
* Handle the restored event for the model.
*/
public function restored(Restored $event)
{
$model = $event->getModel();
$this->saved(new Saved($model));
}
/**
* Determine if the given model uses soft deletes.
*/
protected function usesSoftDelete(Model $model): bool
{
return in_array(SoftDeletes::class, class_uses_recursive($model));
}
}