Changeset 24447

Show
Ignore:
Timestamp:
11/20/08 16:19:39 (7 weeks ago)
Author:
kazuho
Message:

redesign plugin architecture, add mobile/plugin.pm

Location:
lang/perl/NanoA/trunk
Files:
2 added
3 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/NanoA/trunk/app/tinybbs/start.pm

    r23704 r24447  
    33use strict; 
    44use warnings; 
     5use utf8; 
     6 
     7use plugin::mobile; 
    58 
    69use base qw(NanoA); 
  • lang/perl/NanoA/trunk/lib/NanoA.pm

    r24441 r24447  
    99my %REQUIRED; 
    1010my %LOADED; 
     11my %HOOKS; 
    1112 
    1213BEGIN { 
    1314    %REQUIRED = (); 
    1415    %LOADED = (); 
     16    %HOOKS = ( 
     17        prerun  => {}, 
     18        postrun => {}, 
     19    ); 
    1520}; 
    1621 
     
    1823    my ($klass, $config) = @_; 
    1924    my $self = bless { 
    20         config  => $config, 
    21         query   => undef, 
    22         headers => { 
     25        config        => $config, 
     26        query         => undef, 
     27        headers       => { 
    2328            -type    => 'text/html', 
    2429            -charset => 'utf8', 
    2530        }, 
    26         prerun_hooks  => [], 
    27         postrun_hooks => [], 
    28         stash   => {}, 
     31        stash         => {}, 
    2932    }, $klass; 
    3033    $self; 
    3134} 
    3235 
    33 sub prerun { 
    34     my $self = shift; 
    35     foreach my $h (@{$self->prerun_hooks}) { 
    36         $h->($self); 
    37     } 
    38 } 
    39  
    40 sub postrun { 
    41     my ($self, $bodyref) = @_; 
    42     foreach my $h (@{$self->postrun_hooks}) { 
    43         $h->($self, $bodyref); 
    44     } 
     36sub run_hooks { 
     37    my $self = shift; 
     38    my $mode = shift; 
     39    my $hooks = $HOOKS{$mode}->{ref $self} 
     40        or return; 
     41    $_->($self, @_) 
     42        for @$hooks; 
     43} 
     44 
     45sub register_hook { 
     46    my ($klass, $mode, $func) = @_; 
     47    die "unknown hook: $mode\n" 
     48        unless $HOOKS{$mode}; 
     49    my $target = $HOOKS{$mode}->{ref $klass || $klass} ||= []; 
     50    push @$target, $func; 
    4551} 
    4652 
     
    6571    my $self = shift; 
    6672    $self->{headers}; 
    67 } 
    68  
    69 sub prerun_hooks { 
    70     my $self = shift; 
    71     $self->{prerun_hooks}; 
    72 } 
    73  
    74 sub postrun_hooks { 
    75     my $self = shift; 
    76     $self->{postrun_hooks}; 
    7773} 
    7874 
  • lang/perl/NanoA/trunk/lib/NanoA/Dispatch.pm

    r24441 r24447  
    3131    my $handler = $handler_klass->new($config); 
    3232    $config->init_app($handler); 
    33     $handler->prerun(); 
     33     
     34    $handler->run_hooks('prerun'); 
    3435    my $body = $handler->run(); 
    35     $handler->postrun(\$body); 
     36    $handler->run_hooks('postrun', \$body); 
    3637     
    3738    $handler->print_header();