1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/vuongxuongminh/yii2-searchable |
4
|
|
|
* @copyright Copyright (c) 2019 Vuong Xuong Minh |
5
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace vxm\searchable; |
9
|
|
|
|
10
|
|
|
use yii\base\Behavior; |
11
|
|
|
use yii\db\ActiveRecord; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Behavior support syncing record event with indexing data. |
15
|
|
|
* |
16
|
|
|
* @author Vuong Minh <[email protected]> |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
*/ |
19
|
|
|
class SearchableBehavior extends Behavior |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \yii\db\ActiveRecord |
24
|
|
|
* @inheritDoc |
25
|
|
|
*/ |
26
|
|
|
public $owner; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The class names that syncing is disabled for. |
30
|
|
|
* |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
protected static $syncingDisabledFor = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
*/ |
38
|
12 |
|
public function events() |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
12 |
|
ActiveRecord::EVENT_AFTER_INSERT => 'afterSave', |
42
|
|
|
ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave', |
43
|
|
|
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete' |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Handle the saved event for the model. |
49
|
|
|
*/ |
50
|
6 |
|
public function afterSave() |
51
|
|
|
{ |
52
|
6 |
|
$model = $this->owner; |
53
|
|
|
|
54
|
6 |
|
if (static::syncingDisabledFor($model)) { |
55
|
1 |
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
if (!$model->shouldBeSearchable()) { |
59
|
1 |
|
$model->unsearchable(); |
60
|
|
|
|
61
|
1 |
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
$model->searchable(); |
65
|
4 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Handle the deleted event for the model. |
69
|
|
|
*/ |
70
|
3 |
|
public function afterDelete(): void |
71
|
|
|
{ |
72
|
3 |
|
$model = $this->owner; |
73
|
|
|
|
74
|
3 |
|
if (static::syncingDisabledFor($model)) { |
75
|
1 |
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
$model->unsearchable(); |
79
|
2 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Enable syncing for the given class. |
83
|
|
|
* |
84
|
|
|
* @param string $class of records need to enable syncing. |
85
|
|
|
*/ |
86
|
2 |
|
public static function enableSyncingFor($class): void |
87
|
|
|
{ |
88
|
2 |
|
unset(static::$syncingDisabledFor[$class]); |
89
|
2 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Disable syncing for the given class. |
93
|
|
|
* |
94
|
|
|
* @param string $class of records need to disable syncing. |
95
|
|
|
*/ |
96
|
2 |
|
public static function disableSyncingFor($class): void |
97
|
|
|
{ |
98
|
2 |
|
static::$syncingDisabledFor[$class] = true; |
99
|
2 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Determine if syncing is disabled for the given class or model. |
103
|
|
|
* |
104
|
|
|
* @param object|string $class of records need to disable syncing. |
105
|
|
|
* @return bool weather syncing disabled. |
106
|
|
|
*/ |
107
|
6 |
|
public static function syncingDisabledFor($class): bool |
108
|
|
|
{ |
109
|
6 |
|
$class = is_object($class) ? get_class($class) : $class; |
110
|
|
|
|
111
|
6 |
|
return isset(static::$syncingDisabledFor[$class]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|