signals-module Help events to escape from the application context

  1. Requirements
  2. Usage
  3. Resources
  4. Change Log

This module extends the existing yii event mechanism, so that events might leave the context of the application. It comes with an behavior that implements the signal emitting feature and a web service, that allows external applications to connect to your signals.

signal emitting

Requirements

  • Developed and testet using Yii v1.1.7, but should also work fine with older versions.
  • Requires PHP 5.3, due to the usage of the event-interceptor extension

Usage

You basically configure the module and its db connection. Then you attach the SignalBehavior to the components, which you want to emit signals. Now, each event that these components raise can break the application context.

Configuration of the module in application config
return array(
 
    [...]
 
    'modules'=>array(
 
      [...]
 
      // configure the signals module
      'signals' => array(
 
        // class path to the module in alias notation
        'class'=>'application.modules.signals-1_0.SignalsModule',
 
        // Configure the db connection for the module.
        // It can use its own connection, or you can create the tables in your
        // application's db. If you don't use a seperate connection, you can
        // skip this configuration.
        'components'=>array(
          'db' => array(
            'class'=>'CDbConnection',
            'connectionString'=>'sqlite:'.dirname(__FILE__).'/../data/signals.s3db',
          ),
        ),
 
        // configure the signals, to which the outside world can connect
        'signals'=>array(
          'Post\\onPostPublished',
        ),
      ),
    ),
 
    [...]
 
);
Attaching the behavior to a component, which shall emit signals
class Post extends CActiveRecord
{
      [...]
 
      // ensure the signal behavior is attached to this component,
      // so that its events can be emitted as signals.
      public function behaviors()
      {
        return array(
 
          // configure the signal behavior
          'signalBehavior' => array(
 
            // class path to the behavior in alias notation
            'class' => 'signals.behaviors.SignalBehavior',
 
            // This is optional. If we don't provide it, every event this
            // component raises might be emitted as a signal. But since we
            // don't want to emit onAfterConstruct-, onAfterFind-,
            // onAfterXyz-Signals (those are too low-level), we can save some
            // overhead by explicitly specifying only the onPostPublished-Event
            // to be emitted as a signal.
            'events' => array(
              'onPostPublished',
            ),
          ),
        );
      }
 
      // event definition
      public function onPostPublished( CEvent $event )
      {
        $this->raiseEvent( 'onPostPublished', $event );
      }
 
      protected function afterSave()
      {
        [...]
 
        // if the post has been saved and its status is published, raise
        // the onPostPublished event, which contains the post id as a parameter
        if ( intval($this->status) === self::STATUS_PUBLISHED)
          $this->onPostPublished( new CEvent($this, array('id'=>$this->id)) );
      }
 
}

Resources

Change Log

June 19, 2011

  • Initial release
8 0
14 followers
464 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Web Service
Developed by: Ben
Created on: Jun 19, 2011
Last updated: 12 years ago

Downloads

show all

Related Extensions