File Coverage

lib/MooseX/DIC/ContainerFactory.pm
Criterion Covered Total %
statement 34 34 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 0 1 0.0
total 42 43 97.6


line stmt bran cond sub pod time code
1             package MooseX::DIC::ContainerFactory;
2              
3 5     5   1933 use Moose;
  5         2189741  
  5         38  
4             with 'MooseX::DIC::Loggable';
5              
6 5     5   38922 use aliased 'MooseX::DIC::Container::DefaultImpl';
  5         3335  
  5         26  
7 5     5   568 use aliased 'MooseX::DIC::Configuration::Code';
  5         9  
  5         27  
8 5     5   3485 use aliased 'MooseX::DIC::Configuration::YAML';
  5         13  
  5         30  
9 5     5   598 use List::Util 'reduce';
  5         14  
  5         1662  
10              
11             has environment => (is => 'ro', isa => 'Str', default => 'default' );
12             has scan_path => ( is => 'ro', isa => 'ArrayRef[Str]', required => 1 );
13              
14             sub build_container {
15 8     8 0 38 my ($self) = @_;
16              
17             # Build the registry
18 8         260 $self->logger->debug("Building the registry for the container...");
19 8         136 my $registry = MooseX::DIC::ServiceRegistry->new;
20 8         4086 $self->_apply_config_to($registry);
21 8         1896 $self->logger->debug($registry->services_count." services registered");
22            
23             # Build the container
24 8         290 my $container = DefaultImpl->new( environment => $self->environment, registry => $registry );
25              
26 8         7087 $self->logger->debug("The container has been built from the registry");
27 8         109 return $container;
28             }
29              
30             sub _apply_config_to {
31 8     8   27 my ($self,$registry) = @_;
32              
33 8         91 my @config_readers = ( Code->new, YAML->new );
34              
35 8     2   4449 my $paths = reduce { $a." ".$b } @{$self->scan_path};
  2         7  
  8         368  
36 8         275 $self->logger->debug("Fetching services from scanning inside $paths...");
37 8         140 foreach my $reader (@config_readers) {
38 16         763 my @services_metadata = $reader->get_services_metadata_from_path( $self->scan_path );
39 16         107 foreach my $service_metadata (@services_metadata) {
40 19         156 $registry->add_service_definition($service_metadata);
41 19         628 $self->logger->debug("Service ".$service_metadata->class_name." was registered for interface ".$service_metadata->implements);
42             }
43             }
44             }
45              
46             1;