line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nephia::Plugin::ResponseHandler; |
2
|
2
|
|
|
2
|
|
16308
|
use 5.008005; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
77
|
|
3
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
56
|
|
4
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
56
|
|
5
|
2
|
|
|
2
|
|
897
|
use parent 'Nephia::Plugin'; |
|
2
|
|
|
|
|
260
|
|
|
2
|
|
|
|
|
12
|
|
6
|
2
|
|
|
2
|
|
1938
|
use Nephia::Response; |
|
2
|
|
|
|
|
14032
|
|
|
2
|
|
|
|
|
883
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
1
|
|
|
1
|
0
|
18
|
my ($class, %opts) = @_; |
12
|
1
|
|
|
|
|
10
|
my $self = $class->SUPER::new(%opts); |
13
|
1
|
|
|
|
|
62
|
$self->app->{response_handler} = $opts{handler}; |
14
|
1
|
|
33
|
|
|
9
|
$self->app->{response_handler}{HASH} ||= $self->can('_hash_handler'); |
15
|
1
|
|
33
|
|
|
17
|
$self->app->{response_handler}{ARRAY} ||= $self->can('_array_handler'); |
16
|
1
|
|
33
|
|
|
11
|
$self->app->{response_handler}{SCALAR} ||= $self->can('_scalar_handler'); |
17
|
1
|
|
|
|
|
12
|
my $app = $self->app; |
18
|
1
|
|
|
|
|
6
|
$app->action_chain->after('Core', ResponseHandler => $self->can('_response_handler')); |
19
|
1
|
|
|
|
|
120
|
return $self; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _response_handler { |
23
|
5
|
|
|
5
|
|
45856
|
my ($app, $context) = @_; |
24
|
5
|
|
|
|
|
16
|
my $res = $context->get('res'); |
25
|
5
|
|
100
|
|
|
34
|
my $type = ref($res) || 'SCALAR'; |
26
|
5
|
50
|
|
|
|
19
|
if ($app->{response_handler}{$type}) { |
27
|
5
|
|
|
|
|
16
|
$app->{response_handler}{$type}->($app, $context); |
28
|
|
|
|
|
|
|
} |
29
|
5
|
|
|
|
|
403
|
return $context; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _hash_handler { |
33
|
3
|
|
|
3
|
|
4
|
my ($app, $context) = @_; |
34
|
3
|
|
|
|
|
9
|
my $res = $context->get('res'); |
35
|
3
|
100
|
|
|
|
18
|
if ($res->{template}) { |
36
|
2
|
|
|
|
|
7
|
my $template = delete($res->{template}); |
37
|
2
|
|
100
|
|
|
12
|
my $content_type = delete($res->{content_type}) || 'text/html; charset=UTF-8'; |
38
|
2
|
|
|
|
|
12
|
my $res_obj = Nephia::Response->new( |
39
|
|
|
|
|
|
|
200, |
40
|
|
|
|
|
|
|
['Content-Type' => $content_type], |
41
|
|
|
|
|
|
|
$app->dsl('render')->($template, $res) |
42
|
|
|
|
|
|
|
); |
43
|
2
|
|
|
|
|
1753
|
$context->set('res' => $res_obj); |
44
|
2
|
|
|
|
|
10
|
return $res_obj; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
else { |
47
|
1
|
|
|
|
|
6
|
return $app->dsl('json_res')->($res) |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _array_handler { |
52
|
1
|
|
|
1
|
|
1
|
my ($app, $context) = @_; |
53
|
1
|
|
|
|
|
4
|
my $res = $context->get('res'); |
54
|
1
|
|
|
|
|
8
|
$context->set('res' => Nephia::Response->new(@$res)); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _scalar_handler { |
58
|
1
|
|
|
1
|
|
2
|
my ($app, $context) = @_; |
59
|
1
|
|
|
|
|
4
|
my $res = $context->get('res'); |
60
|
1
|
|
|
|
|
11
|
$context->set('res' => Nephia::Response->new(200, ['Content-Type' => 'text/html; charset=UTF-8'], $res)); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
1; |
64
|
|
|
|
|
|
|
__END__ |