Tags:
archive_me1Add my vote for this tag create new tag
, view all tags

Refactoring Proposal: Enhanced Plugin Architecture

Motivation

The current plugin mechanism don't allow the posibility to have OO-styled plugins wihout ugly hacks from the plugin side, as the handlers are registered by name (ie: using the TWiki::Plugins::PluginName::Handlername style), which prevent OO-Styled plugins from receiving the $self reference as the first parameter.

Description

Modify the way the plugin handlers are registered and applied to allow OO-plugins to register their handlers without any ugly hack.

-- RafaelAlvarez - 01 Jul 2005

Impact and Available Solutions

Implementation

Here is the patch against DEVELOP 4483

Index: lib/TWiki/Plugins.pm
===================================================================
--- lib/TWiki/Plugins.pm   (revision 4483)
+++ lib/TWiki/Plugins.pm   (working copy)
@@ -311,12 +311,12 @@
     my $handlerName = shift;
 
     my $status;
-    foreach my $handler ( @{$this->{registeredHandlers}{$handlerName}} ) {
+    foreach my $plugin ( @{$this->{registeredHandlers}{$handlerName}} ) {
         # Set the value of $SESSION for this call stack
         local $SESSION = $this->{session};
         # apply handler on the remaining list of args
         no strict 'refs';
-        $status = &$handler;
+        my $status=$plugin->invoke($handlerName,@_);
         use strict 'refs';
         if( $status && $onlyOnceHandlers{$handlerName} ) {
             return $status;
Index: lib/TWiki/Plugin.pm
===================================================================
--- lib/TWiki/Plugin.pm   (revision 4483)
+++ lib/TWiki/Plugin.pm   (working copy)
@@ -205,7 +205,7 @@
     foreach my $h ( @registrableHandlers ) {
         my $sub = $p.'::'.$h;
         if( defined( &$sub )) {
-            push( @{$plugins->{registeredHandlers}{$h}}, $sub );
+            push( @{$plugins->{registeredHandlers}{$h}}, $this );
             if( $deprecated{$h} ) {
                 $this->{session}->writeWarning
                   ( $this->{name}.' defines deprecated '.$h );
@@ -214,6 +214,17 @@
     }
 }
 
+# Invoke a handler
+sub invoke {  
+   my $this=shift;
+    my $handlerName=shift;
+   my $handler = 'TWiki::Plugins::' . $this->{name}.'::'.$handlerName;
+   no strict 'refs';
+   my $status=&$handler;
+   use strict 'refs';
+   return $status;
+}
+
 # Get the version number of the specified plugin.
 # SMELL: may die if the plugin doesn't compile
 sub getVersion {

The key change is to register plugin under each handler it has, and to move the responsability to invoke the handler from PluginsDotPm to PluginDotPm? . That way, subclases of PluginDotPm? can override registerHandlers and invoke in any way they need them to be.

A nice "side effect" of the DakarRelease architecture is that the current PluginDotPm? can serve as a wrapper for old-styled plugins.

-- RafaelAlvarez - 01 Jul 2005

Commited on v4484.

-- RafaelAlvarez - 01 Jul 2005


Discussion

Already merged, with more than 3 years in production, and counting smile

I updated the form, but as the ChangeProposalForm changed, the new data is being displayed.

-- RafaelAlvarez - 08 Aug 2008

 
Topic revision: r3 - 08 Aug 2008 - 22:13:14 - RafaelAlvarez
 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback