-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Scout.php
238 lines (205 loc) · 7.54 KB
/
Scout.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
namespace rias\scout;
use Algolia\AlgoliaSearch\Config\SearchConfig;
use Algolia\AlgoliaSearch\SearchClient;
use Craft;
use craft\base\Element;
use craft\base\Plugin;
use craft\events\DefineBehaviorsEvent;
use craft\events\ElementEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\services\Elements;
use craft\services\Utilities;
use craft\web\twig\variables\CraftVariable;
use Exception;
use Illuminate\Support\Collection;
use rias\scout\behaviors\SearchableBehavior;
use rias\scout\jobs\DeindexElement;
use rias\scout\jobs\IndexElement;
use rias\scout\models\Settings;
use rias\scout\utilities\ScoutUtility;
use rias\scout\variables\ScoutVariable;
use yii\base\Event;
class Scout extends Plugin
{
public const EDITION_STANDARD = 'standard';
public const EDITION_PRO = 'pro';
public static function editions(): array
{
return [
self::EDITION_STANDARD,
self::EDITION_PRO,
];
}
/** @var \rias\scout\Scout */
public static $plugin;
public bool $hasCpSettings = true;
/** @var \Illuminate\Support\Collection */
private $beforeDeleteRelated;
public function init()
{
Craft::$app->onInit(function () {
parent::init();
self::$plugin = $this;
Craft::$container->setSingleton(SearchClient::class, function () {
$config = SearchConfig::create(
self::$plugin->getSettings()->getApplicationId(),
self::$plugin->getSettings()->getAdminApiKey()
);
$config->setConnectTimeout($this->getSettings()->connect_timeout);
return SearchClient::createWithConfig($config);
});
$request = Craft::$app->getRequest();
if ($request->getIsConsoleRequest()) {
$this->controllerNamespace = 'rias\scout\console\controllers\scout';
}
$this->validateConfig();
$this->registerBehaviors();
$this->registerVariables();
$this->registerEventHandlers();
$this->registerUtility();
});
}
protected function createSettingsModel(): Settings
{
return new Settings();
}
public function getSettings(): Settings
{
return parent::getSettings();
}
/** @codeCoverageIgnore */
protected function settingsHtml(): string
{
$overrides = Craft::$app->getConfig()->getConfigFromFile(strtolower($this->handle));
return Craft::$app->getView()->renderTemplate('scout/settings', [
'settings' => $this->getSettings(),
'overrides' => array_keys($overrides),
]);
}
private function registerUtility(): void
{
Event::on(
Utilities::class,
Utilities::EVENT_REGISTER_UTILITY_TYPES,
function (RegisterComponentTypesEvent $event) {
$event->types[] = ScoutUtility::class;
}
);
}
private function registerBehaviors(): void
{
// Register the behavior on the Element class
Event::on(
Element::class,
Element::EVENT_DEFINE_BEHAVIORS,
function (DefineBehaviorsEvent $event) {
$event->behaviors['searchable'] = SearchableBehavior::class;
}
);
}
private function registerVariables(): void
{
// Register our variables
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('scout', ScoutVariable::class);
}
);
}
private function validateConfig(): void
{
$indices = $this->getSettings()->getIndices();
if ($indices->unique('indexName')->count() !== $indices->count()) {
throw new Exception('Index names must be unique in the Scout config.');
}
}
private function registerEventHandlers(): void
{
$events = [
[Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT],
[Elements::class, Elements::EVENT_AFTER_RESTORE_ELEMENT],
[Elements::class, Elements::EVENT_AFTER_UPDATE_SLUG_AND_URI],
];
foreach ($events as $event) {
Event::on(
$event[0],
$event[1],
function (ElementEvent $event) {
/** @var SearchableBehavior $element */
$element = $event->element;
if (!$element->hasMethod('searchable') || !$element->shouldBeSearchable()) {
return;
}
if (Scout::$plugin->getSettings()->queue) {
Craft::$app->getQueue()
->ttr(Scout::$plugin->getSettings()->ttr)
->priority(Scout::$plugin->getSettings()->priority)
->push(
new IndexElement([
'id' => $element->id,
'siteId' => $element->site ? $element->site->id : null
])
);
} else {
$element->searchable();
}
}
);
}
Event::on(
Elements::class,
Elements::EVENT_BEFORE_DELETE_ELEMENT,
function (ElementEvent $event) {
if (!Scout::$plugin->getSettings()->indexRelations) {
$this->beforeDeleteRelated = new Collection();
return;
}
/** @var SearchableBehavior $element */
$element = $event->element;
if (!$element->hasMethod('searchable') || !$element->shouldBeSearchable()) {
return;
}
// Only run this through the queue if the user has that enabled
if (Scout::$plugin->getSettings()->queue) {
Craft::$app->getQueue()
->ttr(Scout::$plugin->getSettings()->ttr)
->priority(Scout::$plugin->getSettings()->priority)
->push(
new DeindexElement([
'id' => $element->id,
'siteId' => $element->site ? $element->site->id : null
])
);
}
}
);
Event::on(
Elements::class,
Elements::EVENT_AFTER_DELETE_ELEMENT,
function (ElementEvent $event) {
// Skip this step if we already ran the DeIndex function earlier
if (Scout::$plugin->getSettings()->queue) {
return;
}
/** @var SearchableBehavior $element */
$element = $event->element;
if ($element->hasMethod('unsearchable')) {
$element->unsearchable();
}
if ($this->beforeDeleteRelated) {
$this->beforeDeleteRelated->each(function (Element $relatedElement) {
/* @var SearchableBehavior $relatedElement */
if ($relatedElement->hasMethod('searchable')) {
$relatedElement->searchable(false);
}
});
}
}
);
}
}