line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::ServiceManage; |
2
|
2
|
|
|
2
|
|
92390
|
use Mojo::Base 'Mojolicious::Service'; |
|
2
|
|
|
|
|
19716
|
|
|
2
|
|
|
|
|
10
|
|
3
|
2
|
|
|
2
|
|
200
|
use Carp 'croak'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
72
|
|
4
|
2
|
|
|
2
|
|
523
|
use Mojo::Loader qw/find_modules load_class/; |
|
2
|
|
|
|
|
231100
|
|
|
2
|
|
|
|
|
152
|
|
5
|
2
|
|
|
2
|
|
21
|
use Mojo::Util qw/camelize decamelize/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
78
|
|
6
|
2
|
|
|
2
|
|
9
|
use Scalar::Util; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1236
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has services => sub{{}}; |
11
|
|
|
|
|
|
|
has namespaces=>sub{["Mojolicious::Service"]}; |
12
|
|
|
|
|
|
|
has lazy => 1; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub load_service{ |
16
|
1
|
|
|
1
|
1
|
3
|
my ($self, $name) = @_; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Try all namespaces and full module name |
19
|
1
|
50
|
|
|
|
5
|
my $suffix = $name =~ /^[a-z]/ ? camelize $name : $name; |
20
|
1
|
|
|
|
|
2
|
my @classes = map {"${_}::$suffix"} @{$self->namespaces}; |
|
2
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
3
|
|
21
|
1
|
|
|
|
|
3
|
for my $class (@classes, $name) { |
22
|
3
|
100
|
|
|
|
99
|
if(_load($class)){ |
23
|
1
|
|
|
|
|
25
|
my $service = $class->new(models=>$self->models,dbi=>$self->dbi,app=>$self->app); |
24
|
1
|
|
|
|
|
26
|
Scalar::Util::weaken $service->{app}; |
25
|
1
|
|
|
|
|
6
|
$self->service($name,$service); |
26
|
1
|
|
|
|
|
3
|
return $service; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Not found |
31
|
0
|
|
|
|
|
0
|
die qq{Service "$name" missing, maybe you need to install it?\n}; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub load_all_service{ |
35
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
36
|
0
|
|
|
|
|
0
|
foreach(map{find_modules($_)}@{$self->namespaces}){ |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
37
|
0
|
|
|
|
|
0
|
$_=~/^.+\:([^\:]+)$/; |
38
|
0
|
|
|
|
|
0
|
my $name = decamelize($1); |
39
|
0
|
0
|
|
|
|
0
|
if(_load($_)){ |
40
|
0
|
|
|
|
|
0
|
my $service = $_->new(models=>$self->models,dbi=>$self->dbi,app=>$self->app); |
41
|
0
|
|
|
|
|
0
|
Scalar::Util::weaken $service->{app}; |
42
|
0
|
|
|
|
|
0
|
$self->service($name,$service); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub new{ |
48
|
1
|
|
|
1
|
1
|
82
|
my ($self,$conf) = @_; |
49
|
1
|
50
|
|
|
|
6
|
my $namespaces = delete $conf->{namespaces} if($conf->{namespaces}); |
50
|
1
|
|
|
|
|
10
|
$self = $self->SUPER::new($conf); |
51
|
1
|
50
|
33
|
|
|
18
|
if($namespaces && ref $namespaces eq "ARRAY"){ |
52
|
1
|
|
|
|
|
4
|
unshift(@{$self->namespaces},$_) for(reverse @$namespaces); |
|
1
|
|
|
|
|
4
|
|
53
|
|
|
|
|
|
|
} |
54
|
1
|
50
|
|
|
|
4
|
$self->load_all_service unless($self->lazy); |
55
|
1
|
|
|
|
|
9
|
return $self; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub service{ |
59
|
2
|
|
|
2
|
1
|
19
|
my ($self, $name, $service) = @_; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Set service |
62
|
2
|
100
|
|
|
|
6
|
if($service){ |
63
|
1
|
|
|
|
|
3
|
$self->services->{$name} = $service; |
64
|
1
|
|
|
|
|
6
|
return $self; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
1
|
50
|
|
|
|
3
|
unless($self->services->{$name}){ |
68
|
1
|
|
|
|
|
4
|
$self->load_service($name); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Check services existence |
72
|
1
|
50
|
|
|
|
3
|
croak qq{service "$name" is not yet created } unless($self->services->{$name}); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Get service |
75
|
1
|
|
|
|
|
7
|
return $self->services->{$name}; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub _load { |
81
|
3
|
|
|
3
|
|
6
|
my $module = shift; |
82
|
3
|
100
|
|
|
|
9
|
return $module->isa('Mojolicious::Service') unless my $e = load_class $module; |
83
|
1
|
50
|
|
|
|
198
|
ref $e ? die $e : return undef; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 NAME |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Mojolicious::ServiceManage - Service management for Mojolicious Framework |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 VERSION |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Version 0.01 |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 SYNOPSIS |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
use Mojolicious::ServiceManage |
98
|
|
|
|
|
|
|
my $service_manage = Mojolicious::ServiceManage->new({ |
99
|
|
|
|
|
|
|
dbi=>DBIx::Custom->new(), |
100
|
|
|
|
|
|
|
models=>{}, |
101
|
|
|
|
|
|
|
namespaces=>s["Mojolicious::Service"], |
102
|
|
|
|
|
|
|
lazy => 1 |
103
|
|
|
|
|
|
|
}); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
## fetch a service |
106
|
|
|
|
|
|
|
my $user_service = $service_manage->service("user"); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 DESCRIPTION |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Mojolicious::ServiceManage是为Mojolicious框架提供Service支持的模块。 |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Mojolicious::ServiceManage 从 Mojolicious::Service中继承了所有属性,并实现以下属性。 |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 services |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
存储service的属性。 |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head2 namespaces |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
namespaces 用于说明service类所在的命名空间,这个属性的值是一个arrayref 类型的值,支持在多个命名空间中查找service。 |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 lazy |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
用于说明是否启用懒加载模式。 |
131
|
|
|
|
|
|
|
如果值为true则启用懒加载,只有在实际请求一个service时才加载其类并实例化一个service对象。 |
132
|
|
|
|
|
|
|
如果为flase则在创建Mojolicious::ServiceManage时加载所有service类并实例化成对象。 |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 METHODS |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Mojolicious::ServiceManage 从 Mojolicious::Service中继承了所有方法,并实现以下方法。 |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 load_service |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
根据service的名字加载service。 |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 load_all_service |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
加载 namespaces 属性指定的所有命名空间下的所有service,并实例化。 |
148
|
|
|
|
|
|
|
注:只有在非懒加载模式的初始化阶段才会调用这个方法。 |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 new |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
生成一个新的Mojolicious::ServiceManage对象。 |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 service |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
根据 service 的名称从 services 属性中获取 service。如果在 services 属性中不存在对应的键,则尝试从 namespaces 属性指定的命名空间中加载并实例化一个service。如果尝试加载后仍获取失败,则返回 undef。 |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 AUTHOR |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
wfso, C<< <461663376 at qq.com> >> |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 BUGS |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
170
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
171
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 SUPPORT |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
perldoc Mojolicious::ServiceManage |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
You can also look for information at: |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=over 4 |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
L |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
L |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item * CPAN Ratings |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
L |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item * Search CPAN |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
L |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=back |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Copyright 2017 wfso. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
This program is distributed under the MIT (X11) License: |
214
|
|
|
|
|
|
|
L |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person |
217
|
|
|
|
|
|
|
obtaining a copy of this software and associated documentation |
218
|
|
|
|
|
|
|
files (the "Software"), to deal in the Software without |
219
|
|
|
|
|
|
|
restriction, including without limitation the rights to use, |
220
|
|
|
|
|
|
|
copy, modify, merge, publish, distribute, sublicense, and/or sell |
221
|
|
|
|
|
|
|
copies of the Software, and to permit persons to whom the |
222
|
|
|
|
|
|
|
Software is furnished to do so, subject to the following |
223
|
|
|
|
|
|
|
conditions: |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be |
226
|
|
|
|
|
|
|
included in all copies or substantial portions of the Software. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
229
|
|
|
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
230
|
|
|
|
|
|
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
231
|
|
|
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
232
|
|
|
|
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
233
|
|
|
|
|
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
234
|
|
|
|
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
235
|
|
|
|
|
|
|
OTHER DEALINGS IN THE SOFTWARE. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
1; # End of Mojolicious::ServiceManage |