line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Amon2; |
2
|
25
|
|
|
25
|
|
2803323
|
use strict; |
|
25
|
|
|
|
|
182
|
|
|
25
|
|
|
|
|
800
|
|
3
|
25
|
|
|
25
|
|
137
|
use warnings; |
|
25
|
|
|
|
|
51
|
|
|
25
|
|
|
|
|
647
|
|
4
|
25
|
|
|
25
|
|
676
|
use 5.008001; |
|
25
|
|
|
|
|
105
|
|
5
|
25
|
|
|
25
|
|
8913
|
use Amon2::Util (); |
|
25
|
|
|
|
|
62
|
|
|
25
|
|
|
|
|
586
|
|
6
|
25
|
|
|
25
|
|
8620
|
use Plack::Util (); |
|
25
|
|
|
|
|
143715
|
|
|
25
|
|
|
|
|
492
|
|
7
|
25
|
|
|
25
|
|
165
|
use Carp (); |
|
25
|
|
|
|
|
50
|
|
|
25
|
|
|
|
|
484
|
|
8
|
25
|
|
|
25
|
|
10622
|
use Amon2::Config::Simple; |
|
25
|
|
|
|
|
79
|
|
|
25
|
|
|
|
|
814
|
|
9
|
25
|
|
|
25
|
|
9478
|
use Amon2::ContextGuard; |
|
25
|
|
|
|
|
77
|
|
|
25
|
|
|
|
|
7920
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '6.15'; |
12
|
|
|
|
|
|
|
{ |
13
|
|
|
|
|
|
|
our $CONTEXT; # You can localize this variable in your application. |
14
|
16
|
|
|
16
|
1
|
117
|
sub context { $CONTEXT } |
15
|
3
|
|
|
3
|
1
|
9
|
sub set_context { $CONTEXT = $_[1] } |
16
|
|
|
|
|
|
|
sub context_guard { |
17
|
23
|
|
|
23
|
1
|
130
|
Amon2::ContextGuard->new($_[0], \$CONTEXT); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
43
|
|
|
43
|
1
|
3756
|
my $class = shift; |
23
|
43
|
50
|
|
|
|
188
|
my %args = @_ == 1 ? %{ $_[0] } : @_; |
|
0
|
|
|
|
|
0
|
|
24
|
43
|
|
|
|
|
203
|
bless { %args }, $class; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# For CLI. |
28
|
|
|
|
|
|
|
sub bootstrap { |
29
|
4
|
|
|
4
|
|
428
|
my $class = shift; |
30
|
4
|
|
|
|
|
39
|
my $self = $class->new(@_); |
31
|
4
|
|
|
|
|
46
|
$class->set_context($self); |
32
|
4
|
|
|
|
|
10
|
return $self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Class method. |
36
|
|
|
|
|
|
|
sub base_dir { |
37
|
0
|
|
0
|
0
|
1
|
0
|
my $proto = ref $_[0] || $_[0]; |
38
|
0
|
|
|
|
|
0
|
my $base_dir = Amon2::Util::base_dir($proto); |
39
|
0
|
|
|
0
|
|
0
|
Amon2::Util::add_method($proto, 'base_dir', sub { $base_dir }); |
|
0
|
|
|
|
|
0
|
|
40
|
0
|
|
|
|
|
0
|
$base_dir; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
0
|
1
|
0
|
sub load_config { Amon2::Config::Simple->load(shift) } |
44
|
|
|
|
|
|
|
sub config { |
45
|
1
|
|
|
1
|
1
|
3
|
my $class = shift; |
46
|
1
|
|
33
|
|
|
7
|
$class = ref $class || $class; |
47
|
1
|
50
|
|
|
|
4
|
die "Do not call Amon2->config() directly." if __PACKAGE__ eq $class; |
48
|
25
|
|
|
25
|
|
187
|
no strict 'refs'; |
|
25
|
|
|
|
|
73
|
|
|
25
|
|
|
|
|
10618
|
|
49
|
1
|
|
|
|
|
6
|
my $config = $class->load_config(); |
50
|
1
|
|
|
5
|
|
11
|
*{"$class\::config"} = sub { $config }; # Class cache. |
|
1
|
|
|
|
|
6
|
|
|
5
|
|
|
|
|
26
|
|
51
|
1
|
|
|
|
|
6
|
return $config; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
0
|
1
|
0
|
sub mode_name { $ENV{PLACK_ENV} } |
55
|
|
|
|
|
|
|
|
56
|
2
|
100
|
|
2
|
1
|
5323
|
sub debug_mode { $ENV{AMON2_DEBUG} ? 1 : 0 } |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub add_config { |
59
|
2
|
50
|
|
2
|
1
|
155
|
my ($class, $key, $hash) = @_; $hash or Carp::croak("missing args: \$hash"); |
|
2
|
|
|
|
|
7
|
|
60
|
2
|
|
|
|
|
315
|
Carp::cluck("Amon2->add_config() method was deprecated."); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# This method will be deprecated. |
63
|
|
|
|
|
|
|
$class->config->{$key} = +{ |
64
|
2
|
50
|
|
|
|
9
|
%{$class->config->{$key} || +{}}, |
65
|
2
|
|
|
|
|
85
|
%{$hash}, |
|
2
|
|
|
|
|
8
|
|
66
|
|
|
|
|
|
|
}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# ------------------------------------------------------------------------- |
70
|
|
|
|
|
|
|
# Pluggable things. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub load_plugins { |
73
|
11
|
|
|
11
|
1
|
27218
|
my ($class, @args) = @_; |
74
|
11
|
|
|
|
|
42
|
while (@args) { |
75
|
13
|
|
|
|
|
48
|
my $module = shift @args; |
76
|
13
|
100
|
100
|
|
|
81
|
my $conf = @args>0 && ref($args[0]) eq 'HASH' ? shift @args : undef; |
77
|
13
|
|
|
|
|
104
|
$class->load_plugin($module, $conf); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub load_plugin { |
82
|
15
|
|
|
15
|
1
|
355
|
my ($class, $module, $conf) = @_; |
83
|
15
|
|
|
|
|
88
|
$module = Plack::Util::load_class($module, 'Amon2::Plugin'); |
84
|
15
|
|
|
|
|
254
|
$module->init($class, $conf); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# ------------------------------------------------------------------------- |
88
|
|
|
|
|
|
|
# Local context maker. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub make_local_context { |
91
|
1
|
|
|
1
|
0
|
126
|
my $class = shift; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
## Mo critic. |
94
|
1
|
|
|
7
|
|
87
|
eval sprintf(q{ |
|
7
|
|
|
1
|
|
8179
|
|
|
1
|
|
|
3
|
|
33
|
|
|
3
|
|
|
|
|
35
|
|
95
|
|
|
|
|
|
|
package %s; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub context { $%s::CONTEXT } |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub set_context { $%s::CONTEXT = $_[1] } |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub context_guard { |
102
|
|
|
|
|
|
|
Amon2::ContextGuard->new($_[0], \$%s::CONTEXT); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
}, $class, $class, $class, $class); |
105
|
1
|
50
|
|
|
|
8
|
die $@ if $@; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
__END__ |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=encoding utf-8 |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 NAME |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Amon2 - lightweight web application framework |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 SYNOPSIS |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
package MyApp; |
120
|
|
|
|
|
|
|
use parent qw/Amon2/; |
121
|
|
|
|
|
|
|
use Amon2::Config::Simple; |
122
|
|
|
|
|
|
|
sub load_config { Amon2::Config::Simple->load(shift) } |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 DESCRIPTION |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Amon2 is simple, readable, extensible, B<STABLE>, B<FAST> web application framework based on L<Plack>. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 METHODS |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 CLASS METHODS for C<< Amon2 >> class |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=over 4 |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item my $c = MyApp->context(); |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Get the context object. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item MyApp->set_context($c) |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Set your context object(INTERNAL USE ONLY). |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=back |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 CLASS METHODS for inherited class |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=over 4 |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item C<< MyApp->config() >> |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
This method returns configuration information. It is generated by C<< MyApp->load_config() >>. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item C<< MyApp->mode_name() >> |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
This is a mode name for Amon2. The default implementation of this method is: |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub mode_name { $ENV{PLACK_ENV} } |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
You can override this method if you want to determine the mode by other method. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item C<< MyApp->new() >> |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Create new context object. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item C<< MyApp->bootstrap() >> |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
my $c = MyApp->bootstrap(); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Create new context object and set it to global context. When you are writing CLI script, setup the global context object by this method. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item C<< MyApp->base_dir() >> |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This method returns the application base directory. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=item C<< MyApp->load_plugin($module_name[, \%config]) >> |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This method loads the plugin for the application. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
I<$module_name> package name of the plugin. You can write it as two form like L<DBIx::Class>: |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
__PACKAGE__->load_plugin("Web::CSRFDefender"); # => loads Amon2::Plugin::Web::CSRFDefender |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
If you want to load a plugin in your own name space, use the '+' character before a package name, like following: |
183
|
|
|
|
|
|
|
__PACKAGE__->load_plugin("+MyApp::Plugin::Foo"); # => loads MyApp::Plugin::Foo |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item C<< MyApp->load_plugins($module_name[, \%config ], ...) >> |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Load multiple plugins at one time. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
If you want to load a plugin in your own name space, use the '+' character before a package name like following: |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
__PACKAGE__->load_plugins("+MyApp::Plugin::Foo"); # => loads MyApp::Plugin::Foo |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=item C<< MyApp->load_config() >> |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
You can get a configuration hashref from C<< config/$ENV{PLACK_ENV}.pl >>. You can override this method for customizing configuration loading method. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item C<< MyApp->add_config() >> |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
DEPRECATED. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=item C<< MyApp->debug_mode() >> |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
B<((EXPERIMENTAL))> |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
This method returns a boolean value. It returns true when $ENV{AMON2_DEBUG} is true value, false otherwise. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
You can override this method if you need. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=back |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 PROJECT LOCAL MODE |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
B<THIS MODE IS HIGHLY EXPERIMENTAL> |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Normally, Amon2's context is stored in a global variable. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
This module makes the context to project local. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
It means, normally context class using Amon2 use C<$Amon2::CONTEXT> in each project, but context class using L</PROJECT LOCAL MODE> use C<$MyApp::CONTEXT>. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
B<<< It means you can't use code depend C<<Amon2->context>> and C<<Amon2->context>> under this mode. >>>> |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 NOTES ABOUT create_request |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Older L<Amon2::Web::Request> has only 1 argument like following, it uses C<< Amon2->context >> to get encoding: |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
sub create_request { |
228
|
|
|
|
|
|
|
my ($class, $env) = @_; |
229
|
|
|
|
|
|
|
Amon2::Web::Request->new($env); |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
If you want to use L</PROJECT LOCAL MODE>, you need to pass class name of context class, as following: |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
sub create_request { |
235
|
|
|
|
|
|
|
my ($class, $env) = @_; |
236
|
|
|
|
|
|
|
Amon2::Web::Request->new($env, $class); |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head2 HOW DO I ENABLE PROJECT LOCAL MODE? |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
C< MyApp->make_local_context() > turns on the project local mode. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
There is no way to revert it, thanks. |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 METHODS |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
This module inserts 3 methods to your context class. |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=over 4 |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=item MyApp->context() |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Shorthand for $MyApp::CONTEXT |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=item MyApp->set_context($context) |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
It's the same as: |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
$MyApp::CONTEXT = $context |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item my $guard = MyApp->context_guard() |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
Create new context guard class. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
It's the same as: |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
Amon2::ContextGuard->new(shift, \$MyApp::CONTEXT); |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=back |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head1 DOCUMENTS |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
More complicated documents are available on L<http://amon.64p.org/> |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=head1 SUPPORTS |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
#amon at irc.perl.org is also available. |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head1 AUTHOR |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
Tokuhiro Matsuno E<lt>tokuhirom@gmail.comE<gt> |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=over 4 |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=item noblejasper |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=item hiratara |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=item s-aska |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=item Kentaro Kuribayashi |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=item Yuki Ibe |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=item mattn |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=item Masahiro Nagano |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=item rightgo09 |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=item karupanerura |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=item hatyuki |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=item Keiji, Yoshimi |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=item Nishibayashi Takuji |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=item dragon3 |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=item Fuji, Goro |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=item issm |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=item hisaichi5518 |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=item Adrian |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
=item Fuji, Goro |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=item ITO Nobuaki |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
=item Geraud CONTINSOUZAS |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=item Syohei YOSHIDA |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
=item magnolia |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=item Katsuhiro Konishi |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
=back |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
=head1 LICENSE |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |