All Projects → ikkez → f3-events

ikkez / f3-events

Licence: GPL-3.0 license
Event system for the PHP Fat-Free Framework

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to f3-events

Chordly
Chordly is a javascript library that may be used to detect and act upon key sequences entered by a user.
Stars: ✭ 14 (-41.67%)
Mutual labels:  events
commons.openshift.org
Repository for OpenShift Commons Community Site
Stars: ✭ 31 (+29.17%)
Mutual labels:  events
desim
A discrete-time events simulation framework, written in rust, using the generator experimental feature
Stars: ✭ 27 (+12.5%)
Mutual labels:  events
code-of-conduct
rubyberlin.github.io/code-of-conduct/
Stars: ✭ 60 (+150%)
Mutual labels:  events
KittyEvents
Super simple event system on top of ActiveJob
Stars: ✭ 20 (-16.67%)
Mutual labels:  events
kalend
React calendar component with support for multiple views and events
Stars: ✭ 135 (+462.5%)
Mutual labels:  events
vector
A high-performance observability data pipeline.
Stars: ✭ 12,138 (+50475%)
Mutual labels:  events
fswatch
File/Directory Watcher for Modern C++
Stars: ✭ 56 (+133.33%)
Mutual labels:  events
dead-simple
💀💡 Dead simple PubSub and EventEmitter in JavaScript
Stars: ✭ 21 (-12.5%)
Mutual labels:  events
vsSolutionBuildEvent
🎛 Event-Catcher with variety of advanced Actions to service projects, libraries, build processes, runtime environment of the Visual Studio, MSBuild Tools, and …
Stars: ✭ 66 (+175%)
Mutual labels:  events
berlinblockchainweek
Website for Berlin Blockchain Week 2018
Stars: ✭ 15 (-37.5%)
Mutual labels:  events
angular-PubSub
Angular 1.x implementation of the Publish–Subscribe pattern.
Stars: ✭ 32 (+33.33%)
Mutual labels:  events
talksearch
🎤 An interactive search experience for video titles and transcripts
Stars: ✭ 24 (+0%)
Mutual labels:  events
event-dispatcher
💥 Best events support (symfony/event-dispatcher) to Nette Framework (@nette)
Stars: ✭ 23 (-4.17%)
Mutual labels:  events
PyDREAM
Python Implementation of Decay Replay Mining (DREAM)
Stars: ✭ 22 (-8.33%)
Mutual labels:  events
vue-bus
Tiny simple central event bus plugin for Vue.js
Stars: ✭ 50 (+108.33%)
Mutual labels:  events
2018
Webend v4.5.26
Stars: ✭ 24 (+0%)
Mutual labels:  events
xsystem
Building Blocks for XState-based Actor Systems.
Stars: ✭ 40 (+66.67%)
Mutual labels:  events
kube-watch
Simple tool to get webhooks on Kubernetes cluster events
Stars: ✭ 21 (-12.5%)
Mutual labels:  events
event
An event dispatching library for PHP
Stars: ✭ 19 (-20.83%)
Mutual labels:  events

Sugar Events

This is a event system for the PHP Fat-free Framework. Here is what's included so far:

  • emit events from any point of your app
  • attach one or multiple listeners to an event
  • a listener (hook) can have a priority order
  • additional options for listeners
  • local events on specific objects
  • send payload and context data with an event
  • sub-events and event propagation
  • stop the event chain
  • works with F3 v3.5 and PHP v7.2+

This event system is experimental, so please handle with care.

Installation

  • Method 1: use composer: composer require ikkez/f3-events

  • Method 2: copy the lib/event.php file into your F3 lib/ directory or another directory that is known to the AUTOLOADER

How does it work:

The Event class is a child of Prefab, so you can get it everywhere like this:

// fetch the global Event instance
$events = \Sugar\Event::instance();

Define a listener / hook:

// typical F3 callstring
$events->on('user_login', 'Notification->user_login');
// or with callbacks
$events->on('user_login', function(){
  // ...
});
// or with callables
$events->on('user_login', [$this,'method']); 

Send an event:

$events->emit('user_login');

Send payload with event:

$events->on('user_login', function($username){
  \Logger::log($username.' logged in');
});
$events->emit('user_login', 'freakazoid');

Multiple listeners with prioritization:

$events->on('user_login', function($username){
  \Logger::log($username.' logged in');
}, 10); // 10 is default priority
$events->on('user_login', function(){
  \Flash::addMessage('You have logged in successfully');
}, 20); // 20 is a higher priority and is called first
$events->emit('user_login', 'freakazoid');

Stop the event chain:

$events->on('user_login', function($username){
  \Logger::log($username.' logged in');
});
$events->on('user_login', function(){
  \Flash::addMessage('You have logged in successfully');
  return false; // <-- skip any other listener on the same event
}, 20);
$events->emit('user_login', 'freakazoid');
// The logger event isn't called anymore

Additional event context data:

$events->on('user_login', function($username,$context){
  if ($context['lang'] == 'en')
    \Flash::addMessage('You have logged in successfully');
  elseif($context['lang'] == 'de')
    \Flash::addMessage('Du hast dich erfolgreich angemeldet');
});
$events->emit('user_login', 'freakazoid', array('lang'=>'en'));

Additional listener options:

$events->on('user_login', function($username,$context,$event){
  \Flash::addMessage('You have logged in successfully', $event['options']['type']);
}, 20, array('type'=>'success'));

I think that are the basic usage samples that could fit the most cases. Nevertheless here are some more advanced things you can do:

Filter payload:

$events->on('get_total', function($basket){
  $sum = 0;
  foreach($basket as $prod) {
    $sum+=$prod;
  }
  return $sum;
});

$products = array(
  'a' => 2,
  'b' => 8,
  'c' => 15,
);

$sum = $events->emit('get_total',$products);
echo $sum; // 25

Add a sub-event. These are called after the parent event. Listeners and sub-events follow the FIFO processing, which means the first that is registered is the first that will be called.

$events->on('get_total.tax', function($sum){
  return $sum+($sum*0.2);
});
$events->on('get_total.shipping', function($sum){
  return $sum+5;
});
$sum = $events->emit('get_total',$products);
echo $sum; // 35

Remove hooks:

$events->off('get_total.tax');
$sum = $events->emit('get_total',$products);
echo $sum; // 30

There is also a mechanic build in which supports local events for mappers and such, which have implemented it:

$user = new \Model\User();
$events->watch($user)->on('update.email','\Mailer->sendEmailActivationLink');

Unit tests

to add the tests to your local F3 test-bench, add this:

// Event Tests
$f3->concat('AUTOLOAD', ',path/to/f3-events/test/');
\Sugar\EventTests::init();

License

You are allowed to use this plugin under the terms of the GNU General Public License version 3 or later.

Copyright (C) 2017 Christian Knuth [ikkez]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].