line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Service; |
2
|
2
|
|
|
2
|
|
11518
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
12
|
|
3
|
2
|
|
|
2
|
|
731
|
use Mojo::Loader qw/load_class/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
74
|
|
4
|
2
|
|
|
2
|
|
25
|
use Scalar::Util; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
706
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub register{ |
8
|
1
|
|
|
1
|
1
|
33
|
my ($self, $app, $conf) = @_; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Merge config |
11
|
1
|
50
|
|
|
|
8
|
$conf = {%{$conf},%{$app->config->{service_config}}} if($app->config->{service_config}); |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
12
|
1
|
|
|
|
|
15
|
$conf->{app} = $app; |
13
|
1
|
50
|
|
|
|
3
|
if($app->renderer->get_helper("dbi")){ |
14
|
0
|
|
0
|
|
|
0
|
$conf->{dbi} ||= $app->dbi; |
15
|
0
|
0
|
|
|
|
0
|
if($app->dbi->can("models")){ |
16
|
0
|
|
0
|
|
|
0
|
$conf->{models} ||= $app->dbi->models; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
1
|
50
|
|
|
|
218
|
if($app->renderer->get_helper("models")){ |
21
|
0
|
|
0
|
|
|
0
|
$conf->{models} ||= $app->models; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
1
|
|
50
|
|
|
163
|
my $services_class = delete $conf->{services_class} || 'Mojolicious::Services'; |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
|
|
4
|
my $e = load_class($services_class); |
27
|
1
|
50
|
|
|
|
31
|
if($e){ |
|
|
50
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
0
|
ref $e ? die $e:die "can't fond the module named '$services_class' ,inspect your installed please"; |
29
|
0
|
|
|
|
|
0
|
return undef; |
30
|
|
|
|
|
|
|
}elsif($services_class->isa("Mojolicious::Services")){ |
31
|
1
|
|
|
|
|
5
|
my $services = $services_class->new($conf); |
32
|
1
|
|
|
|
|
4
|
Scalar::Util::weaken $services->{app}; |
33
|
1
|
|
|
|
|
3
|
Scalar::Util::weaken $services->{dbi}; |
34
|
1
|
|
|
|
|
3
|
Scalar::Util::weaken $services->{models}; |
35
|
|
|
|
|
|
|
$app->helper(service=>sub{ |
36
|
1
|
|
|
1
|
|
166
|
my ($c,$name) = @_; |
37
|
1
|
|
|
|
|
5
|
return $services->service($name);; |
38
|
|
|
|
|
|
|
} |
39
|
1
|
|
|
|
|
7
|
); |
40
|
|
|
|
|
|
|
}else{ |
41
|
0
|
|
|
|
|
|
$app->log->fatal("services_class named '$services_class' is not a subclass for Mojolicious::Services "); |
42
|
0
|
|
|
|
|
|
die "services_class named '$services_class' is not a subclass for Mojolicious::Services "; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=encoding utf8 |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 NAME |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Mojolicious::Plugin::Service - 向Mojolicious框架中引入Service管理器的插件! |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 DESCRIPTION |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
向Mojolicious框架中引入Service管理器的插件。 |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 SYNOPSIS |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Mojolicious |
64
|
|
|
|
|
|
|
$app->plugin('Service',$config); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Mojolicious::Lite |
67
|
|
|
|
|
|
|
plugin('DefaultHelpers',$config); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 METHODS |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Mojolicious::Plugin::Service inherits all methods from Mojolicious::Plugin and implements the following new ones. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 register |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
$plugin->register(Mojolicious->new,$config); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Register helper in Mojolicious application named service. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 config Option |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
register 方法中除接受Mojolicious对象为参数外,还接受一个config参数。这个config参数是必须是一个hashref。 |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
{ |
87
|
|
|
|
|
|
|
dbi=>DBIx::Custom->new(), |
88
|
|
|
|
|
|
|
models=>DBIx::Custom->new->models, |
89
|
|
|
|
|
|
|
namespaces=>s["Mojolicious::Service"], |
90
|
|
|
|
|
|
|
services_class=>"T::Services", |
91
|
|
|
|
|
|
|
lazy => 1 |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 dbi |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
dbi 是为service提供数据库操作接口的对象。 |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 models |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
models 是为service提供数据模型操作接口的对象。 |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 namespace |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
namespace 用于说明service类所在的命名空间,这个属性的值是一个arrayref 类型的值,支持在多个命名空间中查找service。 |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 lazy |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
用于说明是否启用懒加载模式。 |
113
|
|
|
|
|
|
|
如果值为true则启用懒加载,只有在实际请求一个service时才加载其类并实例化一个service对象。 |
114
|
|
|
|
|
|
|
如果为flase则在创建Mojolicious::Services时加载所有service类并实例化成对象。 |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 services_class |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
用户自己实现一个Mojolicious::Services的子类。作为插件中的service管理器对象。 |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 AUTHOR |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
wfso, C<< <461663376@qq.com> >> |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 BUGS |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
129
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
130
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 SUPPORT |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
perldoc Mojolicious::Plugin::Service |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
You can also look for information at: |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=over 4 |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
L |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
L |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * CPAN Ratings |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
L |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * Search CPAN |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
L |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=back |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
1; # End of Mojolicious::Plugin::Service |