Yii 1.1: signals-module

Help events to escape from the application context
14 followers

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

Total 4 comments

#5516 report it
Asgaroth at 2011/10/18 01:11am
Awesome!

this looks great! thanks

#4254 report it
Ben at 2011/06/20 06:33pm
Thanks for that link

Looks indeed similar... I didn't know there is a word for that concept. Have to have a look at their blog these days, guess there might be some useful ideas.

However, I don't want to focus only on the RestEmitter, that sends out those POST requests. A second emitter, which will hopefully make it into the next release, will handle emitting events to the command line. This way, one could start local processes depending on the events that get raised by an yii-app.

#4253 report it
Rodrigo Coelho at 2011/06/20 05:55pm
Seems similar to WebHooks.
#4250 report it
abajja at 2011/06/20 04:27am
Interesting project.

Interesting project.

Leave a comment

Please to leave your comment.

Create extension