| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
4
|
|
|
4
|
|
2027603
|
use 5.008001; |
|
|
4
|
|
|
|
|
21
|
|
|
|
4
|
|
|
|
|
142
|
|
|
2
|
4
|
|
|
4
|
|
21
|
use strict; |
|
|
4
|
|
|
|
|
6
|
|
|
|
4
|
|
|
|
|
139
|
|
|
3
|
4
|
|
|
4
|
|
20
|
use warnings; |
|
|
4
|
|
|
|
|
5
|
|
|
|
4
|
|
|
|
|
248
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Dancer2::Plugin::Adapter; |
|
6
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:YANICK'; |
|
7
|
|
|
|
|
|
|
# ABSTRACT: Wrap any simple class as a service for Dancer2 |
|
8
|
|
|
|
|
|
|
# VERSION |
|
9
|
|
|
|
|
|
|
$Dancer2::Plugin::Adapter::VERSION = '0.006'; |
|
10
|
4
|
|
|
4
|
|
2322
|
use Dancer2::Plugin; |
|
|
4
|
|
|
|
|
11413
|
|
|
|
4
|
|
|
|
|
37
|
|
|
11
|
4
|
|
|
4
|
|
2750
|
use Dancer2; |
|
|
4
|
|
|
|
|
11
|
|
|
|
4
|
|
|
|
|
33
|
|
|
12
|
4
|
|
|
4
|
|
47851
|
use Class::Load qw/try_load_class/; |
|
|
4
|
|
|
|
|
11
|
|
|
|
4
|
|
|
|
|
2448
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my %singletons; |
|
15
|
|
|
|
|
|
|
my $conf; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# called with ($dsl, $name, $object) |
|
18
|
|
|
|
|
|
|
my %save_by_scope = ( |
|
19
|
|
|
|
|
|
|
singleton => sub { $singletons{ $_[1] } = $_[2] }, |
|
20
|
|
|
|
|
|
|
request => sub { |
|
21
|
|
|
|
|
|
|
my $dsl = shift; |
|
22
|
|
|
|
|
|
|
my $hr = $dsl->var("_dpa") || {}; |
|
23
|
|
|
|
|
|
|
$hr->{ $_[0] } = $_[1]; |
|
24
|
|
|
|
|
|
|
$dsl->var( "_dpa", $hr ); |
|
25
|
|
|
|
|
|
|
}, |
|
26
|
|
|
|
|
|
|
none => sub { }, |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# called with ($dsl, $name) |
|
30
|
|
|
|
|
|
|
my %fetch_by_scope = ( |
|
31
|
|
|
|
|
|
|
singleton => sub { $singletons{ $_[1] } }, |
|
32
|
|
|
|
|
|
|
request => sub { my $hr = $_[0]->var("_dpa") || {}; $hr->{ $_[1] }; }, |
|
33
|
|
|
|
|
|
|
none => sub { }, |
|
34
|
|
|
|
|
|
|
); |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
register service => sub { |
|
37
|
18
|
|
|
18
|
|
1295176
|
my ( $dsl, $name ) = plugin_args(@_); |
|
38
|
|
|
|
|
|
|
|
|
39
|
18
|
50
|
|
|
|
90
|
unless ($name) { |
|
40
|
0
|
|
|
|
|
0
|
die "Dancer2::Plugin::Adapter::service() requires a name argument"; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
18
|
|
66
|
|
|
60
|
$conf ||= plugin_setting(); |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# ensure service is defined |
|
46
|
18
|
50
|
|
|
|
537
|
my $object_conf = $conf->{$name} |
|
47
|
|
|
|
|
|
|
or die "No configuration for Adapter '$name'"; |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# set scope, but default to 'request' if not set |
|
50
|
18
|
|
100
|
|
|
69
|
my $scope = $conf->{$name}{scope} || 'request'; |
|
51
|
18
|
50
|
|
|
|
59
|
unless ( $fetch_by_scope{$scope} ) { |
|
52
|
0
|
|
|
|
|
0
|
die "Scope '$scope' is invalid"; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# return cached object if already created |
|
56
|
18
|
|
|
|
|
49
|
my $cached = $fetch_by_scope{$scope}->($dsl, $name); |
|
57
|
18
|
100
|
|
|
|
66
|
return $cached if defined $cached; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# otherwise, instantiate the object from config settings |
|
60
|
13
|
50
|
|
|
|
42
|
my $class = $object_conf->{class} |
|
61
|
|
|
|
|
|
|
or die "No class specified for Adapter '$name'"; |
|
62
|
|
|
|
|
|
|
|
|
63
|
13
|
50
|
|
|
|
52
|
try_load_class($class) |
|
64
|
|
|
|
|
|
|
or die "Module '$class' could not be loaded"; |
|
65
|
|
|
|
|
|
|
|
|
66
|
13
|
|
100
|
|
|
450
|
my $new = $object_conf->{constructor} || 'new'; |
|
67
|
13
|
|
|
|
|
24
|
my $options = $object_conf->{options}; |
|
68
|
|
|
|
|
|
|
|
|
69
|
13
|
100
|
|
|
|
75
|
my @options = |
|
|
|
50
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
ref($options) eq 'HASH' ? %$options |
|
71
|
|
|
|
|
|
|
: ref($options) eq 'ARRAY' ? @$options |
|
72
|
|
|
|
|
|
|
: defined($options) ? $options |
|
73
|
|
|
|
|
|
|
: (); |
|
74
|
|
|
|
|
|
|
|
|
75
|
13
|
50
|
|
|
|
21
|
my $object = eval { $class->$new(@options) } |
|
|
13
|
|
|
|
|
65
|
|
|
76
|
|
|
|
|
|
|
or die "Could not create $class object: $@"; |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# cache by scope |
|
79
|
13
|
|
|
|
|
4462
|
$save_by_scope{$scope}->( $dsl, $name, $object ); |
|
80
|
13
|
|
|
|
|
124
|
return $object; |
|
81
|
|
|
|
|
|
|
}; |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
register_plugin for_versions => [ 2 ]; |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# vim: ts=2 sts=2 sw=2 et: |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
__END__ |