18
18
use craft \events \PluginEvent ;
19
19
use craft \events \RegisterComponentTypesEvent ;
20
20
use craft \events \RegisterUrlRulesEvent ;
21
+ use craft \events \RegisterUserPermissionsEvent ;
21
22
use craft \services \Plugins ;
23
+ use craft \services \UserPermissions ;
22
24
use craft \services \Utilities ;
25
+ use craft \web \Application ;
26
+ use craft \web \Controller ;
23
27
use craft \web \twig \variables \CraftVariable ;
24
28
use craft \web \UrlManager ;
25
- use lhs \elasticsearch \jobs \DeleteElement ;
26
29
use lhs \elasticsearch \jobs \IndexElement ;
27
- use lhs \elasticsearch \jobs \SomeJob ;
28
30
use lhs \elasticsearch \models \Settings ;
31
+ use lhs \elasticsearch \services \Elasticsearch as ElasticsearchService ;
29
32
use lhs \elasticsearch \utilities \ElasticsearchUtilities ;
30
33
use lhs \elasticsearch \variables \ElasticsearchVariable ;
31
34
use yii \base \Event ;
32
35
use yii \elasticsearch \Connection ;
36
+ use yii \elasticsearch \DebugPanel ;
33
37
34
38
/**
35
39
* Craft plugins are very much like little applications in and of themselves. We’ve made
45
49
* @package Elasticsearch
46
50
* @since 1.0.0
47
51
*
48
- * @property Elasticsearch $elasticsearch
49
- * @property Settings $ settings
50
- * @property Connection $connection
52
+ * @property services\ Elasticsearch service
53
+ * @property Settings settings
54
+ * @property Connection elasticsearch
51
55
* @method Settings getSettings()
52
56
*/
53
57
class Elasticsearch extends Plugin
54
58
{
55
- /**
56
- * Static property that is an instance of this plugin class so that it can be accessed via
57
- * Elasticsearch::$plugin
58
- *
59
- * @var Elasticsearch
60
- */
61
- public static $ plugin ;
62
-
63
- // Static Properties
64
- // =========================================================================
65
-
59
+ const TRANSLATION_CATEGORY = 'elasticsearch ' ;
66
60
// Public Methods
67
61
// =========================================================================
68
62
69
-
70
63
public function init ()
71
64
{
72
65
parent ::init ();
73
66
$ this ->name = "Elasticsearch " ;
74
- self ::$ plugin = $ this ;
75
67
76
68
$ this ->setComponents ([
77
- 'connection ' => [
78
- 'class ' => 'yii\elasticsearch\Connection ' ,
79
- 'nodes ' => [
80
- ['http_address ' => $ this ->settings ->http_address ],
81
- // configure more hosts if you have a cluster
82
- ],
83
- 'auth ' => [
84
- 'username ' => $ this ->settings ->auth_username ,
85
- 'password ' => $ this ->settings ->auth_password
86
- ]
87
- ]
69
+ 'service ' => ElasticsearchService::class,
70
+ ]);
71
+
72
+ Craft::$ app ->set ('elasticsearch ' , [
73
+ 'class ' => 'yii\elasticsearch\Connection ' ,
74
+ 'nodes ' => [
75
+ ['http_address ' => $ this ->settings ->http_address ],
76
+ // configure more hosts if you have a cluster
77
+ ],
78
+ 'auth ' => [
79
+ 'username ' => $ this ->settings ->auth_username ,
80
+ 'password ' => $ this ->settings ->auth_password ,
81
+ ],
88
82
]);
89
83
90
84
// Add in our console commands
@@ -125,10 +119,10 @@ function (Event $event) {
125
119
if ($ element ->enabled ) {
126
120
Craft::$ app ->queue ->push (new IndexElement ([
127
121
'siteId ' => $ element ->siteId ,
128
- 'elementId ' => $ element ->id
122
+ 'elementId ' => $ element ->id ,
129
123
]));
130
124
} else {
131
- Elasticsearch:: $ plugin -> elasticsearch ->deleteEntry ($ element );
125
+ ElasticSearch:: getInstance ()-> service ->deleteEntry ($ element );
132
126
}
133
127
134
128
}
@@ -143,7 +137,7 @@ function (Event $event) {
143
137
Element::EVENT_AFTER_DELETE ,
144
138
function (Event $ event ) {
145
139
$ element = $ event ->sender ;
146
- Elasticsearch:: $ plugin -> elasticsearch ->deleteEntry ($ element );
140
+ ElasticSearch:: getInstance ()-> service ->deleteEntry ($ element );
147
141
}
148
142
);
149
143
@@ -160,20 +154,35 @@ function (RegisterComponentTypesEvent $event) {
160
154
Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS ,
161
155
function (PluginEvent $ event ) {
162
156
if ($ event ->plugin === $ this ) {
163
- $ this ->elasticsearch ->reindexAll ();
157
+ $ this ->service ->reindexAll ();
164
158
}
165
159
}
166
160
);
167
161
168
- // Event::on(UserPermissions::class, UserPermissions::EVENT_REGISTER_PERMISSIONS, function(RegisterUserPermissionsEvent $event) {
169
- // $event->permissions[\Craft::t('elasticsearch', 'Elasticsearch')] = [
170
- // 'some-thing' => ['label' => \Craft::t('elasticsearch', 'Something')],
171
- // ];
172
- // });
162
+ Event::on (
163
+ UserPermissions::class,
164
+ UserPermissions::EVENT_REGISTER_PERMISSIONS ,
165
+ function (RegisterUserPermissionsEvent $ event ) {
166
+ $ event ->permissions [Craft::t (self ::TRANSLATION_CATEGORY , 'Elasticsearch ' )] = [
167
+ 'reindex ' => ['label ' => Craft::t (self ::TRANSLATION_CATEGORY , 'Refresh ElasticSearch index ' )],
168
+ ];
169
+ }
170
+ );
173
171
172
+ Event::on (
173
+ Application::class,
174
+ Application::EVENT_BEFORE_REQUEST ,
175
+ function () {
176
+ /** @var \yii\debug\Module */
177
+ $ debugModule = Craft::$ app ->getModule ('debug ' );
178
+
179
+ $ debugModule ->panels ['elasticsearch ' ] = new DebugPanel (['module ' => $ debugModule ]);
180
+ }
181
+ );
182
+ Controller::EVENT_BEFORE_ACTION ;
174
183
Craft::info (
175
184
Craft::t (
176
- ' elasticsearch ' ,
185
+ self :: TRANSLATION_CATEGORY ,
177
186
'{name} plugin loaded ' ,
178
187
['name ' => $ this ->name ]
179
188
),
@@ -207,9 +216,10 @@ protected function settingsHtml(): string
207
216
return Craft::$ app ->view ->renderTemplate (
208
217
'elasticsearch/settings ' ,
209
218
[
210
- 'settings ' => $ this ->getSettings ()
219
+ 'settings ' => $ this ->getSettings (),
211
220
]
212
221
);
213
222
}
214
223
224
+
215
225
}
0 commit comments