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