| 1 | package Mojolicious::Services; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Mojo::Base 'Mojolicious::Service'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Mojo::Loader qw/find_modules load_class/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Mojo::Util qw/camelize decamelize/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '2.1.0'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | has services => sub{{}}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | has namespaces => sub{["Mojolicious::Service"]}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | has lazy => 1; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub load_service{ | ||||||||||||||||||||||||||
|
Subroutine
load_service: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 1 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Try all namespaces and full module name | ||||||||||||||||||||||||||||
| 19 | 1 | ▶ | my $suffix = $name =~ /^[a-z0-9]/ ? camelize $name : $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 1 | my @classes = map {"${_}::$suffix"} @{$self->namespaces}; | |||||||||||||||||||||||||||
| 21 | 1 | for my $class (@classes, $name){ | |||||||||||||||||||||||||||
| 22 | 3 | ▶ | if(_load($class)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 1 | my $service = $class->new(models => $self->models, dbi => $self->dbi, app => $self->app, parent => $self); | |||||||||||||||||||||||||||
| 24 | 1 | Scalar::Util::weaken $service->{app}; | |||||||||||||||||||||||||||
| 25 | 1 | Scalar::Util::weaken $service->{models}; | |||||||||||||||||||||||||||
| 26 | 1 | Scalar::Util::weaken $service->{dbi}; | |||||||||||||||||||||||||||
| 27 | 1 | Scalar::Util::weaken $service->{parent}; | |||||||||||||||||||||||||||
| 28 | 1 | $self->service($name, $service); | |||||||||||||||||||||||||||
| 29 | 1 | return $service; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Not found | ||||||||||||||||||||||||||||
| 34 | 0 | die qq{Service "$name" missing, maybe you need to install it?\n}; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub load_all_service{ | ||||||||||||||||||||||||||
|
Subroutine
load_all_service: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 0 | foreach(map{find_modules($_)}@{$self->namespaces}){ | |||||||||||||||||||||||||||
| 40 | 0 | $_ =~ /^.+\:([^\:]+)$/; | |||||||||||||||||||||||||||
| 41 | 0 | my $name = decamelize($1); | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | if(_load($_)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | my $service = $_->new(models => $self->models, dbi => $self->dbi, app => $self->app, parent => $self); | |||||||||||||||||||||||||||
| 44 | 0 | Scalar::Util::weaken $service->{app}; | |||||||||||||||||||||||||||
| 45 | 0 | Scalar::Util::weaken $service->{models}; | |||||||||||||||||||||||||||
| 46 | 0 | Scalar::Util::weaken $service->{dbi}; | |||||||||||||||||||||||||||
| 47 | 0 | Scalar::Util::weaken $service->{parent}; | |||||||||||||||||||||||||||
| 48 | 0 | $self->service($name, $service); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub new{ | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.1
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 1 | my ($self, $conf) = @_; | |||||||||||||||||||||||||||
| 55 | 1 | ▶ | my $namespaces = delete $conf->{namespaces} if($conf->{namespaces}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1 | $self = $self->SUPER::new($conf); | |||||||||||||||||||||||||||
| 57 | 1 | ▶ | if($namespaces && ref $namespaces eq "ARRAY"){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$namespaces and ref $namespaces eq "ARRAY"
MC/DC0%0/2
$namespaces and ref $namespaces eq "ARRAY"
$namespaces
ref $namespaces eq "ARRAY"
Truth table33%1/3
A and B
A= $namespacesB= ref $namespaces eq "ARRAY"
| |||||||||||||||||||||||||||||
| 58 | 1 | unshift(@{$self->namespaces}, $_) for(reverse @{$namespaces}); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 1 | ▶ | $self->load_all_service unless($self->lazy); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 1 | return $self; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | ▶ | sub service{ | ||||||||||||||||||||||||||
|
Subroutine
service: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 2 | my ($self, $name, $service) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Set service | ||||||||||||||||||||||||||||
| 68 | 2 | ▶ | if($service){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 1 | $self->services->{$name} = $service; | |||||||||||||||||||||||||||
| 70 | 1 | return $self; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | ▶ | unless($self->services->{$name}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | $self->load_service($name); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Check services existence | ||||||||||||||||||||||||||||
| 78 | 1 | ▶ | croak qq{service "$name" is not yet created } unless($self->services->{$name}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Get service | ||||||||||||||||||||||||||||
| 81 | 1 | return $self->services->{$name}; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 3 | ▶ | sub _load{ | ||||||||||||||||||||||||||
|
Subroutine
_load: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 87 | 3 | my $module = shift; | |||||||||||||||||||||||||||
| 88 | 3 | ▶ | return $module->isa('Mojolicious::Service') unless(my $e = load_class $module); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | ref $e ? die $e : return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =encoding utf8 | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 NAME | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Mojolicious::Services - Mojolicious::Services 是为Mojolicious框架提供的Service管理插件。 | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | use Mojolicious::services | ||||||||||||||||||||||||||||
| 102 | my $service_manage = Mojolicious::services->new({ | ||||||||||||||||||||||||||||
| 103 | dbi=>DBIx::Custom->new(), | ||||||||||||||||||||||||||||
| 104 | models=>{}, | ||||||||||||||||||||||||||||
| 105 | namespaces=>s["Mojolicious::Service"], | ||||||||||||||||||||||||||||
| 106 | lazy => 1 | ||||||||||||||||||||||||||||
| 107 | }); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | ## fetch a service | ||||||||||||||||||||||||||||
| 110 | my $user_service = $service_manage->service("user"); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Mojolicious::services是为Mojolicious框架提供Service支持的模块。 | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Mojolicious::services 从 Mojolicious::Service中继承了所有属性,并实现以下属性。 | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head2 services | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 存储service的属性。 | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 namespaces | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | namespaces 用于说明service类所在的命名空间,这个属性的值是一个arrayref 类型的值,支持在多个命名空间中查找service。 | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 lazy | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 用于说明是否启用懒加载模式。 | ||||||||||||||||||||||||||||
| 137 | 如果值为true则启用懒加载,只有在实际请求一个service时才加载其类并实例化一个service对象。 | ||||||||||||||||||||||||||||
| 138 | 如果为flase则在创建Mojolicious::services时加载所有service类并实例化成对象。 | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 METHODS | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Mojolicious::services 从 Mojolicious::Service中继承了所有方法,并实现以下方法。 | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head2 load_service | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 根据service的名字加载service。 | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head2 load_all_service | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 加载 namespaces 属性指定的所有命名空间下的所有service,并实例化。 | ||||||||||||||||||||||||||||
| 154 | 注:只有在非懒加载模式的初始化阶段才会调用这个方法。 | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 new | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 生成一个新的Mojolicious::services对象。 | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 service | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 根据 service 的名称从 services 属性中获取 service。如果在 services 属性中不存在对应的键,则尝试从 namespaces 属性指定的命名空间中加载并实例化一个service。如果尝试加载后仍获取失败,则返回 undef。 | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | wfso, C<< <461663376@qq.com> >> | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 BUGS | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Please report any bugs or feature requests to C<bug-mojolicious-services at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 176 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-services>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 177 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | perldoc Mojolicious::services | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | You can also look for information at: | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =over 4 | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-services> | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | L<http://annocpan.org/dist/Mojolicious-services> | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | L<http://cpanratings.perl.org/d/Mojolicious-services> | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | L<http://search.cpan.org/dist/Mojolicious-services/> | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =back | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =cut | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 2 | 1; # End of Mojolicious::services | |||||||||||||||||||||||||||