Skip to content

Commit

Permalink
Merge pull request #15 from craftpulse/develop-v4
Browse files Browse the repository at this point in the history
feat: before sync / flush hooks
  • Loading branch information
cookie10codes authored Jan 3, 2024
2 parents d2eaa03 + f448dba commit e3b92f2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ 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/).

## 5.2.0 - 2024-01-03 (Happy 2024)

### Added

- Before Sync / Flush event hooks

## 5.1.0 - 2023-12-01

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "craftpulse/craft-typesense",
"description": "Craft Plugin that synchronises with Typesense",
"type": "craft-plugin",
"version": "5.1.0",
"version": "5.2.0",
"keywords": [
"craft",
"cms",
Expand Down
28 changes: 28 additions & 0 deletions src/console/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use craft\elements\Entry;
use craft\helpers\Queue;
use percipiolondon\typesense\jobs\SyncDocumentsJob;
use percipiolondon\typesense\events\DocumentEvent;

use percipiolondon\typesense\Typesense;
use yii\console\Controller;
Expand Down Expand Up @@ -45,6 +46,15 @@
*/
class DefaultController extends Controller
{
// Events
// -------------------------------------------------------------------------

/**
* @event The event that is triggered before a flush / sync happens.
*/
public const EVENT_BEFORE_FLUSH = 'beforeFlush';
public const EVENT_BEFORE_SYNC = 'beforeSync';

// Public Methods
// =========================================================================

Expand All @@ -64,6 +74,15 @@ public function actionFlush()
$this->stdout('Flush ' . $index->indexName);
$this->stdout(PHP_EOL);

if ($this->hasEventHandlers(self::EVENT_BEFORE_FLUSH)) {
$this->trigger(self::EVENT_BEFORE_FLUSH, new DocumentEvent([
'document' => [
'index' => $index->indexName,
'type' => 'Flush',
]
]));
}

Queue::push(new SyncDocumentsJob([
'criteria' => [
'index' => $index->indexName,
Expand All @@ -81,6 +100,15 @@ public function actionSync()
$this->stdout('Sync ' . $index->indexName);
$this->stdout(PHP_EOL);

if ($this->hasEventHandlers(self::EVENT_BEFORE_SYNC)) {
$this->trigger(self::EVENT_BEFORE_SYNC, new DocumentEvent([
'document' => [
'index' => $index->indexName,
'type' => 'Sync',
]
]));
}

Queue::push(new SyncDocumentsJob([
'criteria' => [
'index' => $index->indexName,
Expand Down
28 changes: 28 additions & 0 deletions src/controllers/CollectionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use craft\web\Controller;
use Http\Client\Exception;
use percipiolondon\typesense\events\DocumentEvent;
use percipiolondon\typesense\helpers\CollectionHelper;
use percipiolondon\typesense\jobs\SyncDocumentsJob;

Expand Down Expand Up @@ -51,6 +52,15 @@
*/
class CollectionsController extends Controller
{
// Events
// -------------------------------------------------------------------------

/**
* @event The event that is triggered before a flush / sync happens.
*/
public const EVENT_BEFORE_FLUSH = 'beforeFlush';
public const EVENT_BEFORE_SYNC = 'beforeSync';

// Protected Properties
// =========================================================================
protected array|int|bool $allowAnonymous = ['create-collection', 'drop-collection', 'list-collections', 'retrieve-collection', 'index-documents', 'list-documents', 'delete-documents'];
Expand Down Expand Up @@ -156,6 +166,15 @@ public function actionFlushCollection(): Response
$request = Craft::$app->getRequest();
$index = $request->getBodyParam('index');

if ($this->hasEventHandlers(self::EVENT_BEFORE_FLUSH)) {
$this->trigger(self::EVENT_BEFORE_FLUSH, new DocumentEvent([
'document' => [
'index' => $index,
'type' => 'Flush',
]
]));
}

Queue::push(new SyncDocumentsJob([
'criteria' => [
'index' => $index,
Expand All @@ -182,6 +201,15 @@ public function actionSyncCollection(): Response
$request = Craft::$app->getRequest();
$index = $request->getBodyParam('index');

if ($this->hasEventHandlers(self::EVENT_BEFORE_SYNC)) {
$this->trigger(self::EVENT_BEFORE_SYNC, new DocumentEvent([
'document' => [
'index' => $index,
'type' => 'Sync',
]
]));
}

Queue::push(new SyncDocumentsJob([
'criteria' => [
'index' => $index,
Expand Down
20 changes: 20 additions & 0 deletions src/events/DocumentEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace percipiolondon\typesense\events;

use percipiolondon\typesense\models\CollectionModel;
use yii\base\Event;

/**
* Collection event class.
*
* @author Percipio Global Ltd. <[email protected]>
* @since 1.0.0
*/
class DocumentEvent extends Event
{
/**
* @var CollectionModel|null The collection model associated with the event.
*/
public array|null $document = null;
}

0 comments on commit e3b92f2

Please sign in to comment.