Skip to content
This repository has been archived by the owner on Dec 3, 2018. It is now read-only.

Events subsystem #116

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

davidnewcomb
Copy link
Contributor

Each forum thread is related to one entity on my site. So I am creating threads, merging and deleting them as I create update and delete my own entities. In order for the forums to "talk" back to the main application, I had to add an events system.

src/events/EventService - easy way of sending events.
src/events/PodiumEvent - event names.
src/events/PodiumThreadEvent - event object for events about threads.

I have triggered thread events inside Thread and Post. To keep it simple, I trigger a thread update event whenever anything happens to the posts of that thread.

Using it is easy:

main.php

'forum' => [
        'class' => 'bizley\podium\Podium',
        // Events
        'on '. PodiumEvent::THREAD_CREATED => ['app\components\PodiumListener', 'handler'],
        'on '. PodiumEvent::THREAD_UPDATED => ['app\components\PodiumListener', 'handler'],
        'on '. PodiumEvent::THREAD_DELETED => ['app\components\PodiumListener', 'handler'],

PodiumListener.php

<?php
namespace app\components;

use common\models\MyEntity;

class PodiumListener {

	public static function handler($event) {
		$link = $event->thread->name;
		$p = MyEntity::findOne(["link" => $link]);
		if ($p) {
			$p->updated_at = time();
			$p->save();
		}
		return true;
	}
}

I just needed to have my entity's last modified time linked with the thread, but after doing it I can see loads of other possibilities with other events. One that sprang to mind was catching each Post create/update, parsing the URLs out of it and filing them somewhere else.

I can see it being useful to run actions when threads or posts are created, etc.
Like I said, I've kept it simple to see what the reaction is like.

The only thing worth mentioning is that I wasn't sure if you wanted to include the event triggering inside the thread create/update/delete transaction. I decided to place them just outside because I didn't want the transaction to rollback for an event sending failure. But it's up to you really!

@bizley
Copy link
Owner

bizley commented May 27, 2017

I'm in the middle of splitting Podium into API engine and module implementing it and events are there already ;) So good idea in general. Let me keep this PR for reference until the new version is out.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants