|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace codemonauts\elastic; |
| 4 | + |
| 5 | +use codemonauts\elastic\jobs\UpdateMapping; |
| 6 | +use codemonauts\elastic\jobs\UpdateSearchIndex; |
| 7 | +use codemonauts\elastic\models\Settings; |
| 8 | +use codemonauts\elastic\services\Elasticsearch; |
| 9 | +use codemonauts\elastic\services\Indexes; |
| 10 | +use codemonauts\elastic\services\Search; |
| 11 | +use codemonauts\elastic\utilities\IndexUtility; |
| 12 | +use Craft; |
| 13 | +use craft\base\ElementInterface; |
| 14 | +use craft\base\Plugin; |
| 15 | +use craft\events\ElementEvent; |
| 16 | +use craft\events\RegisterComponentTypesEvent; |
| 17 | +use craft\helpers\App; |
| 18 | +use craft\helpers\ElementHelper; |
| 19 | +use craft\helpers\UrlHelper; |
| 20 | +use craft\services\Elements; |
| 21 | +use craft\services\Fields; |
| 22 | +use craft\services\Utilities; |
| 23 | + |
| 24 | +/** |
| 25 | + * @property Elasticsearch $elasticsearch |
| 26 | + * @property Indexes $indexes |
| 27 | + * @property Search $search |
| 28 | + */ |
| 29 | +class Elastic extends Plugin |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var Elastic |
| 33 | + */ |
| 34 | + public static $plugin; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Settings |
| 38 | + */ |
| 39 | + public static $settings; |
| 40 | + |
| 41 | + /** |
| 42 | + * @inheritDoc |
| 43 | + */ |
| 44 | + public $hasCpSettings = true; |
| 45 | + |
| 46 | + /** |
| 47 | + * @inheritDoc |
| 48 | + */ |
| 49 | + public function init(): void |
| 50 | + { |
| 51 | + parent::init(); |
| 52 | + |
| 53 | + self::$plugin = $this; |
| 54 | + |
| 55 | + self::$settings = self::$plugin->getSettings(); |
| 56 | + |
| 57 | + // If no endpoint is set, do nothing. |
| 58 | + if (Elastic::env(self::$settings->endpoint) === '') { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // If not in transition mode, replace the Craft internal search component |
| 63 | + if (!self::$settings->transition) { |
| 64 | + $componentConfig = [ |
| 65 | + 'search' => Search::class, |
| 66 | + ]; |
| 67 | + Craft::$app->setComponents($componentConfig); |
| 68 | + } |
| 69 | + |
| 70 | + // Add the plugin search components |
| 71 | + $componentConfig = [ |
| 72 | + 'elasticsearch' => [ |
| 73 | + 'class' => Elasticsearch::class, |
| 74 | + 'hosts' => [ |
| 75 | + Elastic::env(self::$settings->endpoint), |
| 76 | + ], |
| 77 | + 'authentication' => self::$settings->authentication, |
| 78 | + 'username' => Elastic::env(self::$settings->username), |
| 79 | + 'password' => Elastic::env(self::$settings->password), |
| 80 | + 'region' => Elastic::env(self::$settings->region), |
| 81 | + ], |
| 82 | + 'indexes' => [ |
| 83 | + 'class' => Indexes::class, |
| 84 | + 'indexName' => Elastic::env(self::$settings->indexName), |
| 85 | + ], |
| 86 | + 'elements' => services\Elements::class, |
| 87 | + 'search' => Search::class, |
| 88 | + ]; |
| 89 | + $this->setComponents($componentConfig); |
| 90 | + |
| 91 | + // When in transition mode, add event to update Elasticsearch indexes as well. |
| 92 | + if (self::$settings->transition) { |
| 93 | + Craft::$app->elements->on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) { |
| 94 | + /** |
| 95 | + * @var ElementInterface $element |
| 96 | + */ |
| 97 | + $element = $event->element; |
| 98 | + $elementType = get_class($element); |
| 99 | + |
| 100 | + if (ElementHelper::isDraftOrRevision($element)) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (count($element::searchableAttributes()) === 0) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + Craft::$app->getQueue()->push(new UpdateSearchIndex([ |
| 109 | + 'elementType' => $elementType, |
| 110 | + 'elementId' => $element->id, |
| 111 | + ])); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + // Register event when changing field definitions |
| 116 | + Craft::$app->fields->on(Fields::EVENT_AFTER_SAVE_FIELD, function() { |
| 117 | + Craft::$app->queue->push(new UpdateMapping()); |
| 118 | + }); |
| 119 | + |
| 120 | + // Register utilities |
| 121 | + Craft::$app->getUtilities()->on(Utilities::EVENT_REGISTER_UTILITY_TYPES, function(RegisterComponentTypesEvent $event) { |
| 122 | + $event->types[] = IndexUtility::class; |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @inheritDoc |
| 128 | + */ |
| 129 | + public function afterInstall() |
| 130 | + { |
| 131 | + parent::afterInstall(); |
| 132 | + |
| 133 | + if (Craft::$app->getRequest()->getIsConsoleRequest()) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + Craft::$app->getResponse()->redirect( |
| 138 | + UrlHelper::cpUrl('settings/plugins/elastic') |
| 139 | + )->send(); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @inheritDoc |
| 144 | + */ |
| 145 | + protected function createSettingsModel() |
| 146 | + { |
| 147 | + return new Settings(); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @inheritDoc |
| 152 | + */ |
| 153 | + protected function settingsHtml(): ?string |
| 154 | + { |
| 155 | + return Craft::$app->getView()->renderTemplate('elastic/settings', [ |
| 156 | + 'settings' => $this->getSettings(), |
| 157 | + 'authenticationOptions' => [ |
| 158 | + 'aws' => 'AWS', |
| 159 | + 'basicauth' => 'BasicAuth' |
| 160 | + ] |
| 161 | + ] |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Parse environment string. Should be replaced with Craft's App::parseEnv after dropping 3.6 support. |
| 167 | + * |
| 168 | + * @param string|null $str The string to parse. |
| 169 | + * |
| 170 | + * @return array|string|null |
| 171 | + */ |
| 172 | + public static function env(string $str = null) |
| 173 | + { |
| 174 | + if ($str === null) { |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 178 | + if (preg_match('/^\$(\w+)$/', $str, $matches)) { |
| 179 | + $value = App::env($matches[1]); |
| 180 | + if ($value !== false) { |
| 181 | + $str = $value; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return $str; |
| 186 | + } |
| 187 | + |
| 188 | + public function getElasticsearch(): Elasticsearch |
| 189 | + { |
| 190 | + return $this->get('elasticsearch'); |
| 191 | + } |
| 192 | + |
| 193 | + public function getIndexes(): Indexes |
| 194 | + { |
| 195 | + return $this->get('indexes'); |
| 196 | + } |
| 197 | + |
| 198 | + public function getSearch(): Search |
| 199 | + { |
| 200 | + return $this->get('search'); |
| 201 | + } |
| 202 | + |
| 203 | + public function getElements(): services\Elements |
| 204 | + { |
| 205 | + return $this->get('elements'); |
| 206 | + } |
| 207 | +} |
0 commit comments