[Jifty-commit] r4911 - in jifty/trunk: lib/Jifty lib/Jifty/Plugin share/web/templates/__jifty

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed Jan 23 15:12:56 EST 2008


Author: sartak
Date: Wed Jan 23 15:12:47 2008
New Revision: 4911

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Plugin.pm
   jifty/trunk/lib/Jifty/Plugin/Halo.pm
   jifty/trunk/lib/Jifty/Plugin/SQLQueries.pm
   jifty/trunk/share/web/templates/__jifty/halo

Log:
 r50692 at onn:  sartak | 2008-01-23 15:12:29 -0500
 Awright! Plugins can now fiddle with (TD) halos as they wish.
 
 As an example, query logging is activated only if you use the SQLQueries plugin. Which also fixes a bug where any queries caught by the Halo plugin would be lost to SQLQueries.


Modified: jifty/trunk/lib/Jifty/Plugin.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin.pm	Wed Jan 23 15:12:47 2008
@@ -59,6 +59,20 @@
 
     # XXX TODO: Add .po path
     $self->init(@_);
+
+    # XXX: If we have methods for halos, add them. Some way of detecting "are
+    # we going to be using halos" would be superb. As it stands right now,
+    # plugins are loaded, initialized, and prereq-examined in the order they're
+    # listed in the config files. Instead, each phase should be separate.
+    Jifty::Util->require("Jifty::Plugin::Halo");
+    Jifty::Plugin::Halo->add_trigger(
+        halo_pre_template  => sub { $self->halo_pre_template(@_) },
+    ) if $self->can('halo_pre_template');
+
+    Jifty::Plugin::Halo->add_trigger(
+        halo_post_template => sub { $self->halo_post_template(@_) },
+    ) if $self->can('halo_post_template');
+
     return $self;
 }
 

Modified: jifty/trunk/lib/Jifty/Plugin/Halo.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Halo.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Halo.pm	Wed Jan 23 15:12:47 2008
@@ -3,6 +3,7 @@
 
 package Jifty::Plugin::Halo;
 use base qw/Jifty::Plugin/;
+use Class::Trigger;
 
 =head1 NAME
 
@@ -33,10 +34,6 @@
 
 }
 
-sub post_init {
-    Jifty->handle->log_sql_statements(1);
-}
-
 # parts of why this is.. weird is because we want to play nicely with Mason
 # halos
 sub around_template {
@@ -46,12 +43,6 @@
     my $DEPTH       = ++Jifty->handler->stash->{'_halo_depth'};
     my $ID          = Jifty->web->serial;
 
-    # if we have queries at this point, they belong to the previous template
-    if (@$STACK) {
-        push @{$STACK->[-1]->{sql_statements}}, Jifty->handle->sql_statement_log;
-        Jifty->handle->clear_sql_statement_log;
-    }
-
     # for now, call the last piece of the template's path the name
     $path =~ m{.*/(.+)};
     my $name = $1 || $path;
@@ -67,15 +58,19 @@
         depth        => $DEPTH,
     };
 
+    # if this is the first frame, discard anything from the previous queries
+    my $previous = $STACK->[-1] || {};
+
     push @$STACK, $frame;
     my $STACK_INDEX = $#$STACK;
 
-    Jifty->web->out(qq{<div id="halo-$ID">});
+    $self->call_trigger('halo_pre_template', frame => $frame, previous => $previous);
+
+    Jifty->web->out(qq{<div id="halo-$ID" class="halo">});
     $orig->();
     Jifty->web->out(qq{</div>});
 
-    push @{$frame->{sql_statements}}, Jifty->handle->sql_statement_log;
-    Jifty->handle->clear_sql_statement_log;
+    $self->call_trigger('halo_post_template', frame => $frame, previous => $previous);
 
     --Jifty->handler->stash->{'_halo_depth'};
 }

Modified: jifty/trunk/lib/Jifty/Plugin/SQLQueries.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/SQLQueries.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/SQLQueries.pm	Wed Jan 23 15:12:47 2008
@@ -6,6 +6,7 @@
 
 our @requests;
 our @slow_queries;
+our @halo_queries;
 
 =head1 NAME
 
@@ -102,7 +103,7 @@
     my $cgi = shift;
 
     my $total_time = 0;
-    my @log = Jifty->handle->sql_statement_log();
+    my @log = (splice @halo_queries), Jifty->handle->sql_statement_log();
     for (@log) {
         my ($time, $statement, $bindings, $duration, $results) = @$_;
 
@@ -129,6 +130,45 @@
     };
 }
 
+=head2 halo_pre_template
+
+Log any queries made to the previous template. Also, keep track of whatever
+queries made so the rest of the plugin can see them (since we clear the log)
+
+=cut
+
+sub halo_pre_template {
+    my $self = shift;
+    my $halo = shift;
+    my %args = @_;
+
+    push @{ $args{previous}{sql_statements} }, Jifty->handle->sql_statement_log;
+    push @halo_queries, Jifty->handle->sql_statement_log;
+
+    Jifty->handle->clear_sql_statement_log;
+}
+
+=head2 halo_post_template
+
+Log any queries made to the current template. Also, keep track of whatever
+queries made so the rest of the plugin can see them (since we clear the log)
+
+XXX: can this somehow be refactored into one function? If the same pattern
+occurs elsewhere I'll look into it.
+
+=cut
+
+sub halo_post_template {
+    my $self = shift;
+    my $halo = shift;
+    my %args = @_;
+
+    push @{ $args{frame}{sql_statements} }, Jifty->handle->sql_statement_log;
+    push @halo_queries, Jifty->handle->sql_statement_log;
+
+    Jifty->handle->clear_sql_statement_log;
+}
+
 =head1 COPYRIGHT AND LICENSE
 
 Copyright 2007 Best Practical Solutions

Modified: jifty/trunk/share/web/templates/__jifty/halo
==============================================================================
--- jifty/trunk/share/web/templates/__jifty/halo	(original)
+++ jifty/trunk/share/web/templates/__jifty/halo	Wed Jan 23 15:12:47 2008
@@ -142,7 +142,7 @@
 @args = sort {$a->[0] cmp $b->[0]} @args;
 
 my $prev = '';
-my @stmts = @{$frame->{'sql_statements'}};
+my @stmts = @{$frame->{'sql_statements'} || []};
 
 </%init>
 </%def>


More information about the Jifty-commit mailing list