Plugin/Service.pm

filter: Statementstmt 28.5% 10 / 35 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 60.0% 3 / 5 Podpod 100% 1 / 1 totaltotal 19.1% 14 / 73 CC 12 SCAR 47.6
CC
12
Cov
17%
register
156.0
__ANON__
2.0
BEGIN
1.0
CRAP 117.3
« Service.pm ↑ Summary Services.pm »
1 package Mojolicious::Plugin::Service;
2 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Mojo::Loader qw/load_class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 0 ▶sub register{
Subroutine register: not called CC=12 SCAR=50.5
Pod documented
8 0 my ($self, $app, $conf) = @_;
9
10 # Merge config
11 0 ▶ $conf = {%{$conf},%{$app->config->{service_config}}} if($app->config->{service_config});
Branch: 1 branch
BranchTrueFalseTotal
if $app->config->{"service_config"} 0 0 0
12 0 $conf->{app} = $app;
13 0 ▶ if($app->renderer->get_helper("dbi")){
Branch: 1 branch
BranchTrueFalseTotal
if ($app->renderer->get_helper("dbi")) 0 0 0
14 0 ▶ $conf->{dbi} ||= $app->dbi;
Condition0%0/3
$conf->{"dbi"} ||= $app->dbi
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf->{"dbi"} ||= $app->dbi
$conf->{"dbi"} $app->dbi
Truth table0%0/3
A ||= B
A= $conf->{"dbi"}B= $app->dbi
ABresult
1X1
011
000
15 0 ▶ if($app->dbi->can("models")){
Branch: 1 branch
BranchTrueFalseTotal
if ($app->dbi->can("models")) 0 0 0
16 0 ▶ $conf->{models} ||= $app->dbi->models;
Condition0%0/3
$conf->{"models"} ||= $app->dbi->models
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf->{"models"} ||= $app->dbi->models
$conf->{"models"} $app->dbi->models
Truth table0%0/3
A ||= B
A= $conf->{"models"}B= $app->dbi->models
ABresult
1X1
011
000
17 }
18 }
19
20 0 ▶ if($app->renderer->get_helper("models")){
Branch: 1 branch
BranchTrueFalseTotal
if ($app->renderer->get_helper("models")) 0 0 0
21 0 ▶ $conf->{models} ||= $app->models;
Condition0%0/3
$conf->{"models"} ||= $app->models
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf->{"models"} ||= $app->models
$conf->{"models"} $app->models
Truth table0%0/3
A ||= B
A= $conf->{"models"}B= $app->models
ABresult
1X1
011
000
22 }
23
24 0 ▶ my $services_class = delete $conf->{services_class} || 'Mojolicious::Services';
Condition0%0/2
delete $conf->{'services_class'} || 'Mojolicious::Services'
l!l
00
MC/DC0%0/1
delete $conf->{'services_class'} || 'Mojolicious::Services'
delete $conf->{'services_class'}
Truth table0%0/2
A
A= delete $conf->{'services_class'}
Aresult
11
00
25
26 0 my $e = load_class($services_class);
27 0 ▶ if($e){
Branch: 2 branches
BranchTrueFalseTotal
if ($e) { } 0 0 0
elsif ($services_class->isa('Mojolicious::Services')) { } 0 0 0
28 0 ▶ ref $e ? die $e:die "can't fond the module named '$services_class' ,inspect your installed please";
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 0 0 0
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 0 ▶ $app->helper(service=>sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 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<bug-mojolicious-Services at rt.cpan.org>, or through
129 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Services>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Services>
149  
150 =item * AnnoCPAN: Annotated CPAN documentation
151  
152 L<http://annocpan.org/dist/Mojolicious-Services>
153  
154 =item * CPAN Ratings
155  
156 L<http://cpanratings.perl.org/d/Mojolicious-Services>
157  
158 =item * Search CPAN
159  
160 L<http://search.cpan.org/dist/Mojolicious-Services/>
161  
162 =back
163  
164  
165 =cut
166  
167 1 1; # End of Mojolicious::Plugin::Service
« Service.pm ↑ Summary Services.pm »