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