line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Services; |
2
|
3
|
|
|
3
|
|
90910
|
use Mojo::Base 'Mojolicious::Service'; |
|
3
|
|
|
|
|
20657
|
|
|
3
|
|
|
|
|
14
|
|
3
|
3
|
|
|
3
|
|
315
|
use Carp 'croak'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
110
|
|
4
|
3
|
|
|
3
|
|
531
|
use Mojo::Loader qw/find_modules load_class/; |
|
3
|
|
|
|
|
226618
|
|
|
3
|
|
|
|
|
166
|
|
5
|
3
|
|
|
3
|
|
22
|
use Mojo::Util qw/camelize decamelize/; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
109
|
|
6
|
3
|
|
|
3
|
|
14
|
use Scalar::Util; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1998
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '1.0.0'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has services => sub{{}}; |
11
|
|
|
|
|
|
|
has namespaces=>sub{["Mojolicious::Service"]}; |
12
|
|
|
|
|
|
|
has lazy => 1; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub load_service{ |
16
|
2
|
|
|
2
|
1
|
6
|
my ($self, $name) = @_; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Try all namespaces and full module name |
19
|
2
|
100
|
|
|
|
11
|
my $suffix = $name =~ /^[a-z0-9]/ ? camelize $name : $name; |
20
|
2
|
|
|
|
|
23
|
my @classes = map {"${_}::$suffix"} @{$self->namespaces}; |
|
4
|
|
|
|
|
21
|
|
|
2
|
|
|
|
|
6
|
|
21
|
2
|
|
|
|
|
6
|
for my $class (@classes, $name) { |
22
|
4
|
100
|
|
|
|
102
|
if(_load($class)){ |
23
|
2
|
|
|
|
|
54
|
my $service = $class->new(models=>$self->models,dbi=>$self->dbi,app=>$self->app); |
24
|
2
|
|
|
|
|
46
|
Scalar::Util::weaken $service->{app}; |
25
|
2
|
|
|
|
|
7
|
Scalar::Util::weaken $service->{models}; |
26
|
2
|
|
|
|
|
6
|
Scalar::Util::weaken $service->{dbi}; |
27
|
2
|
|
|
|
|
12
|
$self->service($name,$service); |
28
|
2
|
|
|
|
|
5
|
return $service; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Not found |
33
|
0
|
|
|
|
|
0
|
die qq{Service "$name" missing, maybe you need to install it?\n}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub load_all_service{ |
37
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
38
|
0
|
|
|
|
|
0
|
foreach(map{find_modules($_)}@{$self->namespaces}){ |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
39
|
0
|
|
|
|
|
0
|
$_=~/^.+\:([^\:]+)$/; |
40
|
0
|
|
|
|
|
0
|
my $name = decamelize($1); |
41
|
0
|
0
|
|
|
|
0
|
if(_load($_)){ |
42
|
0
|
|
|
|
|
0
|
my $service = $_->new(models=>$self->models,dbi=>$self->dbi,app=>$self->app); |
43
|
0
|
|
|
|
|
0
|
Scalar::Util::weaken $service->{app}; |
44
|
0
|
|
|
|
|
0
|
Scalar::Util::weaken $service->{models}; |
45
|
0
|
|
|
|
|
0
|
Scalar::Util::weaken $service->{dbi}; |
46
|
0
|
|
|
|
|
0
|
$self->service($name,$service); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub new{ |
52
|
2
|
|
|
2
|
1
|
87
|
my ($self,$conf) = @_; |
53
|
2
|
50
|
|
|
|
10
|
my $namespaces = delete $conf->{namespaces} if($conf->{namespaces}); |
54
|
2
|
|
|
|
|
19
|
$self = $self->SUPER::new($conf); |
55
|
2
|
50
|
33
|
|
|
79
|
if($namespaces && ref $namespaces eq "ARRAY"){ |
56
|
2
|
|
|
|
|
8
|
unshift(@{$self->namespaces},$_) for(reverse @$namespaces); |
|
2
|
|
|
|
|
11
|
|
57
|
|
|
|
|
|
|
} |
58
|
2
|
50
|
|
|
|
10
|
$self->load_all_service unless($self->lazy); |
59
|
2
|
|
|
|
|
17
|
return $self; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub service{ |
63
|
4
|
|
|
4
|
1
|
24
|
my ($self, $name, $service) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Set service |
66
|
4
|
100
|
|
|
|
12
|
if($service){ |
67
|
2
|
|
|
|
|
6
|
$self->services->{$name} = $service; |
68
|
2
|
|
|
|
|
11
|
return $self; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
2
|
50
|
|
|
|
8
|
unless($self->services->{$name}){ |
72
|
2
|
|
|
|
|
10
|
$self->load_service($name); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Check services existence |
76
|
2
|
50
|
|
|
|
5
|
croak qq{service "$name" is not yet created } unless($self->services->{$name}); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Get service |
79
|
2
|
|
|
|
|
20
|
return $self->services->{$name}; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _load { |
85
|
4
|
|
|
4
|
|
7
|
my $module = shift; |
86
|
4
|
100
|
|
|
|
12
|
return $module->isa('Mojolicious::Service') unless my $e = load_class $module; |
87
|
1
|
50
|
|
|
|
193
|
ref $e ? die $e : return undef; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=encoding utf8 |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 NAME |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Mojolicious::Services - Mojolicious::Services 是为Mojolicious框架提供的Service管理插件。 |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 VERSION |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Version 0.01 |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 SYNOPSIS |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
use Mojolicious::services |
106
|
|
|
|
|
|
|
my $service_manage = Mojolicious::services->new({ |
107
|
|
|
|
|
|
|
dbi=>DBIx::Custom->new(), |
108
|
|
|
|
|
|
|
models=>{}, |
109
|
|
|
|
|
|
|
namespaces=>s["Mojolicious::Service"], |
110
|
|
|
|
|
|
|
lazy => 1 |
111
|
|
|
|
|
|
|
}); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
## fetch a service |
114
|
|
|
|
|
|
|
my $user_service = $service_manage->service("user"); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 DESCRIPTION |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Mojolicious::services是为Mojolicious框架提供Service支持的模块。 |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Mojolicious::services 从 Mojolicious::Service中继承了所有属性,并实现以下属性。 |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 services |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
存储service的属性。 |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 namespaces |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
namespaces 用于说明service类所在的命名空间,这个属性的值是一个arrayref 类型的值,支持在多个命名空间中查找service。 |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 lazy |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
用于说明是否启用懒加载模式。 |
141
|
|
|
|
|
|
|
如果值为true则启用懒加载,只有在实际请求一个service时才加载其类并实例化一个service对象。 |
142
|
|
|
|
|
|
|
如果为flase则在创建Mojolicious::services时加载所有service类并实例化成对象。 |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 METHODS |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Mojolicious::services 从 Mojolicious::Service中继承了所有方法,并实现以下方法。 |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 load_service |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
根据service的名字加载service。 |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 load_all_service |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
加载 namespaces 属性指定的所有命名空间下的所有service,并实例化。 |
158
|
|
|
|
|
|
|
注:只有在非懒加载模式的初始化阶段才会调用这个方法。 |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 new |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
生成一个新的Mojolicious::services对象。 |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 service |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
根据 service 的名称从 services 属性中获取 service。如果在 services 属性中不存在对应的键,则尝试从 namespaces 属性指定的命名空间中加载并实例化一个service。如果尝试加载后仍获取失败,则返回 undef。 |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 AUTHOR |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
wfso, C<< <461663376@qq.com> >> |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 BUGS |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
180
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
181
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 SUPPORT |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
perldoc Mojolicious::services |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
You can also look for information at: |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=over 4 |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
L |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
L |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=item * CPAN Ratings |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
L |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=item * Search CPAN |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
L |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=back |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
1; # End of Mojolicious::services |