-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alban Jubert
committed
Feb 27, 2018
0 parents
commit 9c6867f
Showing
30 changed files
with
1,681 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"pluginName":"Elasticsearch","pluginDescription":"Bring the power of Elasticsearch to you Craft 3 CMS project","pluginVersion":"1.0.0","pluginAuthorName":"La Haute Société","pluginVendorName":"La Haute Société","pluginAuthorUrl":"https://www.lahautesociete.com","pluginAuthorGithub":"juban","codeComments":"codeComments","pluginComponents":["jobs","tasks","consolecommands","controllers","cpsection","models","records","services","settings","variables"],"consolecommandName":"Elasticsearch","controllerName":"Elasticsearch","cpsectionName":"Elasticsearch","modelName":"Elasticsearch","recordName":"Elasticsearch","serviceName":"Elasticsearch","apiVersion":"api_version_3_0"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# CRAFT ENVIRONMENT | ||
.env.php | ||
.env.sh | ||
.env | ||
|
||
# COMPOSER | ||
/vendor | ||
|
||
# BUILD FILES | ||
/bower_components/* | ||
/node_modules/* | ||
/build/* | ||
/yarn-error.log | ||
|
||
# MISC FILES | ||
.cache | ||
.DS_Store | ||
.idea | ||
.project | ||
.settings | ||
*.esproj | ||
*.sublime-workspace | ||
*.sublime-project | ||
*.tmproj | ||
*.tmproject | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
config.codekit3 | ||
prepros-6.config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Elasticsearch Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). | ||
|
||
## 1.0.0 - 2018-02-06 | ||
### Added | ||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2018 Alban Jubert | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Elasticsearch plugin for Craft CMS 3.x | ||
|
||
Bring the power of Elasticsearch to you Craft 3 CMS project | ||
|
||
![Screenshot](resources/img/plugin-logo.png) | ||
|
||
## Requirements | ||
|
||
This plugin requires Craft CMS 3.0.0-RC1 or later. | ||
In order to index data, you will need an Elasticsearch 6.0 or later with the Ingest attachment processor plugin activated. | ||
|
||
2. Then tell Composer to load the plugin: | ||
|
||
composer require la-hautes-societe/craft-elasticsearch | ||
|
||
3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Elasticsearch. | ||
|
||
## Elasticsearch Overview | ||
|
||
-Insert text here- | ||
|
||
## Configuring Elasticsearch | ||
|
||
-Insert text here- | ||
|
||
## Using Elasticsearch | ||
|
||
-Insert text here- | ||
|
||
## Elasticsearch Roadmap | ||
|
||
Some things to do, and ideas for potential features: | ||
|
||
* Release it | ||
|
||
Brought to you by [La Haute Société](https://www.lahautesociete.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "la-haute-societe/craft-elasticsearch", | ||
"description": "Bring the power of Elasticsearch to your Craft CMS projects.", | ||
"type": "craft-plugin", | ||
"version": "1.0.0", | ||
"keywords": [ | ||
"craft", | ||
"cms", | ||
"craftcms", | ||
"craft-plugin", | ||
"elasticsearch" | ||
], | ||
"support": { | ||
"docs": "https://github.com/la-haute-societe/craft-elasticsearch/blob/master/README.md", | ||
"issues": "https://github.com/la-haute-societe/craft-elasticsearch/issues" | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "La Haute Société", | ||
"homepage": "https://www.lahautesociete.com" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"require": { | ||
"craftcms/cms": "^3.0.0-RC1", | ||
"yiisoft/yii2-elasticsearch": "^2.1.0-dev" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"lhs\\elasticsearch\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"name": "Elasticsearch", | ||
"handle": "elasticsearch", | ||
"schemaVersion": "1.0.0", | ||
"hasCpSettings": true, | ||
"hasCpSection": true, | ||
"changelogUrl": "https://raw.githubusercontent.com/la-haute-societe/craft-elasticsearch/master/CHANGELOG.md", | ||
"components": { | ||
"elasticsearch": "lhs\\elasticsearch\\services\\Elasticsearch" | ||
}, | ||
"class": "lhs\\elasticsearch\\Elasticsearch" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
<?php | ||
/** | ||
* Elasticsearch plugin for Craft CMS 3.x | ||
* | ||
* Bring the power of Elasticsearch to you Craft 3 CMS project | ||
* | ||
* @link https://www.lahautesociete.com | ||
* @copyright Copyright (c) 2018 Alban Jubert | ||
*/ | ||
|
||
namespace lhs\elasticsearch; | ||
|
||
use Craft; | ||
use craft\base\Element; | ||
use craft\base\Plugin; | ||
use craft\console\Application as ConsoleApplication; | ||
use craft\elements\Entry; | ||
use craft\events\PluginEvent; | ||
use craft\events\RegisterUrlRulesEvent; | ||
use craft\services\Plugins; | ||
use craft\web\twig\variables\CraftVariable; | ||
use craft\web\UrlManager; | ||
use lhs\elasticsearch\jobs\DeleteElement; | ||
use lhs\elasticsearch\jobs\IndexElement; | ||
use lhs\elasticsearch\jobs\SomeJob; | ||
use lhs\elasticsearch\models\Settings; | ||
use lhs\elasticsearch\variables\ElasticsearchVariable; | ||
use yii\base\Event; | ||
use yii\elasticsearch\Connection; | ||
|
||
/** | ||
* Craft plugins are very much like little applications in and of themselves. We’ve made | ||
* it as simple as we can, but the training wheels are off. A little prior knowledge is | ||
* going to be required to write a plugin. | ||
* | ||
* For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL, | ||
* as well as some semi-advanced concepts like object-oriented programming and PHP namespaces. | ||
* | ||
* https://craftcms.com/docs/plugins/introduction | ||
* | ||
* @author Alban Jubert | ||
* @package Elasticsearch | ||
* @since 1.0.0 | ||
* | ||
* @property Elasticsearch $elasticsearch | ||
* @property Settings $settings | ||
* @property Connection $connection | ||
* @method Settings getSettings() | ||
*/ | ||
class Elasticsearch extends Plugin | ||
{ | ||
/** | ||
* Static property that is an instance of this plugin class so that it can be accessed via | ||
* Elasticsearch::$plugin | ||
* | ||
* @var Elasticsearch | ||
*/ | ||
public static $plugin; | ||
|
||
// Static Properties | ||
// ========================================================================= | ||
public $hasCpSection = true; | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* Set our $plugin static property to this class so that it can be accessed via | ||
* Elasticsearch::$plugin | ||
* | ||
* Called after the plugin class is instantiated; do any one-time initialization | ||
* here such as hooks and events. | ||
* | ||
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically; | ||
* you do not need to load it in your init() method. | ||
* | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
$this->name = "Elasticsearch"; | ||
self::$plugin = $this; | ||
|
||
$this->setComponents([ | ||
'connection' => [ | ||
'class' => 'yii\elasticsearch\Connection', | ||
'nodes' => [ | ||
['http_address' => $this->settings->http_address], | ||
// configure more hosts if you have a cluster | ||
], | ||
'auth' => [ | ||
'username' => $this->settings->auth_username, | ||
'password' => $this->settings->auth_password | ||
] | ||
] | ||
]); | ||
|
||
// Add in our console commands | ||
if (Craft::$app instanceof ConsoleApplication) { | ||
$this->controllerNamespace = 'lahautesociete\elasticsearch\console\controllers'; | ||
} | ||
|
||
// Register our site routes | ||
// Event::on( | ||
// UrlManager::class, | ||
// UrlManager::EVENT_REGISTER_SITE_URL_RULES, | ||
// function (RegisterUrlRulesEvent $event) { | ||
// $event->rules['siteActionTrigger1'] = 'elasticsearch/elasticsearch'; | ||
// } | ||
// ); | ||
|
||
// Register our CP routes | ||
Event::on( | ||
UrlManager::class, | ||
UrlManager::EVENT_REGISTER_CP_URL_RULES, | ||
function (RegisterUrlRulesEvent $event) { | ||
$event->rules['elasticsearch/test-connection'] = 'elasticsearch/elasticsearch/test-connection'; | ||
$event->rules['elasticsearch/reindex-all'] = 'elasticsearch/elasticsearch/reindex-all'; | ||
} | ||
); | ||
|
||
// Register our variables | ||
Event::on( | ||
CraftVariable::class, | ||
CraftVariable::EVENT_INIT, | ||
function (Event $event) { | ||
/** @var CraftVariable $variable */ | ||
$variable = $event->sender; | ||
$variable->set('elasticsearch', ElasticsearchVariable::class); | ||
} | ||
); | ||
|
||
/* | ||
* Add or update an element to the index | ||
*/ | ||
Event::on( | ||
Element::class, | ||
Element::EVENT_AFTER_SAVE, | ||
function (Event $event) { | ||
$element = $event->sender; | ||
if ($element instanceof Entry) { | ||
if ($element->enabled) { | ||
// Elasticsearch::$plugin->elasticsearch->indexEntry($element); | ||
Craft::$app->queue->push(new IndexElement([ | ||
'siteId' => $element->siteId, | ||
'elementId' => $element->id | ||
])); | ||
} else { | ||
Elasticsearch::$plugin->elasticsearch->deleteEntry($element); | ||
} | ||
|
||
} | ||
} | ||
); | ||
|
||
/* | ||
* Delete an element from the index | ||
*/ | ||
Event::on( | ||
Element::class, | ||
Element::EVENT_AFTER_DELETE, | ||
function (Event $event) { | ||
$element = $event->sender; | ||
Elasticsearch::$plugin->elasticsearch->deleteEntry($element); | ||
} | ||
); | ||
|
||
// Do something after we're installed | ||
Event::on( | ||
Plugins::class, | ||
Plugins::EVENT_AFTER_INSTALL_PLUGIN, | ||
function (PluginEvent $event) { | ||
if ($event->plugin === $this) { | ||
// We were just installed | ||
} | ||
} | ||
); | ||
|
||
|
||
/** | ||
* Logging in Craft involves using one of the following methods: | ||
* | ||
* Craft::trace(): record a message to trace how a piece of code runs. This is mainly for development use. | ||
* Craft::info(): record a message that conveys some useful information. | ||
* Craft::warning(): record a warning message that indicates something unexpected has happened. | ||
* Craft::error(): record a fatal error that should be investigated as soon as possible. | ||
* | ||
* Unless `devMode` is on, only Craft::warning() & Craft::error() will log to `craft/storage/logs/web.log` | ||
* | ||
* It's recommended that you pass in the magic constant `__METHOD__` as the second parameter, which sets | ||
* the category to the method (prefixed with the fully qualified class name) where the constant appears. | ||
* | ||
* To enable the Yii debug toolbar, go to your user account in the AdminCP and check the | ||
* [] Show the debug toolbar on the front end & [] Show the debug toolbar on the Control Panel | ||
* | ||
* http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html | ||
*/ | ||
Craft::info( | ||
Craft::t( | ||
'elasticsearch', | ||
'{name} plugin loaded', | ||
['name' => $this->name] | ||
), | ||
__METHOD__ | ||
); | ||
} | ||
|
||
// Protected Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* Creates and returns the model used to store the plugin’s settings. | ||
* | ||
* @return \craft\base\Model|null | ||
*/ | ||
protected function createSettingsModel() | ||
{ | ||
return new Settings(); | ||
} | ||
|
||
/** | ||
* Returns the rendered settings HTML, which will be inserted into the content | ||
* block on the settings page. | ||
* | ||
* @return string The rendered settings HTML | ||
*/ | ||
protected function settingsHtml(): string | ||
{ | ||
return Craft::$app->view->renderTemplate( | ||
'elasticsearch/settings', | ||
[ | ||
'settings' => $this->getSettings() | ||
] | ||
); | ||
} | ||
} |
Oops, something went wrong.