File Coverage

lib/MooseX/DIC/Container/DefaultImpl.pm
Criterion Covered Total %
statement 39 39 100.0
branch 12 12 100.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 0 3 0.0
total 61 66 92.4


line stmt bran cond sub pod time code
1             package MooseX::DIC::Container::DefaultImpl;
2              
3 4     4   2420 use MooseX::DIC::Types;
  4         11  
  4         126  
4 4     4   25 use aliased 'MooseX::DIC::UnregisteredServiceException';
  4         7  
  4         27  
5 4     4   436 use aliased 'MooseX::DIC::ServiceRegistry';
  4         9  
  4         24  
6 4     4   2257 use MooseX::DIC::ServiceFactoryFactory 'build_factory';
  4         11  
  4         216  
7              
8 4     4   28 use Moose;
  4         9  
  4         27  
9             with 'MooseX::DIC::Container';
10              
11             has environment => ( is => 'ro', isa => 'Str', default => 'default' );
12             has registry => (is => 'ro', isa => 'MooseX::DIC::ServiceRegistry', required => 1);
13             has singletons => (is => 'ro', isa => 'HashRef[HashRef[Any]]', default => sub { { default => {} } });
14             has service_factories => ( is => 'ro', isa => 'HashRef[ServiceFactory]', default => sub { {} } );
15              
16             sub has_service {
17 6     6 0 24 my ( $self, $interface_name ) = @_;
18              
19 6         196 return $self->registry->has_service($interface_name);
20             }
21              
22             sub get_service {
23 30     30 0 20845 my ( $self, $package_name,$original_environment ) = @_;
24 30   33     1086 my $environment = $original_environment || $self->environment;
25              
26             # Check it is a registered service
27 30         890 my $meta = $self->registry->get_service_definition($package_name,$environment);
28 30 100       94 UnregisteredServiceException->throw( service => $package_name )
29             unless $meta;
30              
31 29         45 my $service;
32              
33             # If it is a singleton, there's a chance it has already been built
34 29 100       911 if ( $meta->scope eq 'singleton' ) {
35              
36             # First retrieve it from the environment, then from default environment
37 22         696 $service = $self->singletons->{ $meta->environment }->{$package_name};
38 22 100       456 $service = $self->singletons->{'default'}->{$package_name}
39             unless $service;
40             }
41 29 100       197 return $service if $service;
42              
43             # If the service hasn't been built yet, use the builder class to do it
44 21         630 my $service_factory = $self->_get_service_factory( $meta->builder );
45 21         110 $service = $service_factory->build_service($meta);
46              
47             # Cache the service if it's a singleton
48 20 100       701 if ( $meta->scope eq 'singleton' ) {
49 13         395 $self->singletons->{ $meta->environment }->{$package_name} = $service;
50             }
51              
52 20         101 return $service;
53             }
54              
55             sub get_service_metadata {
56 3     3 0 12 my ($self,$interface_name,$environment) = @_;
57              
58 3         73 return $self->registry->get_service_definition($interface_name,$environment);
59             }
60              
61             sub _get_service_factory {
62 21     21   57 my ( $self, $factory_type ) = @_;
63              
64 21         633 my $service_factory = $self->service_factories->{$factory_type};
65 21 100       55 unless ($service_factory) {
66 8         35 $service_factory = build_factory( $factory_type, $self );
67 8         265 $self->service_factories->{$factory_type} = $service_factory;
68             }
69              
70 21         52 return $service_factory;
71             }
72              
73             1;