File Coverage

lib/MooseX/DIC/Configuration/YAML.pm
Criterion Covered Total %
statement 71 72 98.6
branch 14 16 87.5
condition 3 3 100.0
subroutine 15 16 93.7
pod 0 4 0.0
total 103 111 92.7


line stmt bran cond sub pod time code
1             package MooseX::DIC::Configuration::YAML;
2              
3 5     5   2172 use Moose;
  5         12  
  5         32  
4             with 'MooseX::DIC::Configuration';
5              
6 5     5   33549 use YAML::XS;
  5         11051  
  5         284  
7 5     5   1532 use File::Spec::Functions qw/splitpath rel2abs/;
  5         3426  
  5         316  
8 5     5   41 use File::Slurper 'read_binary';
  5         11  
  5         200  
9 5     5   30 use Try::Tiny;
  5         8  
  5         231  
10 5     5   1512 use MooseX::DIC::Configuration::Scanner::FileConfig 'fetch_config_files_from_path';
  5         17  
  5         254  
11 5     5   32 use aliased 'MooseX::DIC::ContainerConfigurationException';
  5         11  
  5         38  
12 5     5   837 use aliased 'MooseX::DIC::Configuration::ServiceMetadata';
  5         12  
  5         18  
13 5     5   2956 use MooseX::DIC::Configuration::ServiceMetadata::Dependency qw/from_attribute from_yaml/;
  5         24  
  5         36  
14 5     5   2161 use Module::Load 'load';
  5         14  
  5         51  
15              
16             sub get_services_metadata_from_path {
17 8     8 0 30 my ($self,$paths) = @_;
18              
19             return
20 8         46 map { build_services_metadata_from_config_file($_) }
  3         16  
21             fetch_config_files_from_path($paths);
22             }
23              
24             sub build_services_metadata_from_config_file {
25 4     4 0 13 my ($config_file) = @_;
26              
27 4 50       77 ContainerConfigurationException->throw(message=>"Specified config file $config_file not found")
28             unless -f $config_file;
29              
30             # Parse YAML config file
31 4         9 my $raw_config;
32             try {
33 4     4   235 my $config_content = read_binary($config_file);
34 4         714 $raw_config = Load $config_content;
35             } catch {
36 0     0   0 ContainerConfigurationException->throw(message=>"Error while loading config file $config_file: $_");
37 4         48 };
38              
39             # Load included files, to be applied later
40 4         89 my @included_files = ();
41 4 100       21 push @included_files,@{$raw_config->{include}} if exists($raw_config->{include});
  1         5  
42              
43 4         10 my @services_metadata = ();
44 4         7 while(my ($interface,$implementators) = each(%{$raw_config->{mappings}})) {
  9         54  
45              
46             #Make sure the interface package is loaded
47 5         27 load $interface;
48              
49             # The config specs allows the implementators of an interface to be specified
50             # either as a string which defines a simple implementator with default values,
51             # or as a hash of full implementators wich are key-value service metadata
52             # definitions.
53              
54 5 100       27679 if(ref($implementators) eq 'HASH') {
55 4         39 while( my ($implementator,$definition) = each(%$implementators)) {
56              
57             # Make sure the implementator package is loaded
58 4         16 load $implementator;
59              
60 4         39834 my %dependencies = build_dependencies_for($implementator,$definition);
61              
62             my $service_metadata = ServiceMetadata->new(
63             class_name => $implementator,
64             implements => $interface,
65             (exists($definition->{scope})? (scope => $definition->{scope}):()),
66             (exists($definition->{builder})? (builder => $definition->{builder}):()),
67             (exists($definition->{environment})? (environment => $definition->{environment}):()),
68 4 100       186 (exists($definition->{qualifiers})? (qualifiers => $definition->{qualifiers}):()),
    100          
    100          
    50          
69             dependencies => \%dependencies
70             );
71 4         28 push @services_metadata, $service_metadata;
72             }
73             } else {
74             # Make sure the implementator package is loaded
75 1         4 load $implementators;
76              
77 1         10850 my %dependencies = build_dependencies_for($implementators);
78              
79 1         38 my $service_metadata = ServiceMetadata->new(
80             class_name => $implementators,
81             implements => $interface,
82             dependencies => \%dependencies
83             );
84 1         4 push @services_metadata, $service_metadata;
85             }
86              
87             }
88              
89             # Load include files
90             push @services_metadata,
91 1         51 map { build_services_metadata_from_config_file($_) }
92 4         16 map { normalize_included_file_path($config_file,$_) }
  1         5  
93             @included_files;
94              
95 4         38 return @services_metadata;
96             }
97              
98             sub build_dependencies_for {
99 5     5 0 16 my ($package,$definition) = @_;
100              
101             my %dependencies =
102 5         23 map {( $_->name => from_attribute($_) ) }
  2         112  
103             $package->meta->get_all_attributes;
104              
105             # Override the dependency metadata found in the class with specific config
106 5 100 100     186 if(defined($definition) and exists($definition->{dependencies})){
107 1         2 while(my ($dependency,$dependency_definition) = each(%{$definition->{dependencies}})) {
  2         8  
108 1         5 $dependencies{$dependency} = from_yaml($dependency,$dependency_definition);
109             }
110             }
111              
112 5         17 return %dependencies;
113              
114             }
115              
116             sub normalize_included_file_path {
117 1     1 0 4 my ($original_file,$included_file) = @_;
118 1         6 my ($volume,$path,$file) = splitpath($original_file);
119              
120 1         21 return rel2abs($included_file,$path);
121             }
122              
123             1;