Download the PHP package sunlab/wn-measures-plugin without Composer
On this page you can find all versions of the php package sunlab/wn-measures-plugin. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sunlab/wn-measures-plugin
More information about sunlab/wn-measures-plugin
Files in sunlab/wn-measures-plugin
Package wn-measures-plugin
Short Description Allows you to create/increment and display any measures you want on any model
License MIT
Homepage https://github.com/sunlabdev/wn-measures-plugin
Informations about the package wn-measures-plugin
Measures
This plugin allows you to create, update and display any measures you want on a model, some examples could be:
- Blog post's views
- Number of forum topic creation from a member
- Counting successive daily-connexion of a member
- API's resource fetches count
- ...
This plugin is intended to be used in more complex plugins or as is to register some statistics over your website's events.
Measurable Behavior
You can add the Measurable Behavior (think of it as a Trait) to any model you want. Using Storm Extension.
Extending existing models from a Plugin registration file:
Or directly to your Plugin's model:
[Experimental feature] Plugin's native and generic event listener
For the most basic events, you won't even need to write a line of code.
This plugin creates a generic event listener which can handle the most basics use cases.
Example here, it will listen for all the model.afterCreate
event over the Winter\Forum\Models\Topic
and increment a topic_created
measure on the logged-in user.
Create/Increment a measure
Once you've added the Measurable Behavior to a model, you can use the model's method incrementMeasure
.
Note: You don't have to process any kind of initialization of the measure, just use it.
Practical example 1, count how many times a user edit his posts
Practical example 2, creating a post views in Winter.Blog:
Bulk incrementation
You can increment multiple models measure at once, this is useful when you want to measure the amount of models fetches from an API.
To use Bulk incrementation, you need to pass a Builder instance of your query to the MeasureManager:
Orphan measures
For some reason, you may want to increment some orphan measures:
Decrement or reset a measure
The plugin support both decrement and reset on measures, related to model or orphan measure:
Displaying a measure
To display the measure from a model, just use the getMeasure
or getAmountOf
methods on it.
getMeasure
returns a Measure model which contains an amount
property
Events
As said earlier, this plugin gets really powerful and handy to use in other plugins. Events are fired on increment, decrement and reset measures. All the events contains the model (null for orphan measures), the measure object at its new state, and the amount. As an example, you can take a look into SunLab/Gamification for a full example:
MeasureManager and models
The MeasureManager
static class handles orphan and bulk measures modification,
but can also increment model measure:
TODO-List:
In a near future, I'll add some feature such as:
- [ ] Bulk incrementation from a model collection instead of the Builder
- [ ] A ReportWidget displaying some specific measures