[Jifty-commit] r1389 - in jifty/trunk: lib lib/Jifty lib/Jifty/Plugin

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed Jun 28 14:15:39 EDT 2006


Author: jpeacock
Date: Wed Jun 28 14:15:17 2006
New Revision: 1389

Added:
   jifty/trunk/lib/Jifty/Plugin/
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty.pm
   jifty/trunk/lib/Jifty/ClassLoader.pm
   jifty/trunk/lib/Jifty/Plugin.pm
   jifty/trunk/lib/Jifty/Util.pm

Log:
Rewrite Jifty::ClassLoader to support plugins; now the @INC entry is an
array of regexes that can be added to by plugins for additional class
generation.

Alter Jifty::Plugin and Jifty modules to use the new J::CL methods.
Working Login plugin to follow on list.

Modified: jifty/trunk/lib/Jifty.pm
==============================================================================
--- jifty/trunk/lib/Jifty.pm	(original)
+++ jifty/trunk/lib/Jifty.pm	Wed Jun 28 14:15:17 2006
@@ -119,8 +119,8 @@
     push @Jifty::Record::ISA, Jifty->config->framework('Database')->{'RecordBaseClass'};
 
     __PACKAGE__->logger( Jifty::Logger->new( $args{'logger_component'} ) );
-    # Get a classloader set up
-    Jifty::ClassLoader->new(base => Jifty->config->framework('ApplicationClass'))->require;
+
+    my $base = Jifty->config->framework('ApplicationClass');
 
     # Set up plugins
     my @plugins;
@@ -128,8 +128,13 @@
         my $class = "Jifty::Plugin::".(keys %{$plugin})[0];
         my %options = %{ $plugin->{(keys %{$plugin})[0]} };
         Jifty::Util->require($class);
-        push @plugins, $class->new(%options);
+        Jifty::ClassLoader->new(base => $class);
+        push @plugins, $class->new(base => $base, %options);
     }
+
+    # Get a classloader set up
+    Jifty::ClassLoader->new(base => $base)->require;
+
     __PACKAGE__->plugins(@plugins);
     __PACKAGE__->handler(Jifty::Handler->new());
     __PACKAGE__->api(Jifty::API->new());

Modified: jifty/trunk/lib/Jifty/ClassLoader.pm
==============================================================================
--- jifty/trunk/lib/Jifty/ClassLoader.pm	(original)
+++ jifty/trunk/lib/Jifty/ClassLoader.pm	Wed Jun 28 14:15:17 2006
@@ -27,15 +27,113 @@
 automatically loaded.
 
 =cut
+our @_CLASSES;
+our $_already_pushed;
 
 sub new {
     my $class = shift;
     my $self = bless {@_}, $class;
 
-    push @INC, $self;
+    $self->load_defaults() if $self->{base};
+
+    # finally, put this object into @INC for later
+    unless ( $_already_pushed ) {
+        push(@INC, $self);
+        $_already_pushed = 1;
+    }
     return $self;
 }
 
+
+sub register ($&) {
+    my ($self, $regex, $code) = @_;
+    push @_CLASSES, [$regex, $code];
+}
+
+sub preregister ($&) {
+    my ($self, $regex, $code) = @_;
+    unshift @_CLASSES, [$regex, $code];
+}
+
+sub load_defaults {
+    my ($self) = @_;
+    my $base = $self->{base};
+
+    $self->register(
+        qr/^(?:$base)$/,
+        sub {
+            my ($module) = @_;
+            return $self->return_class(
+                  "use warnings; use strict;\n"
+                . " package " . $self->{base} . ";\n" 
+                . "sub _autogenerated { 1 };\n"
+                . " 1;"
+            );
+        }
+    );
+
+    $self->register(
+        qr/^(?:$base)::(Record|Collection|Notification|Dispatcher|Bootstrap|Upgrade)$/,
+        sub {
+            my ($module) = @_;
+            return $self->return_class(
+                  "use warnings; use strict; package $module;\n"
+                . "use base qw/Jifty::$1/;\n"
+                . "sub _autogenerated { 1 };\n"
+                . "1;"
+            );
+        }
+    );
+    
+    $self->register(
+        qr/^(?:$base)::CurrentUser$/,
+        sub {
+            my ($module) = @_;
+            return $self->return_class(
+                  "use warnings; use strict; package $module;\n"
+                . "use base qw/Jifty::CurrentUser/;\n"
+                . "sub _autogenerated { 1 };\n"
+                . "1;"
+            );
+        }
+    );
+
+    $self->register(
+        qr/^(?:$base)::Model::(\w+)Collection$/,
+        sub {
+            my ($module) = @_;
+            my $base = $self->{base}; # for clarity purposes
+            return $self->return_class(
+                  "use warnings; use strict; package $module;\n"
+                . "use base qw/${base}::Collection/;\n"
+                . "sub record_class { '${base}::Model::$1' }\n"
+                . "sub _autogenerated { 1 };\n"
+                . "1;"
+            );
+        }
+    );
+
+    $self->register(
+        qr/^(?:$base)::Action::(Create|Update|Delete)([^\.]+)$/,
+        sub {
+            my ($module) = @_;
+            my $modelclass = $self->{base} . "::Model::" . $2;
+            Jifty::Util->require($modelclass);
+
+            return undef unless eval { $modelclass->table };
+
+            return $self->return_class(
+                  "use warnings; use strict; package $module;\n"
+                . "use base qw/Jifty::Action::Record::$1/;\n"
+                . "sub record_class { '$modelclass' };\n"
+                . "sub autogenerated { 1 };\n"
+                . "1;"
+            );
+        }
+    );
+
+}
+    
 =head2 INC
 
 The hook that is called when a module has been C<require>'d that
@@ -105,47 +203,12 @@
     $module =~ s/.pm$//;
     $module =~ s{/}{::}g;
 
-    # The quick check
-    return undef unless $module =~ m!^$base!;
-
-    if ( $module =~ m!^(?:$base)$! ) {
-        return $self->return_class(
-            "use warnings; use strict; package " . $base . ";\n" . " 1;" );
-    }
-#    elsif ( $module =~ m!^(?:$base)::Action$! ) {
-#        return $self->return_class(
-#                  "use warnings; use strict; package $module;\n"
-#                . "use base qw/Jifty::Action/; sub _autogenerated { 1 };\n"
-#                . "1;" );
-#    }
-    elsif ( $module =~ m!^(?:$base)::(Record|Collection|Notification|Dispatcher|Bootstrap|Upgrade)$! ) {
-        return $self->return_class(
-                  "use warnings; use strict; package $module;\n"
-                . "use base qw/Jifty::$1/; sub _autogenerated { 1 };\n"
-                . "1;" );
-    } elsif ( $module =~ m!^(?:$base)::CurrentUser$! ) {
-        return $self->return_class(
-                  "use warnings; use strict; package $module;\n"
-                . "use base qw/Jifty::CurrentUser/; sub _autogenerated { 1 };\n"
-                . "1;" );
-    } elsif ( $module =~ m!^(?:$base)::Model::(\w+)Collection$! ) {
-        return $self->return_class(
-                  "use warnings; use strict; package $module;\n"
-                . "use base qw/@{[$base]}::Collection/;\n"
-                . "sub record_class { '@{[$base]}::Model::$1' }\n"
-                . "1;" );
-    } elsif ( $module =~ m!^(?:$base)::Action::(Create|Update|Delete)([^\.]+)$! ) {
-        my $modelclass = $base . "::Model::" . $2;
-        Jifty::Util->require($modelclass);
-
-        return undef unless eval { $modelclass->table };
-
-        return $self->return_class(
-                  "use warnings; use strict; package $module;\n"
-                . "use base qw/Jifty::Action::Record::$1/;\n"
-                . "sub record_class { '$modelclass' };\n"
-                . "sub autogenerated { 1 };\n"
-                . "1;" );
+    foreach my $class ( @_CLASSES ) {
+        my ($regex, $code) = @{$class};
+        if ( $module =~ /$regex/ ) {
+            my $return = &$code($module);
+            return $return if defined $return;
+        }
     }
     return undef;
 }
@@ -176,8 +239,8 @@
 
 sub require {
     my $self = shift;
-    
     my $base = $self->{base};
+
     # if we don't even have an application class, this trick will not work
     return unless ($base); 
     Jifty::Util->require($base);

Modified: jifty/trunk/lib/Jifty/Plugin.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin.pm	Wed Jun 28 14:15:17 2006
@@ -49,15 +49,17 @@
 
 sub new {
     my $class = shift;
+    push @_, plugin => $class;
     
-    # Get a classloader set up
-    Jifty::ClassLoader->new(base => $class)->require;
+    # Need to initialize a dispatcher
     Jifty::Util->require($class->dispatcher);
 
     # XXX TODO: Add .po path
 
-    my $self = bless {} => $class;
+    my $self = bless {@_} => $class;
     $self->init(@_);
+    $self->plugin_defaults();
+    
     return $self;
 }
 
@@ -74,6 +76,46 @@
     1;
 }
 
+sub plugin_defaults {
+    my $self = shift;
+    my $base = $self->{base};
+    my $plugin = $self->{plugin};
+
+    # Create the plugin classloader elements on behalf of the application
+    require Jifty::ClassLoader;
+    my $classloader = Jifty::ClassLoader->new();
+    
+    $classloader->preregister(
+        qr/^(?:$base)::(Notification|CurrentUser)$/o,
+        sub {
+            my $module = shift;
+            my $plugin = $self->{plugin};
+            my $toplevel = "${plugin}::$1";
+            return $self->return_class(
+                      "use warnings; use strict; package $module;\n"
+                    . "use base qw/$toplevel/;\n"
+                    . "sub _autogenerated { 1 };\n"
+                    . "1;"
+            );
+        },
+    );
+
+    $classloader->preregister(
+        qr/^(?:$base)::Action::([^\.]+)$/o,
+        sub {
+            my $module = shift;
+            my $plugin = $self->{plugin};
+            my $action = "${plugin}::Action::$1";
+            return $self->return_class(
+                  "use warnings; use strict; package $module;\n"
+                . "use base qw/$action/;\n"
+                . "sub autogenerated { 1 };\n"
+                . "1;"
+            );
+        }
+    );
+}
+
 =head2 new_request
 
 Called right before every request.  By default, this adds the plugin's
@@ -133,4 +175,11 @@
     return $class."::Dispatcher";
 }
 
+sub return_class {
+    my $self = shift;
+    my $content = shift;
+    open my $fh, '<', \$content;
+    return $fh;
+}
+
 1;

Modified: jifty/trunk/lib/Jifty/Util.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Util.pm	(original)
+++ jifty/trunk/lib/Jifty/Util.pm	Wed Jun 28 14:15:17 2006
@@ -197,7 +197,7 @@
     if ($UNIVERSAL::require::ERROR) {
         my $error = $UNIVERSAL::require::ERROR;
         $error =~ s/ at .*?\n$//;
-        Jifty->log->error(sprintf("$error at %s line %d\n", (caller)[1,2]));
+        Jifty->log->warn(sprintf("$error at %s line %d\n", (caller)[1,2]));
         return 0;
     }
 


More information about the Jifty-commit mailing list