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 4     4   1581 use Moose;
  4         8  
  4         118  
4             with 'MooseX::DIC::Configuration';
5              
6 4     4   24003 use YAML::XS;
  4         7482  
  4         216  
7 4     4   1016 use File::Spec::Functions qw/splitpath rel2abs/;
  4         2650  
  4         241  
8 4     4   27 use File::Slurper 'read_binary';
  4         8  
  4         144  
9 4     4   20 use Try::Tiny;
  4         8  
  4         199  
10 4     4   1090 use MooseX::DIC::Configuration::Scanner::FileConfig 'fetch_config_files_from_path';
  4         9  
  4         188  
11 4     4   23 use aliased 'MooseX::DIC::ContainerConfigurationException';
  4         7  
  4         25  
12 4     4   549 use aliased 'MooseX::DIC::Configuration::ServiceMetadata';
  4         7  
  4         13  
13 4     4   2046 use MooseX::DIC::Configuration::ServiceMetadata::Dependency qw/from_attribute from_yaml/;
  4         15  
  4         24  
14 4     4   1528 use Module::Load 'load';
  4         9  
  4         33  
15              
16             sub get_services_metadata_from_path {
17 7     7 0 28 my ($self,$paths) = @_;
18              
19             return
20 7         34 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 16 my ($config_file) = @_;
26              
27 4 50       86 ContainerConfigurationException->throw(message=>"Specified config file $config_file not found")
28             unless -f $config_file;
29              
30             # Parse YAML config file
31 4         11 my $raw_config;
32             try {
33 4     4   267 my $config_content = read_binary($config_file);
34 4         607 $raw_config = Load $config_content;
35             } catch {
36 0     0   0 ContainerConfigurationException->throw(message=>"Error while loading config file $config_file: $_");
37 4         52 };
38              
39             # Load included files, to be applied later
40 4         105 my @included_files = ();
41 4 100       36 push @included_files,@{$raw_config->{include}} if exists($raw_config->{include});
  1         4  
42              
43 4         12 my @services_metadata = ();
44 4         11 while(my ($interface,$implementators) = each(%{$raw_config->{mappings}})) {
  9         58  
45              
46             #Make sure the interface package is loaded
47 5         29 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       28227 if(ref($implementators) eq 'HASH') {
55 4         25 while( my ($implementator,$definition) = each(%$implementators)) {
56              
57             # Make sure the implementator package is loaded
58 4         17 load $implementator;
59              
60 4         39407 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       249 (exists($definition->{qualifiers})? (qualifiers => $definition->{qualifiers}):()),
    100          
    100          
    50          
69             dependencies => \%dependencies
70             );
71 4         33 push @services_metadata, $service_metadata;
72             }
73             } else {
74             # Make sure the implementator package is loaded
75 1         10 load $implementators;
76              
77 1         20518 my %dependencies = build_dependencies_for($implementators);
78              
79 1         68 my $service_metadata = ServiceMetadata->new(
80             class_name => $implementators,
81             implements => $interface,
82             dependencies => \%dependencies
83             );
84 1         6 push @services_metadata, $service_metadata;
85             }
86              
87             }
88              
89             # Load include files
90             push @services_metadata,
91 1         58 map { build_services_metadata_from_config_file($_) }
92 4         17 map { normalize_included_file_path($config_file,$_) }
  1         8  
93             @included_files;
94              
95 4         45 return @services_metadata;
96             }
97              
98             sub build_dependencies_for {
99 5     5 0 22 my ($package,$definition) = @_;
100              
101             my %dependencies =
102 5         33 map {( $_->name => from_attribute($_) ) }
  2         157  
103             $package->meta->get_all_attributes;
104              
105             # Override the dependency metadata found in the class with specific config
106 5 100 100     246 if(defined($definition) and exists($definition->{dependencies})){
107 1         2 while(my ($dependency,$dependency_definition) = each(%{$definition->{dependencies}})) {
  2         9  
108 1         4 $dependencies{$dependency} = from_yaml($dependency,$dependency_definition);
109             }
110             }
111              
112 5         25 return %dependencies;
113              
114             }
115              
116             sub normalize_included_file_path {
117 1     1 0 6 my ($original_file,$included_file) = @_;
118 1         11 my ($volume,$path,$file) = splitpath($original_file);
119              
120 1         33 return rel2abs($included_file,$path);
121             }
122              
123             1;