Changeset 24551 for lang/perl/NanoA

Show
Ignore:
Timestamp:
11/21/08 15:13:04 (5 years ago)
Author:
kazuho
Message:

support for application-level plugin installation, prioritized plugin execution, update docs

Location:
lang/perl/NanoA/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/NanoA/trunk/app/system/tutorial.mt

    r24548 r24551  
    138138<h2 id="hooks">アプリケーション全体の共通処理</h2> 
    139139 
    140 <h2 id="session">セッション対応</h2> 
    141  
    142 <p> 
    143 各コントローラ (.pm ファイルまたは .mt ファイル) の中で plugin::session をロードすると、自動的にセッションクッキーが発行されるようになります。また、$app->session() 関数を呼んで <a href="http://d.hatena.ne.jp/tokuhirom/20081026/1224992245">HTTP::Session</a> オブジェクトを取得し、認証処理等を実現可能です。 
     140<h2 id="plugin">プラグイン機構</h2> 
     141 
     142<p> 
     143NanoA の動作を、プラグインを使って拡張することができます。プラグインは、各コントローラ (.pm ファイルまたは .mt ファイル) で use pluginname; するだけで適用されます。 
     144</p> 
     145 
     146<div class="pre_caption">例. セッションプラグインの適用 (.pm)</div> 
     147<pre> 
     148use plugin::session; 
     149</pre> 
     150 
     151<div class="pre_caption">例. セッションプラグインの適用 (.mt)</div> 
     152<pre> 
     153&#x3f; use plugin::session; 
     154</pre> 
     155 
     156<p> 
     157また、myapp/config.pm 内で use pluginname; すれば、同ディレクトリ内の全コントローラにプラグインが適用されます。例えば、全コントローラでモバイル対応機能を有効化したい、という場合は、こちらの手法が便利です。 
     158</p> 
     159 
     160<h2 id="session">セッションプラグイン</h2> 
     161 
     162<p> 
     163セッションプラグインをロードすると、自動的にセッションクッキーが発行されるようになります。また、$app->session() 関数を呼んで HTTP::Session オブジェクトを取得し、認証処理等を実現可能です。  
    144164</p> 
    145165 
     
    149169</pre> 
    150170 
     171<p> 
    151172<div class="pre_caption">セッション機能の有効化 (.mt)</div> 
    152173<pre> 
     
    154175</pre> 
    155176 
     177</p> 
     178 
    156179<div class="pre_caption">セッションオブジェクトの取得</div> 
    157180<pre> 
     
    159182</pre> 
    160183 
    161 <h2 id="mobile">ケータイ対応</h2> 
    162  
    163 <p> 
    164 各コントローラ (.pm ファイルまたは .mt ファイル) の中で plugin::mobile をロードすると、携帯端末にあわせて自動的に文字コードを変換して送受信するようになります。 
     184<h2 id="mobile">モバイルプラグイン</h2> 
     185 
     186<p> 
     187モバイルプラグインをロードすると、携帯端末にあわせて自動的に文字コードを変換して送受信するようになります。 
    165188</p> 
    166189 
  • lang/perl/NanoA/trunk/lib/NanoA.pm

    r24547 r24551  
    4343    my $self = shift; 
    4444    my $mode = shift; 
    45     my $hooks = $HOOKS{$mode}->{ref $self} 
    46         or return; 
    47     $_->($self, @_) 
    48         for @$hooks; 
     45    my $klass = ref $self; 
     46    my @hooks; 
     47    if (my $h = $HOOKS{$mode}->{$klass}) { 
     48        push @hooks, @$h; 
     49    } 
     50    if ($klass =~ /^([^:]+)/) { 
     51        if (my $h = $HOOKS{$mode}->{$1 . '::config'}) { 
     52            push @hooks, @$h; 
     53        } 
     54    } 
     55    return 
     56        unless @hooks; 
     57    $_->[0]->($self, @_) 
     58        for sort { $a->[1] <=> $b->[1] } @hooks; 
    4959} 
    5060 
    5161sub register_hook { 
    52     my ($klass, $mode, $func) = @_; 
    53     die "unknown hook: $mode\n" 
     62    my ($klass, $mode, $func, $prio) = @_; 
     63    $prio ||= 50; 
     64    die 'unknown hook: ' . $mode. "\n" 
    5465        unless $HOOKS{$mode}; 
    55     my $target = $HOOKS{$mode}->{ref $klass || $klass} ||= []; 
    56     return 
    57         if grep { $_ == $func } @$target; 
    58     push @$target, $func; 
    59     1; 
     66    my $hooks = $HOOKS{$mode}->{ref $klass || $klass} ||= []; 
     67    unless (grep { $_->[0] == $func } @$hooks) { 
     68        push @$hooks, [ 
     69            $func, 
     70            $prio, 
     71        ]; 
     72    } 
    6073} 
    6174 
     
    109122    my ($self, $uri, $status) = @_; 
    110123    $status ||= 302; 
    111     print "Status: $status\nLocation: $uri\n\n"; 
     124    print 'Status: ', $status, "\nLocation: " . $uri . "\n\n"; 
    112125    CGI::ExceptionManager::detach(); 
    113126} 
     
    161174        $ct .= "; charset=" . delete $headers->{-charset}; 
    162175    } 
    163     print "Content-Type: $ct\n"; 
     176    print 'Content-Type: ', $ct, "\n"; 
    164177    foreach my $n (sort keys %$headers) { 
    165178        my $v = $headers->{$n}; 
     
    168181        if (ref $v eq 'ARRAY') { 
    169182            foreach my $vv (@$v) { 
    170                 print ucfirst($n), ": $vv\n"; 
     183                print ucfirst($n), ': ', $vv, "\n"; 
    171184            } 
    172185        } else { 
    173             print ucfirst($n), ": $v\n"; 
     186            print ucfirst($n), ': ', $v, "\n"; 
    174187        } 
    175188    } 
     
    189202    return if $LOADED{$mark_path}; 
    190203    local $@; 
    191     if (do "$path") { 
     204    if (do $path) { 
    192205        $LOADED{$mark_path} = 1; 
    193206        return 1; 
     
    226239sub read_file { 
    227240    my $fname = shift; 
    228     open my $fh, '<:utf8', $fname or die "cannot read $fname:$!"; 
     241    open my $fh, '<:utf8', $fname or die 'cannot read ' . $fname. ":$!"; 
    229242    my $s = do { local $/; join '', <$fh> }; 
    230243    close $fh; 
     
    235248    my $module = shift; 
    236249    no strict 'refs'; 
    237     *{"$module\::$_"} = \&{$_} 
     250    *{$module . '::' . $_} = \&{$_} 
    238251        for qw(escape_html); 
    239252} 
  • lang/perl/NanoA/trunk/plugin/mobile.pm

    r24452 r24551  
    1010    my $pkg = caller; 
    1111    # add funcs to caller 
    12     NanoA::register_hook( 
    13         $pkg, 
    14         'prerun', 
    15         \&_prerun, 
    16     ); 
    17     NanoA::register_hook( 
    18         $pkg, 
    19         'postrun', 
    20         \&_postrun, 
    21     ); 
     12    NanoA::register_hook($pkg, 'prerun', \&_prerun, 0); 
     13    NanoA::register_hook($pkg, 'postrun', \&_postrun, 90); 
    2214} 
    2315 
  • lang/perl/NanoA/trunk/plugin/session.pm

    r24547 r24551  
    1212    my $pkg = caller; 
    1313    # add funcs to caller 
    14     NanoA::register_hook( 
    15         $pkg, 
    16         'postrun', 
    17         \&_postrun, 
    18     ); 
     14    NanoA::register_hook($pkg, 'postrun', \&_postrun); 
    1915} 
     16 
     17sub _postrun { 
     18    my ($app, $bodyref) = @_; 
     19    $app->session->header_filter($app); 
     20} 
     21 
     22no warnings 'redefine'; 
    2023 
    2124sub NanoA::session { 
     
    3134} 
    3235 
    33 sub _postrun { 
    34     my ($app, $bodyref) = @_; 
    35     $app->session->header_filter($app); 
    36 } 
    37  
    38361;