Skip to content

Commit ad90e36

Browse files
committed
✨ Progress bar now display actual progress
🐛 Templates containing `{% cache %}` twig tags are now correctly indexed ♻️ Huge refacto
1 parent 6a4dd52 commit ad90e36

File tree

14 files changed

+489
-283
lines changed

14 files changed

+489
-283
lines changed

src/Elasticsearch.php

+50-40
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@
1818
use craft\events\PluginEvent;
1919
use craft\events\RegisterComponentTypesEvent;
2020
use craft\events\RegisterUrlRulesEvent;
21+
use craft\events\RegisterUserPermissionsEvent;
2122
use craft\services\Plugins;
23+
use craft\services\UserPermissions;
2224
use craft\services\Utilities;
25+
use craft\web\Application;
26+
use craft\web\Controller;
2327
use craft\web\twig\variables\CraftVariable;
2428
use craft\web\UrlManager;
25-
use lhs\elasticsearch\jobs\DeleteElement;
2629
use lhs\elasticsearch\jobs\IndexElement;
27-
use lhs\elasticsearch\jobs\SomeJob;
2830
use lhs\elasticsearch\models\Settings;
31+
use lhs\elasticsearch\services\Elasticsearch as ElasticsearchService;
2932
use lhs\elasticsearch\utilities\ElasticsearchUtilities;
3033
use lhs\elasticsearch\variables\ElasticsearchVariable;
3134
use yii\base\Event;
3235
use yii\elasticsearch\Connection;
36+
use yii\elasticsearch\DebugPanel;
3337

3438
/**
3539
* Craft plugins are very much like little applications in and of themselves. We’ve made
@@ -45,46 +49,36 @@
4549
* @package Elasticsearch
4650
* @since 1.0.0
4751
*
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
5155
* @method Settings getSettings()
5256
*/
5357
class Elasticsearch extends Plugin
5458
{
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';
6660
// Public Methods
6761
// =========================================================================
6862

69-
7063
public function init()
7164
{
7265
parent::init();
7366
$this->name = "Elasticsearch";
74-
self::$plugin = $this;
7567

7668
$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+
],
8882
]);
8983

9084
// Add in our console commands
@@ -125,10 +119,10 @@ function (Event $event) {
125119
if ($element->enabled) {
126120
Craft::$app->queue->push(new IndexElement([
127121
'siteId' => $element->siteId,
128-
'elementId' => $element->id
122+
'elementId' => $element->id,
129123
]));
130124
} else {
131-
Elasticsearch::$plugin->elasticsearch->deleteEntry($element);
125+
ElasticSearch::getInstance()->service->deleteEntry($element);
132126
}
133127

134128
}
@@ -143,7 +137,7 @@ function (Event $event) {
143137
Element::EVENT_AFTER_DELETE,
144138
function (Event $event) {
145139
$element = $event->sender;
146-
Elasticsearch::$plugin->elasticsearch->deleteEntry($element);
140+
ElasticSearch::getInstance()->service->deleteEntry($element);
147141
}
148142
);
149143

@@ -160,20 +154,35 @@ function (RegisterComponentTypesEvent $event) {
160154
Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS,
161155
function (PluginEvent $event) {
162156
if ($event->plugin === $this) {
163-
$this->elasticsearch->reindexAll();
157+
$this->service->reindexAll();
164158
}
165159
}
166160
);
167161

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+
);
173171

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;
174183
Craft::info(
175184
Craft::t(
176-
'elasticsearch',
185+
self::TRANSLATION_CATEGORY,
177186
'{name} plugin loaded',
178187
['name' => $this->name]
179188
),
@@ -207,9 +216,10 @@ protected function settingsHtml(): string
207216
return Craft::$app->view->renderTemplate(
208217
'elasticsearch/settings',
209218
[
210-
'settings' => $this->getSettings()
219+
'settings' => $this->getSettings(),
211220
]
212221
);
213222
}
214223

224+
215225
}

src/assetbundles/elasticsearch/ElasticsearchAsset.php

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace lhs\elasticsearch\assetbundles\Elasticsearch;
1212

13-
use Craft;
1413
use craft\web\AssetBundle;
1514
use craft\web\assets\cp\CpAsset;
1615

0 commit comments

Comments
 (0)