Symbiosis, event structure for bootstrapping plugins.

Symbiosis, event structure for bootstrapping plugins.

README

Symbiosis is a drop-in event driven plugin architecture.

The goal is to allow anyone to create a plugin structure for their existing code base using an event oriented system.
The secondary benefit of using Symbiosis is that the event structure can be used apart from the plugin structure.

Latest Stable Version
Build Status

Requirements

PHP 5.4+

Setup

  1. Add as a composer dependency or install directly from composer.
  2. That’s it!

Testing

  1. Run composer install --dev.
  2. Run phpunit.

Example Plugin

Plugin

<?php

namespace YourAppPlugin;

use ZumbaSymbiosisFrameworkPlugin,
    ZumbaSymbiosisEventEventManager,
    ZumbaSymbiosisFrameworkRegisterable;

class SamplePlugin extends Plugin implements Registerable {

  public function getEvents() {
    return array(
      'sample.someevent' => function($event) {
        print_r($event->data());
      });
    );
  }

}

Your application bootstrap

<?php

use ZumbaSymbiosisPluginPluginManager;

// Somewhere in your application bootstrap, load your plugins
$pluginManager = new PluginManager(
  '/path/to/your/plugin/directory', // Path to where you stored your plugins
  'YourAppPlugin'                  // namespace defined in your plugins (see example above)
);
$pluginManager->loadPlugins();

Your application

<?php

use ZumbaSymbiosisEventEvent;

// Somewhere in your app, trigger plugins listening to event
$pluginManager->trigger(new Event('sample.someevent', array('ping' => 'pong')));

Output

Array
(
    [ping] => pong
)

Individual Event Registries

As of v1.2, event registries have been added to allow for separation of events. This allows for “namespacing”
your event registries. The EventManager remains backwards compatible as now the EventManager creates a static instance
of an EventRegistry. Since the event structure is loosly coupled in the Plugin architecture, this allows for namespacing
your event registries per plugin.

Example Event Registry namespacing

<?php

$registry1 = new ZumbaSymbiosisEventEventRegistry();
$registry2 = new ZumbaSymbiosisEventEventRegistry();

$registry1->register('sample.someevent', function ($event) {
	print_r($event->data());
});
$registry2->register('sample.someevent', function ($event) {
	echo "Separate registryn";
	print_r($event->data());
});

$event = new ZumbaSymbiosisEventEvent('sample.someevent', array('ping' => 'pong'));
$registry1->trigger($event);
// Prints: 
// Array(
//   [ping] => pong
// )

$registry2->trigger($event);
// Prints:
// Separate registry
// Array(
//   [ping] => pong
// )

Source

Add a Comment

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *