File Coverage

lib/Devel/ebug/Wx/ServiceManager.pm
Criterion Covered Total %
statement 18 58 31.0
branch 0 14 0.0
condition n/a
subroutine 6 18 33.3
pod 8 11 72.7
total 32 101 31.6


line stmt bran cond sub pod time code
1             package Devel::ebug::Wx::ServiceManager;
2              
3 1     1   968 use strict;
  1         2  
  1         29  
4 1     1   5 use base qw(Class::Accessor::Fast);
  1         2  
  1         72  
5 1     1   5 use Devel::ebug::Wx::Plugin qw(:manager);
  1         23  
  1         596  
6              
7             =head1 NAME
8              
9             Devel::ebug::Wx::Service::ServiceManager - manage services
10              
11             =head1 SYNOPSIS
12              
13             my $sm = $wxebug->service_manager; # or find it elsewhere
14             my $service = $sm->get_service( $service_name );
15             # use the $service
16              
17             # alternate ways of getting a service
18             my $srv = $wxebug->service_manager->get_service( 'foo_frobnicate' );
19             my $srv = $wxebug->foo_frobnicate_service;
20              
21             =head1 DESCRIPTION
22              
23             The service manager is responsible for finding, initializing and
24             terminating services. Users of the service usually need just to call
25             C to retrieve a service instance.
26              
27             =head1 METHODS
28              
29             =cut
30              
31             load_plugins( search_path => 'Devel::ebug::Wx::Service' );
32              
33             __PACKAGE__->mk_ro_accessors( qw(_active_services _wxebug) );
34              
35             =head2 services
36              
37             my @service_classes = Devel::ebug::Wx::ServiceManager->services;
38              
39             Returns a list of service classes known to the service manager.
40              
41             =head2 active_services
42              
43             my @services = $sm->active_services;
44              
45             Returns a list of services currently registered with the service manager.
46              
47             =cut
48              
49 0     0 1   sub active_services { @{$_[0]->_active_services} }
  0            
50 0     0 1   sub services { Devel::ebug::Wx::Plugin->service_classes }
51 0     0 0   sub add_service { push @{$_[0]->_active_services}, $_[1] }
  0            
52              
53             sub new {
54 0     0 1   my( $class ) = @_;
55 0           my $self = $class->SUPER::new;
56 0           my @services = map $_->new, $self->services;
57              
58 0           $self->{_active_services} = \@services;
59              
60 0           return $self;
61             }
62              
63             =head2 initialize
64              
65             $sm->initialze( $wxebug );
66              
67             Calls C on all service instances and sets their
68             C property to true.
69              
70             =cut
71              
72             sub initialize {
73 0     0 1   my( $self ) = @_;
74              
75 0           foreach my $service ( $self->active_services ) {
76 0 0         next if $service->initialized;
77 0 0         $service->service_manager( $self )
78             if $service->can( 'service_manager' );
79 0           $service->initialize( $self );
80 0           $service->initialized( 1 );
81             }
82             }
83              
84             =head2 load_configuration
85              
86             $sm->load_configuration;
87              
88             Calls C on all service instances.
89              
90             =head2 maybe_call_method
91              
92             $sm->maybe_call_method( $method, @args );
93              
94             Calls method C<$method> on all active services that provide it, passing
95             C<@args> as arguments.
96              
97             =cut
98              
99             sub load_configuration {
100 0     0 1   my( $self ) = @_;
101              
102 0           $_->load_configuration foreach $self->active_services;
103             }
104              
105             sub maybe_call_method {
106 0     0 1   my( $self, $method, @args ) = @_;
107              
108 0           $_->$method( @args ) foreach grep $_->can( $method ),
109             $self->active_services;
110             }
111              
112             =head2 finalize
113              
114             $sm->finalize( $wxebug );
115              
116             Calls C on all service instances, then calls C
117             on them and sets their C property to true.
118              
119             Important: the C property is still true even after
120             C has been called..
121              
122             =cut
123              
124             sub finalize {
125 0     0 1   my( $self, $wxebug ) = @_;
126              
127             # distinguish between explicit and implicit state saving?
128 0           $_->save_configuration foreach $self->active_services;
129 0           foreach my $service ( $self->active_services ) {
130 0 0         next if $service->finalized;
131 0           $service->finalize;
132 0           $service->finalized( 1 );
133             }
134             }
135              
136             =head2 get_service
137              
138             my $service_instance = $sm->get_service( 'service_name' );
139              
140             Returns an active service with the given name, or C if none is
141             found. If the service has not been initialized, calls C
142             as well, but not C.
143              
144             =cut
145              
146             sub get_service {
147 0     0 1   my( $self, $name ) = @_;
148 0           my( $service, @rest ) = grep $_->service_name eq $name,
149             $self->active_services;
150              
151             # @rest can be nonempty only if two clashing services exist
152 0 0         unless( $service->initialized ) {
153 0 0         $service->service_manager( $self )
154             if $service->can( 'service_manager' );
155 0           $service->initialize( $self );
156 0           $service->initialized( 1 );
157             }
158 0           return $service;
159             }
160              
161             =head1 SEE ALSO
162              
163             L
164              
165             =cut
166              
167             # FIXME document
168             package Devel::ebug::Wx::ServiceManager::Holder;
169              
170 1     1   6 use strict;
  1         1  
  1         23  
171 1     1   4 use base qw(Exporter);
  1         2  
  1         111  
172              
173             our( @EXPORT, %EXPORT_TAGS );
174             BEGIN {
175 1     1   2 $INC{'Devel/ebug/Wx/ServiceManager/Holder.pm'} = __FILE__;
176 1         2 @EXPORT = qw(AUTOLOAD service_manager get_service);
177 1         178 %EXPORT_TAGS = ( 'noautoload' => [ qw(service_manager get_service) ] );
178             }
179              
180             sub service_manager { # the usual getter/setter
181 0 0   0 0   return $_[0]->{service_manager} = $_[1] if @_ > 1;
182 0           return $_[0]->{service_manager};
183             }
184              
185             # remap ->xxx_yy_service to ->get_service( 'xxx_yy' )
186             our $AUTOLOAD;
187             sub AUTOLOAD {
188 0     0     my $self = shift;
189 0 0         return if $AUTOLOAD =~ /::DESTROY$/;
190 0           ( my $sub = $AUTOLOAD ) =~ s/.*::(\w+)_service$/$1/;
191 0           return $self->get_service( $1 );
192             }
193              
194 0     0 0   sub get_service { $_[0]->service_manager->get_service( $_[1] ) }
195              
196             1;