Changeset 24551 for lang/perl/NanoA
- Timestamp:
- 11/21/08 15:13:04 (5 years ago)
- Location:
- lang/perl/NanoA/trunk
- Files:
-
- 4 modified
-
app/system/tutorial.mt (modified) (4 diffs)
-
lib/NanoA.pm (modified) (7 diffs)
-
plugin/mobile.pm (modified) (1 diff)
-
plugin/session.pm (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/NanoA/trunk/app/system/tutorial.mt
r24548 r24551 138 138 <h2 id="hooks">アプリケーション全体の共通処理</h2> 139 139 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> 143 NanoA の動作を、プラグインを使って拡張することができます。プラグインは、各コントローラ (.pm ファイルまたは .mt ファイル) で use pluginname; するだけで適用されます。 144 </p> 145 146 <div class="pre_caption">例. セッションプラグインの適用 (.pm)</div> 147 <pre> 148 use plugin::session; 149 </pre> 150 151 <div class="pre_caption">例. セッションプラグインの適用 (.mt)</div> 152 <pre> 153 ? 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 オブジェクトを取得し、認証処理等を実現可能です。 144 164 </p> 145 165 … … 149 169 </pre> 150 170 171 <p> 151 172 <div class="pre_caption">セッション機能の有効化 (.mt)</div> 152 173 <pre> … … 154 175 </pre> 155 176 177 </p> 178 156 179 <div class="pre_caption">セッションオブジェクトの取得</div> 157 180 <pre> … … 159 182 </pre> 160 183 161 <h2 id="mobile"> ケータイ対応</h2>162 163 <p> 164 各コントローラ (.pm ファイルまたは .mt ファイル) の中で plugin::mobileをロードすると、携帯端末にあわせて自動的に文字コードを変換して送受信するようになります。184 <h2 id="mobile">モバイルプラグイン</h2> 185 186 <p> 187 モバイルプラグインをロードすると、携帯端末にあわせて自動的に文字コードを変換して送受信するようになります。 165 188 </p> 166 189 -
lang/perl/NanoA/trunk/lib/NanoA.pm
r24547 r24551 43 43 my $self = shift; 44 44 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; 49 59 } 50 60 51 61 sub 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" 54 65 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 } 60 73 } 61 74 … … 109 122 my ($self, $uri, $status) = @_; 110 123 $status ||= 302; 111 print "Status: $status\nLocation: $uri\n\n";124 print 'Status: ', $status, "\nLocation: " . $uri . "\n\n"; 112 125 CGI::ExceptionManager::detach(); 113 126 } … … 161 174 $ct .= "; charset=" . delete $headers->{-charset}; 162 175 } 163 print "Content-Type: $ct\n";176 print 'Content-Type: ', $ct, "\n"; 164 177 foreach my $n (sort keys %$headers) { 165 178 my $v = $headers->{$n}; … … 168 181 if (ref $v eq 'ARRAY') { 169 182 foreach my $vv (@$v) { 170 print ucfirst($n), ": $vv\n";183 print ucfirst($n), ': ', $vv, "\n"; 171 184 } 172 185 } else { 173 print ucfirst($n), ": $v\n";186 print ucfirst($n), ': ', $v, "\n"; 174 187 } 175 188 } … … 189 202 return if $LOADED{$mark_path}; 190 203 local $@; 191 if (do "$path") {204 if (do $path) { 192 205 $LOADED{$mark_path} = 1; 193 206 return 1; … … 226 239 sub read_file { 227 240 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. ":$!"; 229 242 my $s = do { local $/; join '', <$fh> }; 230 243 close $fh; … … 235 248 my $module = shift; 236 249 no strict 'refs'; 237 *{ "$module\::$_"} = \&{$_}250 *{$module . '::' . $_} = \&{$_} 238 251 for qw(escape_html); 239 252 } -
lang/perl/NanoA/trunk/plugin/mobile.pm
r24452 r24551 10 10 my $pkg = caller; 11 11 # 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); 22 14 } 23 15 -
lang/perl/NanoA/trunk/plugin/session.pm
r24547 r24551 12 12 my $pkg = caller; 13 13 # add funcs to caller 14 NanoA::register_hook( 15 $pkg, 16 'postrun', 17 \&_postrun, 18 ); 14 NanoA::register_hook($pkg, 'postrun', \&_postrun); 19 15 } 16 17 sub _postrun { 18 my ($app, $bodyref) = @_; 19 $app->session->header_filter($app); 20 } 21 22 no warnings 'redefine'; 20 23 21 24 sub NanoA::session { … … 31 34 } 32 35 33 sub _postrun {34 my ($app, $bodyref) = @_;35 $app->session->header_filter($app);36 }37 38 36 1;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)