line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Service; |
2
|
1
|
|
|
1
|
|
1079
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
3
|
1
|
|
|
1
|
|
724
|
use Mojo::Loader qw/load_class/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5
|
use Scalar::Util; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
369
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub register{ |
8
|
0
|
|
|
0
|
1
|
|
my ($self, $app, $conf) = @_; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Merge config |
11
|
0
|
0
|
|
|
|
|
$conf = {%{$conf},%{$app->config->{service_config}}} if($app->config->{service_config}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
12
|
0
|
|
|
|
|
|
$conf->{app} = $app; |
13
|
0
|
0
|
|
|
|
|
if($app->renderer->get_helper("dbi")){ |
14
|
0
|
|
0
|
|
|
|
$conf->{dbi} ||= $app->dbi; |
15
|
0
|
0
|
|
|
|
|
if($app->dbi->can("models")){ |
16
|
0
|
|
0
|
|
|
|
$conf->{models} ||= $app->dbi->models; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
0
|
0
|
|
|
|
|
if($app->renderer->get_helper("models")){ |
21
|
0
|
|
0
|
|
|
|
$conf->{models} ||= $app->models; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
0
|
|
|
|
my $services_class = delete $conf->{services_class} || 'Mojolicious::Services'; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
my $e = load_class($services_class); |
27
|
0
|
0
|
|
|
|
|
if($e){ |
|
|
0
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
ref $e ? die $e:die "can't fond the module named '$services_class' ,inspect your installed please"; |
29
|
0
|
|
|
|
|
|
return undef; |
30
|
|
|
|
|
|
|
}elsif($services_class->isa("Mojolicious::Services")){ |
31
|
0
|
|
|
|
|
|
my $services = $services_class->new($conf); |
32
|
0
|
|
|
|
|
|
Scalar::Util::weaken $services->{app}; |
33
|
0
|
|
|
|
|
|
Scalar::Util::weaken $services->{dbi}; |
34
|
0
|
|
|
|
|
|
Scalar::Util::weaken $services->{models}; |
35
|
|
|
|
|
|
|
$app->helper(service=>sub{ |
36
|
0
|
|
|
0
|
|
|
my ($c,$name) = @_; |
37
|
0
|
|
|
|
|
|
return $services->service($name)->c($c); |
38
|
|
|
|
|
|
|
} |
39
|
0
|
|
|
|
|
|
); |
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=>["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 |